detailReducer.js 9.92 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,

	GET_WECHAT_ATTEN_REQUEST,
	GET_WECHAT_ATTEN_SUCCESS,
	GET_WECHAT_ATTEN_FAILURE,

    PRODUCT_BY_SKNS_SUCCESS,

	SET_GOOESGROUP_FILTER,
	SET_GOOESGROUP_Y,


	//获取资讯评论列表
	GET_COMMENTS_LIST_REQUEST,
	GET_COMMENTS_LIST_SUCCESS,
	GET_COMMENTS_LIST_FAILURE,

	//资讯添加评论
	ADD_COMMENTS_FOR_ARTIVLE_REQUEST,
	ADD_COMMENTS_FOR_ARTIVLE_SUCCESS,
	ADD_COMMENTS_FOR_ARTIVLE_FAILURE,

	//资讯内容点赞
	ADD_PRAISE_FOR_COMMENTS_REQUEST,
	ADD_PRAISE_FOR_COMMENTS_SUCCESS,
	ADD_PRAISE_FOR_COMMENTS_FAILURE,

	//文章点赞Info
	GET_PRAISE_FOR_ARTIVLE_REQUEST,
	GET_PRAISE_FOR_ARTIVLE_SUCCESS,
	GET_PRAISE_FOR_ARTIVLE_FAILURE,

	SET_PRAISE_FOR_COMMENTS,

	//点赞文章
	PRAISE_FOR_ARTIVLE_REQUEST,
	PRAISE_FOR_ARTIVLE_SUCCESS,
	PRAISE_FOR_ARTIVLE_FAILURE,
	//取消点赞文章
	CANCEL_PRAISE_FOR_ARTIVLE_REQUEST,
	CANCEL_PRAISE_FOR_ARTIVLE_SUCCESS,
	CANCEL_PRAISE_FOR_ARTIVLE_FAILURE,
	//收藏
	ADD_FAVORITE_REQUEST,
	ADD_FAVORITE_SUCCESS,
	ADD_FAVORITE_FAILURE,
	//取消收藏
	CANCEL_FAVORITE_REQUEST,
	CANCEL_FAVORITE_SUCCESS,
	CANCEL_FAVORITE_FAILURE,

	RESET_COMMENTS_LIST_PAGE_INFO,

} = 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 GET_WECHAT_ATTEN_REQUEST: {
    		return state.setIn(['wechat', 'isFetching'], true)
    		.setIn(['wechat', 'error'], null);
    	}

    	case GET_WECHAT_ATTEN_SUCCESS: {
    		return state.setIn(['wechat', 'isFetching'], false)
    		.setIn(['wechat', 'error'], null)
    		.setIn(['wechat', 'wechatAtten'], Immutable.fromJS(action.payload.wechatAtten))
			.setIn(['wechat', 'wechatAttenSub'], Immutable.fromJS(action.payload.wechatAttenSub))
			.setIn(['wechat', 'wechatCopy'], Immutable.fromJS(action.payload.wechatCopy));
    	}

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

    	case PRODUCT_BY_SKNS_SUCCESS: {
    		let {
    				productList,
					contentIndex
				} = action.payload;
    		return state.setIn(['content', 'data', contentIndex, 'productList'], Immutable.fromJS(productList));
    	}
		case SET_GOOESGROUP_FILTER: {
			return state.set('goods_group_Filter', action.payload);
    	}
		case SET_GOOESGROUP_Y: {
			return state.set('goods_group_y', action.payload);
    	}
		//获取资讯评论列表
		case GET_COMMENTS_LIST_REQUEST: {
			return state.setIn(['commentsList', 'isFetching'], true)
    		.setIn(['commentsList', 'error'], null);
    	}
		case GET_COMMENTS_LIST_SUCCESS: {
			return state.setIn(['commentsList', 'isFetching'], false)
			.setIn(['commentsList', 'error'], null)
			.setIn(['commentsList', 'page'], action.payload.page)
			.setIn(['commentsList', 'total'], action.payload.total)
			.setIn(['commentsList', 'total_page'], action.payload.total_page)
			.setIn(['commentsList', 'endReached'], action.payload.endReached)
			.setIn(['commentsList', 'data'], Immutable.fromJS(action.payload.data));
    	}
		case GET_COMMENTS_LIST_FAILURE: {
			return state.setIn(['commentsList', 'isFetching'], false)
			.setIn(['commentsList', 'error'], action.payload);
    	}
		case RESET_COMMENTS_LIST_PAGE_INFO: {
    		return state.setIn(['commentsList', 'page'], 0)
				.setIn(['commentsList', 'total_page'], 0)
				.setIn(['commentsList', 'total'], 0)
				.setIn(['commentsList', 'isFetching'], false)
				.setIn(['commentsList', 'error'], null)
				.setIn(['commentsList', 'endReached'], false);
    	}
		//资讯添加评论
		case ADD_COMMENTS_FOR_ARTIVLE_REQUEST: {
			return state.setIn(['addComment', 'isFetching'], true)
			.setIn(['addComment', 'message'], '')
			.setIn(['addComment', 'error'], null);
		}
		case ADD_COMMENTS_FOR_ARTIVLE_SUCCESS: {
			return state.setIn(['addComment', 'isFetching'], false)
			.setIn(['addComment', 'error'], null)
			.setIn(['addComment', 'data'], Immutable.fromJS(action.payload)
			.setIn(['addComment', 'message'], '发表评论成功'));
		}
		case ADD_COMMENTS_FOR_ARTIVLE_FAILURE: {
			return state.setIn(['addComment', 'isFetching'], false)
			.setIn(['addComment', 'message'], '发表评论失败')
			.setIn(['addComment', 'error'], action.payload);
		}
		case SET_PRAISE_FOR_COMMENTS: {
			return state.setIn(['commentsList', 'data'], Immutable.fromJS(action.payload));
		}
		//资讯内容点赞
		case ADD_PRAISE_FOR_COMMENTS_REQUEST: {
			break;
		}

		case ADD_PRAISE_FOR_COMMENTS_SUCCESS: {
			break;
		}
		case ADD_PRAISE_FOR_COMMENTS_FAILURE: {
			break;
		}
		//文章点赞Info
		case GET_PRAISE_FOR_ARTIVLE_REQUEST: {
			return state.setIn(['addPraiseInfo', 'isFetching'], true)
			.setIn(['addPraiseInfo', 'error'], null);
		}
		case GET_PRAISE_FOR_ARTIVLE_SUCCESS: {
			return state.setIn(['addPraiseInfo', 'isFetching'], false)
			.setIn(['addPraiseInfo', 'browseNum'], action.payload.browseNum)
			.setIn(['addPraiseInfo', 'id'], action.payload.id)
			.setIn(['addPraiseInfo', 'isFavor'], action.payload.isFavor)
			.setIn(['addPraiseInfo', 'isPraise'], action.payload.isPraise)
			.setIn(['addPraiseInfo', 'praiseHeadIco'], Immutable.fromJS(action.payload.praiseHeadIco))
			.setIn(['addPraiseInfo', 'praiseNum'], action.payload.praiseNum)
			.setIn(['addPraiseInfo', 'error'], null);
		}
		case GET_PRAISE_FOR_ARTIVLE_FAILURE: {
			return state.setIn(['addPraiseInfo', 'isFetching'], false)
			.setIn(['addPraiseInfo', 'error'], action.payload);
		}
		case PRAISE_FOR_ARTIVLE_REQUEST:
		{
			return state.setIn(['addPraiseInfo', 'isPraise'], 'Y').setIn(['addPraiseInfo', 'praiseNum'], action.payload + 1);
		}
		case PRAISE_FOR_ARTIVLE_SUCCESS:{break;}
		case PRAISE_FOR_ARTIVLE_FAILURE:{break;}
		case CANCEL_PRAISE_FOR_ARTIVLE_REQUEST:
		{
			return state.setIn(['addPraiseInfo', 'isPraise'], 'N').setIn(['addPraiseInfo', 'praiseNum'], action.payload - 1);
		}
		case CANCEL_PRAISE_FOR_ARTIVLE_SUCCESS:{break;}
		case CANCEL_PRAISE_FOR_ARTIVLE_FAILURE:{break;}
		case ADD_FAVORITE_REQUEST:
		{
			return state.setIn(['addPraiseInfo', 'isFavor'], 'Y');
		}
		case ADD_FAVORITE_SUCCESS:{break;}
		case ADD_FAVORITE_FAILURE:{break;}
		case CANCEL_FAVORITE_REQUEST:
		{
			return state.setIn(['addPraiseInfo', 'isFavor'], 'N');
		}
		case CANCEL_FAVORITE_SUCCESS:{break;}
		case CANCEL_FAVORITE_FAILURE:{break;}
    }

    return state;
}