recommendActions.js 5.03 KB
'use strict';

import ReactNative from 'react-native';
import RecommendService from '../../services/RecommendService';

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,

} = 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, recommend} = getState();

        if (recommend.isFetching) {
            return;
        }

        let fetchList = (uid, sourcePage) => {
            dispatch(historySortListRequest());
            return new RecommendService(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, recommend} = getState();
        let productList = [];
        if (recommend && recommend.productList && recommend.productList.size > 0) {
            productList = recommend.productList.toJSON();
        }
        ;

        if (recommend.isFetching) {
            return;
        }

        let fetchList = (uid, category_id, page, sourcePage) => {
            dispatch(historyListRequest(category_id));
            return new RecommendService(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 = recommend.currentPage + 1;
        if (!category_id && category_id != 0) {
            category_id = recommend.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);
            });
    };
}