brandActions.js 7.76 KB
'use strict';

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

const {
	SET_TYPE,

	SET_BRAND_FILTER,
	SET_CHANNEL_FILTER,

	GET_BRAND_LIST_FOR_BOY_REQUEST,
	GET_BRAND_LIST_FOR_BOY_SUCCESS,
	GET_BRAND_LIST_FOR_BOY_FAILURE,

	GET_BRAND_LIST_FOR_GIRL_REQUEST,
	GET_BRAND_LIST_FOR_GIRL_SUCCESS,
	GET_BRAND_LIST_FOR_GIRL_FAILURE,

	GET_BRAND_LIST_FOR_KID_REQUEST,
	GET_BRAND_LIST_FOR_KID_SUCCESS,
	GET_BRAND_LIST_FOR_KID_FAILURE,

	GET_BRAND_LIST_FOR_LIFESTYLE_REQUEST,
	GET_BRAND_LIST_FOR_LIFESTYLE_SUCCESS,
	GET_BRAND_LIST_FOR_LIFESTYLE_FAILURE,

	GET_BRAND_RESOURCE_FOR_BOY_REQUEST,
	GET_BRAND_RESOURCE_FOR_BOY_SUCCESS,
	GET_BRAND_RESOURCE_FOR_BOY_FAILURE,

	GET_BRAND_RESOURCE_FOR_GIRL_REQUEST,
	GET_BRAND_RESOURCE_FOR_GIRL_SUCCESS,
	GET_BRAND_RESOURCE_FOR_GIRL_FAILURE,

	GET_BRAND_RESOURCE_FOR_KID_REQUEST,
	GET_BRAND_RESOURCE_FOR_KID_SUCCESS,
	GET_BRAND_RESOURCE_FOR_KID_FAILURE,

	GET_BRAND_RESOURCE_FOR_LIFESTYLE_REQUEST,
	GET_BRAND_RESOURCE_FOR_LIFESTYLE_SUCCESS,
	GET_BRAND_RESOURCE_FOR_LIFESTYLE_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: GET_BRAND_LIST_FOR_BOY_REQUEST,
    };
}

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

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

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

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

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

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

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

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

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

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

export function getBrandListForLifeStyleFailure(error) {
    return {
        type: GET_BRAND_LIST_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: GET_BRAND_RESOURCE_FOR_BOY_REQUEST,
    };
}

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

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

export function getBrandResourceForGirlRequest() {
    return {
        type: GET_BRAND_RESOURCE_FOR_GIRL_REQUEST,
    };
}

export function getBrandResourceForGirlSuccess(json) {
    return {
        type: GET_BRAND_RESOURCE_FOR_GIRL_SUCCESS,
        payload: json
    }
}

export function getBrandResourceForGirlFailure(error) {
    return {
        type: GET_BRAND_RESOURCE_FOR_GIRL_FAILURE,
        payload: error
    }
}

export function getBrandResourceForKidRequest() {
    return {
        type: GET_BRAND_RESOURCE_FOR_KID_REQUEST,
    };
}

export function getBrandResourceForKidSuccess(json) {
    return {
        type: GET_BRAND_RESOURCE_FOR_KID_SUCCESS,
        payload: json
    }
}

export function getBrandResourceForKidFailure(error) {
    return {
        type: GET_BRAND_RESOURCE_FOR_KID_FAILURE,
        payload: error
    }
}

export function getBrandResourceForLifeStyleRequest() {
    return {
        type: GET_BRAND_RESOURCE_FOR_LIFESTYLE_REQUEST,
    };
}

export function getBrandResourceForLifeStyleSuccess(json) {
    return {
        type: GET_BRAND_RESOURCE_FOR_LIFESTYLE_SUCCESS,
        payload: json
    }
}

export function getBrandResourceForLifeStyleFailure(error) {
    return {
        type: GET_BRAND_RESOURCE_FOR_LIFESTYLE_FAILURE,
        payload: error
    }
}

export function getBrandResource(channel) {
	return (dispatch, getState) => {
		let {app, classify} = getState();
		let content_code;
		if (channel == 0) {
			dispatch(getBrandResourceForBoyRequest());
			content_code = 'ce6ac059493ec26241a8cbe0bfa1b17a';
		}else if (channel == 1) {
			dispatch(getBrandResourceForGirlRequest());
			content_code = 'dac99cdedc1f948e84c145aca561f7d2';
		}else if (channel == 2) {
			dispatch(getBrandResourceForKidRequest());
			content_code = '713784f93f52bb1a7b93916b2bb15510';
		}else if (channel == 3) {
			dispatch(getBrandResourceForLifeStyleRequest());
			content_code = 'f1aa914fd23cbcda71a87de6f5416c75';
		}

		return new BrandService(app.serviceHost).getBrandResource(content_code)
        .then(json => {
			let payload = parseResourceResources(json);
			if (channel == 0) {
				dispatch(getBrandResourceForBoySuccess(payload));
			}else if (channel == 1) {
				dispatch(getBrandResourceForGirlSuccess(payload));
			}else if (channel == 2) {
				dispatch(getBrandResourceForKidSuccess(payload));
			}else if (channel == 3) {
				dispatch(getBrandResourceForLifeStyleSuccess(payload));
			}
		})
		.catch(error => {
			if (channel == 0) {
				dispatch(getBrandResourceForBoyFailure(error));
			}else if (channel == 1) {
				dispatch(getBrandResourceForGirlFailure(error));
			}else if (channel == 2) {
				dispatch(getBrandResourceForKidFailure(error));
			}else if (channel == 3) {
				dispatch(getBrandResourceForLifeStyleFailure(error));
			}
		});
	};
}

export function parseResourceResources(json) {

	let banner = [];
	let custom_brands = [];
	let brandsText = [];

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

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

    return {
		banner,
		custom_brands,
		brandsText,
    };
}