screenSubActions.js 1.85 KB
'use strict';

import ReactNative from 'react-native';
import Utils from '../../utils/Utils';

const {
	SET_DATASOURCE_SUB,
	SET_SELECT_ITEM,
} = require('../../constants/actionTypes').default;


export function updateDataSource(data) {
	if (!data) {
		return;
	}
	let key = data?data.key:'';
	let json = data;
	if (key == 'brand') {
		json = processData(data);
	}
	return {
        type: SET_DATASOURCE_SUB,
		payload: json
    };
}

export function setSelectItem(json) {
	return {
        type: SET_SELECT_ITEM,
		payload: json
    };
}

export function selectItem(data) {
	return (dispatch, getState) => {
		let {app, screenSub} = getState();
		let {filterSub} = screenSub;
		let payload = parseList(data,filterSub);
		dispatch(setSelectItem(payload));
	};
}

function parseList(data,filterSub) {
	let list = filterSub&&filterSub.list?filterSub.list.toJS():[];
	for (var i = 0; i < list.length; i++) {
		let item = list[i];
		item.isSelect = false;
		if (item.name == data.name) {
			item.isSelect = true;
		}
	}
	return list;
}

function processData(data){
	let list = data?data.list:[];
	let latterObject={};
	let newObject={};
	let keys = [];
	let allObj = {};
	let specialList = [];

	list.forEach((item, i) => {
		let key = item.brand_alif?item.brand_alif:item.name;
		if (Utils.isContains(key,'全部') || Utils.isContains(key,'所有')) {
			allObj = item;
		}else if (!regChar(key) || key.length > 1) {
			specialList.push(item);
		} else{
			if (!latterObject[key]) {
				latterObject[key] = [];
				keys.push(key);
			}
			latterObject[key].push(item);
		}
	});
	keys.sort();
	keys.forEach((item, i) => {
		newObject[item] = latterObject[item];
	});

	data.list = newObject;
	data.keyList = keys;
	data.allObj = allObj;
	data.specialList = specialList;
	return data;
}

function regChar(value){
	var reg= /^[A-Za-z]+$/;
	if (reg.test(value)) {
		return true;
	}
	return false;
}