categoryActions.js 6.69 KB
'use strict';

import ReactNative from 'react-native';
import CategoryService from '../../services/CategoryService';
import Immutable, {Map} from 'immutable';
const {
	SET_TYPE,

	GET_CATEGORY_REQUEST,
	GET_CATEGORY_SUCCESS,
	GET_CATEGORY_FAILURE,

	SET_CURRENT_CATEGORY_ID,
	SET_CURRENT_CHANNEL_ID,

	JUMP_TO_CATEGORY,

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

export function getCategory() {
	return (dispatch, getState) => {
		let {app, classify} = getState();
		dispatch(getCategoryRequest());
		return new CategoryService(app.host).getCategoryList('0')
        .then(json => {
        		let payload=parseCategoryList(json);
				dispatch(getCategorySuccess(payload));

				dispatch(dataExposure(payload));
		})
		.catch(error => {
				dispatch(getCategoryFailure(error));
		});
	};
}

function parseCategoryList(json) {
	if(!json){
		return;
	}
	let boy = json && json.boy ? json.boy : [];
	let girl = json && json.girl ? json.girl : [];
	let kids = json && json.kids ? json.kids : [];
	let lifestyle = json && json.lifestyle ? json.lifestyle : [];

	let newBoy=[];
	let newGirl=[];
	let newKids=[];
	let newLifestyle=[];

	//男
	// for (let i in boy)
	boy.map((item, i) => {
		// let item=boy[i];
		let newBoySub=[];
		let all={
                    category_name: '全部'+item.category_name,
                    parent_id: item.category_id,
                    // parent_id: item.parent_id,
                    relation_parameter:item.relation_parameter,
                    node_count:item.node_count,
                };
		if(item.sub){
			newBoySub=[all,...item.sub];
		}else{
			newBoySub.push(all);
		}

		newBoy.push({
			sub:newBoySub,
			category_name:item.category_name,
			category_id:item.category_id,
			parent_id:item.parent_id,
			relation_parameter:item.relation_parameter,
            sort_ico:item.sort_ico,
            node_count:item.node_count,
		});
	});
	//女
	girl.map((item, i) => {
		let newGirlSub=[];
		let all={
                    category_name: '全部'+item.category_name,
                    parent_id: item.category_id,
                    // parent_id: item.parent_id,
                    relation_parameter:item.relation_parameter,
                    node_count:item.node_count,
                };

		if(item.sub){
			newGirlSub=[all,...item.sub];
		}else{
			newGirlSub.push(all);
		}

		newGirl.push({
			sub:newGirlSub,
			category_name:item.category_name,
			category_id:item.category_id,
			parent_id:item.parent_id,
			relation_parameter:item.relation_parameter,
            sort_ico:item.sort_ico,
            node_count:item.node_count,
		});
	});

	//kid
	kids.map((item, i) => {
		let newKidsSub=[];
		let all={
                    category_name: '全部'+item.category_name,
                    parent_id: item.category_id,
                    // parent_id: item.parent_id,
                    relation_parameter:item.relation_parameter,
                    node_count:item.node_count,
                };

		if(item.sub){
			newKidsSub=[all,...item.sub];
		}else{
			newKidsSub.push(all);
		}

		newKids.push({
			sub:newKidsSub,
			category_name:item.category_name,
			category_id:item.category_id,
			parent_id:item.parent_id,
			relation_parameter:item.relation_parameter,
            sort_ico:item.sort_ico,
            node_count:item.node_count,
		});
	});

	//家居
	lifestyle.map((item, i) => {
		let newLifestyleSub=[];
		let all={
                    category_name: '全部'+item.category_name,
                    parent_id: item.category_id,
                    // parent_id: item.parent_id,
                    relation_parameter:item.relation_parameter,
                    node_count:item.node_count,
                };
		if(item.sub){
			newLifestyleSub=[all,...item.sub];

		}else{
			newLifestyleSub.push(all);
		}

		newLifestyle.push({
			sub:newLifestyleSub,
			category_name:item.category_name,
			category_id:item.category_id,
			parent_id:item.parent_id,
			relation_parameter:item.relation_parameter,
            sort_ico:item.sort_ico,
            node_count:item.node_count,
		});
	});

	return {
		boy:newBoy,
		girl:newGirl,
		kids:newKids,
		lifestyle:newLifestyle,
	};
}

export function getCategoryRequest() {
    return {
        type: GET_CATEGORY_REQUEST,
    };
}

export function getCategorySuccess(json) {
    return {
        type: GET_CATEGORY_SUCCESS,
        payload: json
    }
}

export function getCategoryFailure(error) {
    return {
        type: GET_CATEGORY_FAILURE,
        payload: error
    }
}

export function setCurrentCateId(rowID){
	return {
        type: SET_CURRENT_CATEGORY_ID,
        payload: rowID
    }
}

export function setCurrentChannelId(channelID){
	return {
        type: SET_CURRENT_CHANNEL_ID,
        payload: channelID
    }
}


export function jumpToCategory(value, index, channel){
	let channelId=1;
	if(channel==='boy'){
		channelId=1;
	}else if(channel==='girl'){
		channelId=2;
	}else if(channel==='kids'){
		channelId=3;
	}else{
		channelId=4;
	}
	console.log(value);
	ReactNative.NativeModules.YH_CommonHelper.jumpToCategory(value, parseInt(index), parseInt(channelId));
	return {
        type: JUMP_TO_CATEGORY,
        payload: value
    }
}

function dataExposure(json) {
	return (dispatch, getState) => {
		if (!json) {
			return;
		}

		let {boy, girl, kids, lifestyle} = json;

		let newBoy = [];
		let newGirl = [];
		let newKids = [];
		let newLifestyle = [];

		boy.map((item, i) => {
			let subList = [];
			item.sub && item.sub.map((sub, k) => {
				subList.push({
					I_INDEX: k + 1,
					CAT_ID: sub.category_id,
				});
			});

			newBoy.push({
				I_INDEX: i + 1,
				CAT_ID: item.category_id,
				SUBLIST: subList,
			});
		});

		girl.map((item, i) => {
			let subList = [];
			item.sub && item.sub.map((sub, k) => {
				subList.push({
					I_INDEX: k + 1,
					CAT_ID: sub.category_id,
				});
			});

			newGirl.push({
				I_INDEX: i + 1,
				CAT_ID: item.category_id,
				SUBLIST: subList,
			});
		});

		kids.map((item, i) => {
			let subList = [];
			item.sub && item.sub.map((sub, k) => {
				subList.push({
					I_INDEX: k + 1,
					CAT_ID: sub.category_id,
				});
			});

			newKids.push({
				I_INDEX: i + 1,
				CAT_ID: item.category_id,
				SUBLIST: subList,
			});
		});

		lifestyle.map((item, i) => {
			let subList = [];
			item.sub && item.sub.map((sub, k) => {
				subList.push({
					I_INDEX: k + 1,
					CAT_ID: sub.category_id,
				});
			});

			newLifestyle.push({
				I_INDEX: i + 1,
				CAT_ID: item.category_id,
				SUBLIST: subList,
			});
		});


		let params = {
			YH_AppChannelBoy: newBoy,
			YH_AppChannelGirl: newGirl,
			YH_AppChannelKid: newKids,
			YH_AppChannelLifeStyle: newLifestyle,
		};

		// console.log('YB_SHOW_CATEGORY')
		// console.log(params)

		ReactNative.NativeModules.YH_CommonHelper.logEvent('YB_SHOW_CATEGORY', params);
	};
}