talentRankActions.js 1.72 KB
'use strict';

import ReactNative from 'react-native';
import Service from '../../services/TalentRankService';

const {
    TALENT_RANK_REQUEST,
    MONTH_RANK_SUCCESS,
    TOTAL_RANK_SUCCESS,
    TALENT_RANK_FAILURE

} = require('../../constants/actionTypes').default;

export function talentRankRequest() {
    return {
        type: TALENT_RANK_REQUEST
    }
}

export function monthRankSuccess(json) {
    return {
        type: MONTH_RANK_SUCCESS,
        payload: json
    }
}

export function talentRankFailue(error) {
    return {
        type: TALENT_RANK_FAILURE,
        payload: error
    }
}

export function totalRankSuccess(json) {
    return {
        type: TOTAL_RANK_SUCCESS,
        payload: json
    }
}

export function fetchTalentRank(params) {
    return async (dispatch, getState) => {
        let uid = await ReactNative.NativeModules.YH_CommonHelper.uid();
        let { app: { host }, talentRank } = getState();
        let { type, page, pageSize } = params;
        let originRank = talentRank.get(type == 1 ? 'monthRank' : 'totalRank').toJS();
        !page && (params.page = originRank.rankList.page + 1) //如果page不存在  就是请求下一页
        if (params.page > originRank.rankList.totalPage) {
            let a = 1;
            return
        }
        !pageSize && (params.pageSize = 10)
        dispatch(talentRankRequest());
        let data;
        try {
            data = await new Service(host).fetchTalentRank({uid, ...params});
        } catch(error) {
            dispatch(talentRankFailue(error));
        }
        data.rankList && (data.rankList.list = originRank.rankList.list.concat(data.rankList.list))
        type == 1 ? dispatch(monthRankSuccess(data)) : dispatch(totalRankSuccess(data));
    }
}