subjectPostReducer.js 4.57 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 {
    GO_TO_POST,

    SUBJECT_CONTENT_REQUEST,
    SUBJECT_CONTENT_SUCCESS,
    SUBJECT_CONTENT_FAILURE,

    SUBJECT_COMMENTS_REQUEST,
    SUBJECT_COMMENTS_SUCCESS,
    SUBJECT_COMMENTS_FAILURE,
    SUBJECT_ASSETS_SELECTED,
} = 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 GO_TO_POST: {
            return state.set('id', action.payload);
        }

        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 {lastedTime, total, list, pageSize} = action.payload;
            let tailCount = (total%pageSize)>0?1:0;
            let totalPages = total/pageSize + tailCount;

            let existedAry = state.commentList.toJS();
            let nextData = [...existedAry,...list];
            let page = state.currentPage;
            page++;
            let nextState = state.set('commentList',Immutable.fromJS(nextData))
            .set('lastedTime',lastedTime)
            .set('totalPages',totalPages)
            .set('currentPage',page)
            .set('commentCount',total);
            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;
        case SUBJECT_ASSETS_SELECTED: {
            let data = action.payload||[];
            let nextState = state.set('assets',Immutable.fromJS(data));
            return nextState;
        }
            break;
        default:
            return initialState;
            break;
    }
}