browseActions.js 5.72 KB
'use strict';

import ReactNative from 'react-native';
import BrowseService from '../../services/BrowseService';
import Immutable, {Map} from 'immutable';
const {
	HISTORY_LIST_REQUEST,
    HISTORY_LIST_SUCCESS,
    HISTORY_LIST_FAILURE,

    SET_SELECTED_CATEGORY,

    HISTORY_DELETE_REQUEST,
    HISTORY_DELETE_SUCCESS,
    HISTORY_DELETE_FAILURE,

    HISTORY_CLEAR_REQUEST,
    HISTORY_CLEAR_SUCCESS,
    HISTORY_CLEAR_FAILURE,
} = require('../../constants/actionTypes').default;


export function setSelectedCategory(catId, index) {
	return (dispatch, getState) => {
		let {app, browse} = getState();
		let productList = browse.productList.toJS();
		let newProductList = [];
		if (catId == -1) {
			newProductList = productList;
		} else {
			productList.map((item, i) => {
				if (item.category_id == catId) {
					newProductList.push(item);
				}
			});
		}
		
		dispatch({
			type: SET_SELECTED_CATEGORY,
			payload: {productList: newProductList, index}
		});
	};
}

export function historyListRequest() {
    return {
        type: HISTORY_LIST_REQUEST,
    };
}

export function historyListSuccess(json) {
    return {
        type: HISTORY_LIST_SUCCESS,
        payload: json
    };
}

export function historyListFailure(error) {
    return {
        type: HISTORY_LIST_FAILURE,
        payload: error
    };
}

export function historyList() {
	return (dispatch, getState) => {
		let {app, browse} = getState();

		if (browse.isFetching) {
			return;
		}

		let fetchList = (uid, page, pageSize, sourcePage) => {
			dispatch(historyListRequest());
			return new BrowseService(app.host).fetchList(uid, page, pageSize, sourcePage)
			.then(json => {
				let payload = parseHistoryList(json);
				dispatch(historyListSuccess(payload));
				let show = payload.productList.length > 0;
				ReactNative.NativeModules.YH_RecorderHelper.setRightClearButtonVisiblity(show);
			})
			.catch(error => {
				dispatch(historyListFailure(error));
			});
		}

		let uid = 0;
		let sourcePage = '';
		let page = browse.currentPage + 1;
		let pageSize = browse.pageSize;

		Promise.all([
            ReactNative.NativeModules.YH_CommonHelper.sourcePage('YH_MineBrowseHistoryVC'),
        ]).then(result => {
        	sourcePage = result[0];

        	return ReactNative.NativeModules.YH_CommonHelper.uid();
        }).then(data => {
			uid = data;
			fetchList(uid, page, pageSize, sourcePage);
		})
		.catch(error => {
			fetchList(uid, page, pageSize, sourcePage);
		});
	};
}

function parseHistoryList(json) {
	let currentPage = json && json.page ? json.page : 1;

	let productList = json && json.product_list ? json.product_list : [];
	let categoryList = json && json.category_list ? json.category_list : [];

	if (categoryList.length > 0) {
		categoryList = [{category_id: -1, category_name: '全部'} , ...categoryList];
	}

	return {
		categoryList,
		productList,
		currentPage,
	};
}

export function historyDeleteRequest() {
    return {
        type: HISTORY_DELETE_REQUEST,
    };
}

export function historyDeleteSuccess(json) {
    return {
        type: HISTORY_DELETE_SUCCESS,
        payload: json
    };
}

export function historyDeleteFailure(error) {
    return {
        type: HISTORY_DELETE_FAILURE,
        payload: error
    };
}

export function deleteOneHistory(skn, index) {
	return (dispatch, getState) => {
		let {app, browse} = getState();

		if (!skn || browse.isDeleting) {
			return;
		}

		let fetchList = (skn, uid, sourcePage) => {
			dispatch(historyDeleteRequest());
			return new BrowseService(app.host).deleteHistory(skn, uid, sourcePage)
			.then(json => {
				let {productList, selectedProductList} = browse;
				selectedProductList = selectedProductList.delete(index);
				let lists = productList.toArray();
				let indexInAll = productList.findIndex((item, i) => {
					return item.get('product_skn') == skn;
				});
				if (indexInAll != -1) {
					productList = productList.delete(indexInAll);
				}
				dispatch(historyDeleteSuccess({productList, selectedProductList}));
				let show = productList.size > 0;
				ReactNative.NativeModules.YH_RecorderHelper.setRightClearButtonVisiblity(show);
			})
			.catch(error => {
				dispatch(historyDeleteFailure(error));
			});
		}

		let uid = 0;
		let sourcePage = '';

		Promise.all([
            ReactNative.NativeModules.YH_CommonHelper.sourcePage('YH_MineBrowseHistoryVC'),
        ]).then(result => {
        	sourcePage = result[0];

        	return ReactNative.NativeModules.YH_CommonHelper.uid();
        }).then(data => {
			uid = data;
			fetchList(skn, uid, sourcePage);
		})
		.catch(error => {
			
		});
	};
}

export function historyClearRequest() {
    return {
        type: HISTORY_CLEAR_REQUEST,
    };
}

export function historyClearSuccess() {
    return {
        type: HISTORY_CLEAR_SUCCESS,
    };
}

export function historyClearFailure(error) {
    return {
        type: HISTORY_CLEAR_FAILURE,
        payload: error
    };
}

export function clearHistory() {
	return (dispatch, getState) => {
		let {app, browse} = getState();

		if (browse.productList.size == 0) {
			return;
		}

		let fetchList = (skn, uid, sourcePage) => {
			dispatch(historyClearRequest());
			return new BrowseService(app.host).deleteHistory(skn, uid, sourcePage)
			.then(json => {
				dispatch(historyClearSuccess());
				ReactNative.NativeModules.YH_RecorderHelper.setRightClearButtonVisiblity(false);
			})
			.catch(error => {
				dispatch(historyClearFailure(error));
			});
		}

		let uid = 0;
		let sourcePage = '';

		Promise.all([
            ReactNative.NativeModules.YH_CommonHelper.sourcePage('YH_MineBrowseHistoryVC'),
        ]).then(result => {
        	sourcePage = result[0];

        	return ReactNative.NativeModules.YH_CommonHelper.uid();
        }).then(data => {
			uid = data;
			fetchList(null, uid, sourcePage);
		})
		.catch(error => {
			
		});
	};
}