brandActions.js 5.09 KB
'use strict';

import ReactNative from 'react-native';
import BrandService from '../../services/BrandService';

const {
	SET_TYPE,

	SET_BRAND_FILTER,
	SET_CHANNEL_FILTER,

	GETBRANDLIST_FOR_BOY_REQUEST,
	GETBRANDLIST_FOR_BOY_SUCCESS,
	GETBRANDLIST_FOR_BOY_FAILURE,

	GETBRANDLIST_FOR_GIRL_REQUEST,
	GETBRANDLIST_FOR_GIRL_SUCCESS,
	GETBRANDLIST_FOR_GIRL_FAILURE,

	GETBRANDLIST_FOR_KID_REQUEST,
	GETBRANDLIST_FOR_KID_SUCCESS,
	GETBRANDLIST_FOR_KID_FAILURE,

	GETBRANDLIST_FOR_LIFESTYLE_REQUEST,
	GETBRANDLIST_FOR_LIFESTYLE_SUCCESS,
	GETBRANDLIST_FOR_LIFESTYLE_FAILURE,

	GETBRANDRESOURCE_FOR_BOY_REQUEST,
	GETBRANDRESOURCE_FOR_BOY_SUCCESS,
	GETBRANDRESOURCE_FOR_BOY_FAILURE,

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


export function setBrandFilter(filter) {
	return {
		type: SET_BRAND_FILTER,
		payload: filter
	};
}

export function setChannelFilter(filter) {
	return {
		type: SET_CHANNEL_FILTER,
		payload: filter
	};
}

export function getBrandListForBoyRequest() {
    return {
        type: GETBRANDLIST_FOR_BOY_REQUEST,
    };
}

export function getBrandListForBoySuccess(json) {
    return {
        type: GETBRANDLIST_FOR_BOY_SUCCESS,
        payload: json
    }
}

export function getBrandListForBoyFailure(error) {
    return {
        type: GETBRANDLIST_FOR_BOY_FAILURE,
        payload: error
    }
}

export function getBrandListForGirlRequest() {
    return {
        type: GETBRANDLIST_FOR_GIRL_REQUEST,
    };
}

export function getBrandListForGirlSuccess(json) {
    return {
        type: GETBRANDLIST_FOR_GIRL_SUCCESS,
        payload: json
    }
}

export function getBrandListForGirlFailure(error) {
    return {
        type: GETBRANDLIST_FOR_GIRL_FAILURE,
        payload: error
    }
}

export function getBrandListForKidRequest() {
    return {
        type: GETBRANDLIST_FOR_KID_REQUEST,
    };
}

export function getBrandListForKidSuccess(json) {
    return {
        type: GETBRANDLIST_FOR_KID_SUCCESS,
        payload: json
    }
}

export function getBrandListForKidFailure(error) {
    return {
        type: GETBRANDLIST_FOR_KID_FAILURE,
        payload: error
    }
}

export function getBrandListForLifeStyleRequest() {
    return {
        type: GETBRANDLIST_FOR_LIFESTYLE_REQUEST,
    };
}

export function getBrandListForLifeStyleSuccess(json) {
    return {
        type: GETBRANDLIST_FOR_LIFESTYLE_SUCCESS,
        payload: json
    }
}

export function getBrandListForLifeStyleFailure(error) {
    return {
        type: GETBRANDLIST_FOR_LIFESTYLE_FAILURE,
        payload: error
    }
}

export function getBrandList(channel) {
	return (dispatch, getState) => {
		let {app, classify} = getState();
		if (channel == 0) {
			dispatch(getBrandListForBoyRequest());
		}else if (channel == 1) {
			dispatch(getBrandListForGirlRequest());
		}else if (channel == 2) {
			dispatch(getBrandListForKidRequest());
		}else if (channel == 3) {
			dispatch(getBrandListForLifeStyleRequest());
		}
		return new BrandService(app.host).getBrandList(channel)
        .then(json => {
			if (channel == 0) {
				dispatch(getBrandListForBoySuccess(json));
			}else if (channel == 1) {
				dispatch(getBrandListForGirlSuccess(json));
			}else if (channel == 2) {
				dispatch(getBrandListForKidSuccess(json));
			}else if (channel == 3) {
				dispatch(getBrandListForLifeStyleSuccess(json));
			}
		})
		.catch(error => {
			if (channel == 0) {
				dispatch(getBrandListForBoyFailure(error));
			}else if (channel == 1) {
				dispatch(getBrandListForGirlFailure(error));
			}else if (channel == 2) {
				dispatch(getBrandListForKidFailure(error));
			}else if (channel == 3) {
				dispatch(getBrandListForLifeStyleFailure(error));
			}
		});
	};
}

export function getBrandResourceForBoyRequest() {
    return {
        type: GETBRANDRESOURCE_FOR_BOY_REQUEST,
    };
}

export function getBrandResourceForBoySuccess(json) {
    return {
        type: GETBRANDRESOURCE_FOR_BOY_SUCCESS,
        payload: json
    }
}

export function getBrandResourceForBoyFailure(error) {
    return {
        type: GETBRANDRESOURCE_FOR_BOY_FAILURE,
        payload: error
    }
}

export function getBrandResource(channel) {
	return (dispatch, getState) => {
		let {app, classify} = getState();
		dispatch(getBrandResourceForBoyRequest());
		return new BrandService(app.serviceHost).getBrandResource()
        .then(json => {
			let payload = parseResourceResources(json);
			dispatch(getBrandResourceForBoySuccess(payload));
		})
		.catch(error => {
			dispatch(getBrandResourceForBoyFailure(error));
		});
	};
}

export function parseResourceResources(json) {

	let bannerForBoy = [];
	let custom_brandsForBoy = [];
	let brandsTextForBoy = [];

	brandsTextForBoy.push({key: 'default',name: '全部品牌'});

	for (var i = 0; i < json.length; i++) {
		if (json[i].template_name == 'focus') {
			bannerForBoy = json[i];
		}
		if (json[i].template_name == 'custom_brands') {
			custom_brandsForBoy = json[i];
		}
		if (json[i].template_name == 'text') {
			let data = json[i].data;
			let template_id = json[i].template_id;
			let text = data.text;
			brandsTextForBoy.push({key: template_id,name: text});
		}
	}

    return {
		bannerForBoy,
		custom_brandsForBoy,
		brandsTextForBoy,
    };
}