|
|
'use strict';
|
|
|
|
|
|
import ReactNative from 'react-native';
|
|
|
import PlustarService from '../../services/PlustarService';
|
|
|
|
|
|
const {
|
|
|
SET_SEGMENT,
|
|
|
SET_GENDER,
|
|
|
PLUSTAR_LIST_REQUEST,
|
|
|
PLUSTAR_LIST_SUCCESS,
|
|
|
PLUSTAR_LIST_FAILURE,
|
|
|
SWITCH_SEGMENT,
|
|
|
SWITCH_GENDER,
|
|
|
JUMP_WITH_URL,
|
|
|
} = require('../../constants/actionTypes').default;
|
|
|
|
|
|
export function setSegment(segmentType) {
|
|
|
export function setSegment(segment) {
|
|
|
return {
|
|
|
type: SET_SEGMENT,
|
|
|
payload: segmentType,
|
|
|
payload: segment,
|
|
|
};
|
|
|
}
|
|
|
|
|
|
export function plustarListRequest() {
|
|
|
export function setGender(gender) {
|
|
|
return {
|
|
|
type: SET_GENDER,
|
|
|
payload: gender,
|
|
|
};
|
|
|
}
|
|
|
|
|
|
export function swithSegment(activeTab) {
|
|
|
return (dispatch, getState) => {
|
|
|
|
|
|
const {plustar} = getState();
|
|
|
if(activeTab == plustar.activeTab) {
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
dispatch({
|
|
|
type: SWITCH_SEGMENT,
|
|
|
payload: activeTab
|
|
|
});
|
|
|
};
|
|
|
}
|
|
|
|
|
|
export function switchGender(genderType) {
|
|
|
return (dispatch, getState) => {
|
|
|
|
|
|
const {plustar} = getState();
|
|
|
if(genderType == plustar.genderType) {
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
dispatch({
|
|
|
type: SWITCH_GENDER,
|
|
|
payload: genderType
|
|
|
});
|
|
|
};
|
|
|
}
|
|
|
|
|
|
export function plustarListRequest(key) {
|
|
|
return {
|
|
|
type: PLUSTAR_LIST_REQUEST,
|
|
|
payload: key
|
|
|
};
|
|
|
}
|
|
|
|
|
|
export function plustarListSuccess(json) {
|
|
|
export function plustarListSuccess(json, key) {
|
|
|
return {
|
|
|
type: PLUSTAR_LIST_SUCCESS,
|
|
|
payload: json
|
|
|
payload: {...json, key}
|
|
|
};
|
|
|
}
|
|
|
|
|
|
export function plustarListFailure(error) {
|
|
|
export function plustarListFailure(error, key) {
|
|
|
return {
|
|
|
type: PLUSTAR_LIST_FAILURE,
|
|
|
payload: error
|
|
|
payload: {error, key}
|
|
|
};
|
|
|
}
|
|
|
|
|
|
export function plustarList(activeTab, gender) {
|
|
|
/*
|
|
|
* index number 请求数据的index
|
|
|
* reload bool 是否需要强制重新请求数据,
|
|
|
*/
|
|
|
export function plustarList(index, reload = false) {
|
|
|
return (dispatch, getState) => {
|
|
|
let {app, plustar} = getState();
|
|
|
let {activeTab, segment} = plustar;
|
|
|
let item = plustar.get(activeTab);
|
|
|
if (item.isFetching) {
|
|
|
return;
|
|
|
let key = index + '';
|
|
|
let item = plustar.get(key);
|
|
|
if (reload) {
|
|
|
// 强制刷新数据
|
|
|
|
|
|
} else {
|
|
|
if (item.isFetching || item.list.size > 0) {
|
|
|
return;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
dispatch(plustarListRequest());
|
|
|
|
|
|
let brandType = segment.get(activeTab).type;
|
|
|
let gender = item.genderType;
|
|
|
return new PlustarService().fetchList(brandType, gender)
|
|
|
|
|
|
dispatch(plustarListRequest(key));
|
|
|
|
|
|
let {segment, gender} = plustar;
|
|
|
let brandType = segment.get(key).type;
|
|
|
let options = {};
|
|
|
// 如果gender>0,接口请求需要区分男女,需要传递gender参数
|
|
|
if (gender > 0) {
|
|
|
options = {gender};
|
|
|
}
|
|
|
return new PlustarService().fetchList(brandType, options)
|
|
|
.then(json => {
|
|
|
let payload = parsePlustarList(json);
|
|
|
dispatch(plustarListSuccess(payload));
|
|
|
dispatch(plustarListSuccess(payload, key));
|
|
|
})
|
|
|
.catch(error => {
|
|
|
dispatch(plustarListFailure(error));
|
|
|
dispatch(plustarListFailure(error, key));
|
|
|
});
|
|
|
};
|
|
|
}
|
|
|
|
|
|
function parsePlustarList(json) {
|
|
|
let {brand_type, brand_type_name, data} = json;
|
|
|
|
|
|
let parseListData = (list) => {
|
|
|
if (!Array.isArray(list) || list.length == 0) {
|
|
|
return [];
|
|
|
}
|
|
|
|
|
|
let allData = [];
|
|
|
let length = list.length;
|
|
|
for(let i = 0; i < length; i++) {
|
|
|
let data = list[i].data;
|
|
|
allData = [...allData, ...data];
|
|
|
}
|
|
|
list.map((item, i) => {
|
|
|
let {data} = item;
|
|
|
if (data && data.length > 0) {
|
|
|
allData = [...allData, ...data];
|
|
|
}
|
|
|
});
|
|
|
|
|
|
return allData;
|
|
|
}
|
|
|
|
|
|
let header = parseListData(json.head);
|
|
|
let footer = parseListData(json.foot);
|
|
|
let list = parseListData(json.list);
|
|
|
let head = parseListData(data.head);
|
|
|
let foot = parseListData(data.foot);
|
|
|
let list = parseListData(data.list);
|
|
|
|
|
|
return {
|
|
|
header,
|
|
|
footer,
|
|
|
head,
|
|
|
foot,
|
|
|
list,
|
|
|
};
|
|
|
}
|
|
|
|
|
|
export function plustarRouter(url) {
|
|
|
|
|
|
RouterManager.jumpWithUrl(url, null);
|
|
|
export function jumpWithUrl(url) {
|
|
|
ReactNative.NativeModules.YH_PlustarHelper.jumpWithUrl(url);
|
|
|
return {
|
|
|
type: types.ROUTE_PLUSATAR,
|
|
|
type: JUMP_WITH_URL,
|
|
|
payload: url
|
|
|
};
|
|
|
}
|
|
|
|
|
|
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) {
|
|
|
|
...
|
...
|
|