plustarAction.js 4.29 KB
import React from 'react-native';
import * as types from '../constants/ActionTypes';

const PlustarManager = React.NativeModules.YH_PlustarManager;
const GlobalManager = React.NativeModules.YH_GlobalManager;
const RouterManager = React.NativeModules.YH_RouterManager;
const HudManager = React.NativeModules.YH_HudManager;

export function plustarRouter(url) {

    RouterManager.jumpWithUrl(url, null);
    return {
        type: types.ROUTE_PLUSATAR,
    };
}

export function popViewController() {

    RouterManager.popViewController();
    return {
        type: types.ROUTE_POP_VIEW_CONTROLLER,
    };
}

export function switchGender(genderType) {
    return (dispatch, getState) => {

        const {plustar} = getState();
        if(genderType == plustar.payload.genderType) {
            return;
        }

        dispatch({
            type: types.SWITCH_GENDER,
            payload: {
                genderType,
            }
        });

        dispatch(fetchPlustarIfNeeded(0, genderType));
        dispatch(fetchPlustarIfNeeded(1, genderType));
    }; 
}

export function swithSegment(selectedIndex) {

    return (dispatch, getState) => {

        const {plustar} = getState();
        if(selectedIndex == plustar.payload.selectedIndex) {
            return;
        }

        dispatch({
            type: types.SWITCH_SEGMENT,
            payload: {
                selectedIndex,
            }
        });
    };
}

export function fetchPlustarIfNeeded(index, gender=null) {

    let selectedItem = indexToKey(index);
    return (dispatch, getState) => {

        const {plustar} = getState();
        const item = plustar.payload[selectedItem];
        let currentGender = plustar.payload.genderType;
        let reload = false;
        if (gender !== null) {
            reload = true;
            currentGender = gender;
        }

        if (reload || shouldFetchPlustar(item)) {
            if (index === plustar.payload.selectedIndex) {
                HudManager.showLoadingView();
            }
            
            let brandType = item.segment.type;
            return dispatch(fetchPlustar(brandType, currentGender, selectedItem));
        }
    }
}

function indexToKey(index) {

    if (index === 0) {
        return 'left';
    } else if (index === 1) {
        return 'right';
    }
}

function shouldFetchPlustar(item) {

    if (item.loading) {
        return false;
    }

    if (item.data && item.data.length > 0) {
        return false;
    }
    
    return true;
}

function fetchPlustar(brandType, genderType, selectedItem) {

    return dispatch => {
        
        dispatch(requestPlustar(selectedItem));

        let parameters = {
            brand_type: brandType,
            gender: genderType,
        };

        PlustarManager.fetchPlustarList(parameters, (error, value) => {

            HudManager.hideLoadingView();
            dispatch(receivePlustar(error, value, selectedItem));
        });
    };
  
}

function requestPlustar(selectedItem) {

    return {
        type: types.REQUEST_PLASTAR_DATA,
        payload: {
            selectedItem
        }
    };
}

function receivePlustar(error, response, selectedItem) {

    let payload = {};

    if (error) {
        HudManager.showText(error.message);

        return {
            type: types.RECEIVE_PLASTAR_DATA,
            payload: {
                selectedItem
            },
            error: true,
        }
    }

    let head = parseListData(response.data.head);
    let foot = parseListData(response.data.foot);
    let data = parseListData(response.data.list);

    payload = {
        [selectedItem]: {
            loading: false,
            head,
            foot,
            data,
            segment: {
                title: response.brand_type_name, 
                type: response.brand_type
            },
        },
        selectedItem,
    };

    return {
        type: types.RECEIVE_PLASTAR_DATA,
        payload,
    };

}

function createLoadingPayload(selectedItem, loading) {

    let payload = {};
    payload = {
        [selectedItem]: {
            loading: loading,
        },
    };

    return payload;
}

function parseListData(list) {
    
    let allData = [];
    let length = list.length;
    for(let i = 0; i < length; i++) {
        let data = list[i].data;
        allData = [...allData, ...data];
    }
    return allData;
}