browseActions.js 7.31 KB
'use strict';

import ReactNative from 'react-native';
import BrowseService from '../../services/BrowseService';
import Immutable, {Map} from 'immutable';
import {Platform} from 'react-native';

const {
	HISTORY_SORT_LIST_REQUEST,
    HISTORY_SORT_LIST_SUCCESS,
    HISTORY_SORT_LIST_FAILURE,

	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, catName) {
	return (dispatch, getState) => {
		dispatch({
			type: SET_SELECTED_CATEGORY,
			payload: {index}
		});
		dispatch(historyList(catId));
	};
}

export function historySortListRequest() {
    return {
        type: HISTORY_SORT_LIST_REQUEST,
    };
}

export function historySortListSuccess(json) {
    return {
        type: HISTORY_SORT_LIST_SUCCESS,
        payload: json
    };
}

export function historySortListFailure(error) {
    return {
        type: HISTORY_SORT_LIST_FAILURE,
        payload: error
    };
}

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

		if (browse.isFetching) {
			return;
		}

		let fetchList = (uid, sourcePage) => {
			dispatch(historySortListRequest());
			return new BrowseService(app.host).fetchSortList(uid, sourcePage)
			.then(json => {
				if (json.length > 0) {
					json = [{category_id: -1, category_name: '全部'} , ...json];
				}
				dispatch(historySortListSuccess(json));
			})
			.catch(error => {
				dispatch(historySortListFailure(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(uid, sourcePage);
		})
		.catch(error => {
			fetchList(uid, sourcePage);
		});
	};
}

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

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(category_id) {
	return (dispatch, getState) => {
		let {app, browse} = getState();
		let productList = [];
		if (browse && browse.productList && browse.productList.size > 0) {
			productList = browse.productList.toJSON();
		};

		if (browse.isFetching) {
			return;
		}

		let fetchList = (uid, category_id, page, sourcePage) => {
			dispatch(historyListRequest(category_id));
			return new BrowseService(app.host).fetchPagelist(uid, category_id, page, sourcePage)
			.then(json => {

				let currentPage = json && json.page ? json.page : 1;
				let page_total = json && json.page_total ? json.page_total : 1;
				let newproductList = json && json.product_list ? json.product_list : [];
				if (productList.length) {
					if (newproductList.length) {
						productList=[...productList, ...newproductList];
					};
				} else if (newproductList.length){
					productList = newproductList;
				}
				
				dispatch(historyListSuccess({currentPage, page_total, productList}));
				let show = productList.length > 0;
				ReactNative.NativeModules.YH_RecorderHelper.setRightClearButtonVisiblity(show);
			})
			.catch(error => {
				dispatch(historyListFailure(error));
			});
		}

		let uid = 0;
		let sourcePage = '';
		let page = browse.currentPage + 1;
		if (!category_id) {
			category_id = browse.curCategoryID;
		};

		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, category_id, page, sourcePage);
		})
		.catch(error => {
			fetchList(uid, category_id, page, sourcePage);
		});
	};
}


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} = browse;
				let lists = productList.toArray();
				let indexInAll = productList.findIndex((item, i) => {
					return item.get('product_skn') == skn;
				});
				if (indexInAll != -1) {
					productList = productList.delete(indexInAll);
				}
				if (productList.size == 0 || productList.length == 0) {
					dispatch(historySortList());
				}else {
					dispatch(historyDeleteSuccess({productList}));
				}

				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(json) {
    return {
        type: HISTORY_CLEAR_SUCCESS,
        payload:json
    };
}

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

export function clearHistory(selectedProducts) {
	return (dispatch, getState) => {
		let skns = '';
        for (var i = 0; i < selectedProducts.length; i++) {
            skns = skns+selectedProducts[i].product_skn;
            skns = skns+',';
        }
		let {app, browse} = getState();

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

		let fetchList = (skns, uid, sourcePage) => {
			dispatch(historyClearRequest());
			return new BrowseService(app.host).deleteHistory(skns, uid, sourcePage)
			.then(json => {
				dispatch(historySortList());
				dispatch(historyList(0));
				dispatch({type: HISTORY_CLEAR_SUCCESS});
			})
			.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(skns, uid, sourcePage);
		})
		.catch(error => {

		});
	};
}