browseActions.js 7.65 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, catName) {
	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);
				}
			});
		}

		for (var i = 0; i < newProductList.length; i++) {
			let item = newProductList[i];
			let yh_exposureData = {
				'P_NAME': '浏览记录',
				'TAB_ID': index + 1,
				'TAB_NAME': catName,
				'I_INDEX': i + 1,
				'PRD_SKN': item.product_skn?item.product_skn:'',
				exposureEnd : 1,
			};
			item.yh_exposureData = yh_exposureData;
		}

		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 : [];

	for (var i = 0; i < productList.length; i++) {
		let item = productList[i];
		let yh_exposureData = {
			'P_NAME': '浏览记录',
			'TAB_ID': '1',
			'TAB_NAME': '全部',
			'I_INDEX': i + 1 + '',
			'PRD_SKN': item.product_skn?item.product_skn:'',
			exposureEnd : 1,
		};
		item.yh_exposureData = yh_exposureData;
	}

	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(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 => {
				let {productList, selectedProductList} = browse;
							console.log('000')
			dispatch(deleteSelectedProducts(selectedProducts));
				let show = productList.size > 0;
				ReactNative.NativeModules.YH_RecorderHelper.setRightClearButtonVisiblity(show);
			})
			.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 => {

		});
	};
}

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

		if (browse.productList.size == 0) {
			return;
		}
		let {productList, selectedProductList, categoryList} = browse;
		let newProductList = [];
		let categoryId = selectedProducts[0].category_id;
		categoryList = categoryList.toJS();
		let newCategoryList = [];
		categoryList.map((categoryItem, i)=> {
			if (categoryItem.category_id != categoryId) {
				newCategoryList.push(categoryItem);
			}
		});
		productList = productList.toJS();
		productList.map((item, j) => {
			let exist = false;
			selectedProducts.map((selectedItem, i) => {
				if (item.product_skn == selectedItem.product_skn) {
					exist = true;
				}
			});
			if (!exist) {
				newProductList.push(item);
			}
		});

		dispatch({
			type: HISTORY_CLEAR_SUCCESS,
			payload: {productList: newProductList,categoryList:newCategoryList,index:0}
		});

	};
}