subjectPostReducer.js 3.85 KB
/**
 * # subjectPostReducer.js
 *
 * The reducer for all the actions from the various log states
 */
'use strict';
/**
 * ## Imports
 *
 * InitialState
 */
import InitialState from './subjectPostInitialState';
import Immutable, {List, Record} from 'immutable';

const {
    SUBJECT_CONTENT_REQUEST,
    SUBJECT_CONTENT_SUCCESS,
    SUBJECT_CONTENT_FAILURE,

    SUBJECT_COMMENTS_REQUEST,
    SUBJECT_COMMENTS_SUCCESS,
    SUBJECT_COMMENTS_FAILURE,
} = require('../../constants/actionTypes').default;

const initialState = new InitialState;

/**
 * ## guideReducer function
 * @param {Object} state - initialState
 * @param {Object} action - type and payload
 */
export default function postingReducer(state = initialState, action) {
    if (!(state instanceof InitialState)) return initialState.merge(state);

    switch (action.type) {
        case SUBJECT_CONTENT_REQUEST: {
            let nextState =  state.set('isContentFetching', true).set('contentError', null);
			return nextState;
        }
            break;
        case SUBJECT_CONTENT_SUCCESS:{

            let {
                authorInfo,
                blocks,//   contentData  order templateKey
                browse,
                createTime,
                forumCode,
                forumName,
                id,
                isForumTop,
                isHot,
                isIndexTop,
                postsTitle,
                praise,
                praiseUsers,//headIcon  nickName  uid
                shareGoods,
                publishTimeString,
                shareProductSkn,
                LZ,
            } = action.payload;
            let nextState = state.setIn(['authorInfo','headIcon'],authorInfo.headIcon)
            .setIn(['authorInfo','nickName'],authorInfo.nickName)
            .setIn(['authorInfo','uid'],authorInfo.uid)
            .setIn(['authorInfo','url'],authorInfo.url)
            .set('blocks',Immutable.fromJS(blocks))
            .set('browse',browse)
            .set('createTime',createTime)
            .set('forumCode',forumCode)
            .set('forumName',forumName)
            .set('id',id)
            .set('isForumTop',isForumTop)
            .set('isHot',isHot)
            .set('isIndexTop',isIndexTop)
            .set('postsTitle',postsTitle)
            .set('praise',praise)
            .set('praiseUsers',Immutable.fromJS(praiseUsers))
            .set('publishTimeString',publishTimeString)
            .set('shareProductSkn',shareProductSkn)
            .set('LZ',LZ)
            .set('isContentFetching', false).set('contentError', null);
            if (shareGoods) {
                nextState.setIn(['shareGoods','productName'],shareGoods.productName)
                .setIn(['shareGoods','productUrl'],shareGoods.productUrl)
                .setIn(['shareGoods','salesPrice'],shareGoods.salesPrice)
                .setIn(['shareGoods','goodsImage'],shareGoods.goodsImage)
            }
            return nextState;
        }
            break;
        case SUBJECT_CONTENT_FAILURE: {
            let nextState =  state.set('isContentFetching', false).set('contentError', action.payload);
			return nextState;
        }
            break;
        case SUBJECT_COMMENTS_SUCCESS:{
            let existedAry = state.commentList.toJS;
            let nextData = [...existedAry,...action.payload];
            let nextState = state.set('commentList',Immutable.fromJS(nextData));
            return nextState;
        }
            break;
        case SUBJECT_COMMENTS_REQUEST:{
            let nextState =  state.set('isCommentsFetching', true).set('commentsError', null);
			return nextState;
        }
            break;
        case SUBJECT_COMMENTS_FAILURE:
            let nextState =  state.set('isCommentsFetching', false).set('commentsError', action.payload);
            return nextState;
            break;
        default:
            return initialState;
            break;
    }
}