detailReducer.js 4.18 KB
'use strict';

import InitialState from './detailInitialState';
import Immutable, {Map} from 'immutable';

const {
	SET_ARTICLE_ID,

	GET_ARTICLE_REQUEST,
    GET_ARTICLE_SUCCESS,
    GET_ARTICLE_FAILURE,

    GET_AUTHOR_REQUEST,
    GET_AUTHOR_SUCCESS,
    GET_AUTHOR_FAILURE,

    GET_ARTICLE_CONTENT_REQUEST,
    GET_ARTICLE_CONTENT_SUCCESS,
    GET_ARTICLE_CONTENT_FAILURE,

    GET_BRAND_REQUEST,
    GET_BRAND_SUCCESS,
    GET_BRAND_FAILURE,

    GET_OTHER_ARTICLE_REQUEST,
    GET_OTHER_ARTICLE_SUCCESS,
    GET_OTHER_ARTICLE_FAILURE,

    GET_WEIXIN_PUBLIC_REQUEST,
    GET_WEIXIN_PUBLIC_SUCCESS,
    GET_WEIXIN_PUBLIC_FAILURE,

    PRODUCT_BY_SKNS_SUCCESS,

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

const initialState = new InitialState;

export default function detailReducer(state=initialState, action) {
    switch(action.type) {
    	case SET_ARTICLE_ID: {
    		return state.set('articleId', action.payload);
    	}

    	case GET_ARTICLE_REQUEST: {
    		return state.setIn(['article', 'isFetching'], true)
    		.setIn(['article', 'error'], null);
    	}

    	case GET_ARTICLE_SUCCESS: {
    		return state.setIn(['article', 'isFetching'], false)
    		.setIn(['article', 'error'], null)
    		.setIn(['article', 'data'], Immutable.fromJS(action.payload));
    	}

    	case GET_ARTICLE_FAILURE: {
    		return state.setIn(['article', 'isFetching'], false)
    		.setIn(['article', 'error'], action.payload);
    	}

    	case GET_AUTHOR_REQUEST: {
    		return state.setIn(['author', 'isFetching'], true)
    		.setIn(['author', 'error'], null);
    	}

    	case GET_AUTHOR_SUCCESS: {
    		return state.setIn(['author', 'isFetching'], false)
    		.setIn(['author', 'error'], null)
    		.setIn(['author', 'data'], Immutable.fromJS(action.payload));
    	}

    	case GET_AUTHOR_FAILURE: {
    		return state.setIn(['author', 'isFetching'], false)
    		.setIn(['author', 'error'], action.payload);
    	}

    	case GET_ARTICLE_CONTENT_REQUEST: {
    		return state.setIn(['content', 'isFetching'], true)
    		.setIn(['content', 'error'], null);
    	}

    	case GET_ARTICLE_CONTENT_SUCCESS: {
    		return state.setIn(['content', 'isFetching'], false)
    		.setIn(['content', 'error'], null)
    		.setIn(['content', 'data'], Immutable.fromJS(action.payload));
    	}

    	case GET_ARTICLE_CONTENT_FAILURE: {
    		return state.setIn(['content', 'isFetching'], false)
    		.setIn(['content', 'error'], action.payload);
    	}
    	
    	case GET_BRAND_REQUEST: {
    		return state.setIn(['brand', 'isFetching'], true)
    		.setIn(['brand', 'error'], null);
    	}

    	case GET_BRAND_SUCCESS: {
    		return state.setIn(['brand', 'isFetching'], false)
    		.setIn(['brand', 'error'], null)
    		.setIn(['brand', 'data'], Immutable.fromJS(action.payload));
    	}

    	case GET_BRAND_FAILURE: {
    		return state.setIn(['brand', 'isFetching'], false)
    		.setIn(['brand', 'error'], action.payload);
    	}

    	case GET_OTHER_ARTICLE_REQUEST: {
    		return state.setIn(['otherArticle', 'isFetching'], true)
    		.setIn(['otherArticle', 'error'], null);
    	}

    	case GET_OTHER_ARTICLE_SUCCESS: {
    		return state.setIn(['otherArticle', 'isFetching'], false)
    		.setIn(['otherArticle', 'error'], null)
    		.setIn(['otherArticle', 'data'], Immutable.fromJS(action.payload));
    	}

    	case GET_OTHER_ARTICLE_FAILURE: {
    		return state.setIn(['otherArticle', 'isFetching'], false)
    		.setIn(['otherArticle', 'error'], action.payload);
    	}

    	case GET_WEIXIN_PUBLIC_REQUEST: {
    		return state.setIn(['weixin', 'isFetching'], true)
    		.setIn(['weixin', 'error'], null);
    	}

    	case GET_WEIXIN_PUBLIC_SUCCESS: {
    		return state.setIn(['weixin', 'isFetching'], false)
    		.setIn(['weixin', 'error'], null)
    		.setIn(['weixin', 'data'], Immutable.fromJS(action.payload));
    	}

    	case GET_WEIXIN_PUBLIC_FAILURE: {
    		return state.setIn(['weixin', 'isFetching'], false)
    		.setIn(['weixin', 'error'], action.payload);
    	}

    	case PRODUCT_BY_SKNS_SUCCESS: {
    		let {
    				productList,
					contentIndex
				} = action.payload;
    		return state.setIn(['content', 'data', contentIndex, 'productList'], Immutable.fromJS(productList));
    	}
    }

    return state;
}