subjectPostReducer.js 6.27 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,

    SUBJECT_REPLY_REQUEST,
    SUBJECT_REPLY_SUCCESS,
    SUBJECT_REPLY_FAILURE,
    SUBJECT_REPLY_UPDATE,
    SUBJECT_DO_NOTHING,
    GO_TO_LIKE_LIST,

    SUBJECT_LIKE_SUCCESS,
    SUBJECT_UNLIKE_SUCCESS,
    SUBJECT_LIKE_REQUEST,
    SUBJECT_UNLIKE_REQUEST,
} = 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,
                revieweState,
                hasPraise,
                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',decodeURI(postsTitle))
            .set('praise',praise)
            .set('praiseUsers',Immutable.fromJS(praiseUsers))
            .set('publishTimeString',publishTimeString)
            .set('shareProductSkn',shareProductSkn)
            .set('LZ',LZ)
            .set('isContentFetching', false)
            .set('contentError', null)
            .set('revieweState',revieweState)
            .set('hasPraise',hasPraise||'N');
            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;
        case SUBJECT_REPLY_REQUEST: {
            let nextState = state.set('isReplying',true).set('replyError',null);
            return nextState;
        }
            break;
        case SUBJECT_REPLY_SUCCESS:{
            let nextState = state.set('assets',Immutable.fromJS([]))
            .set('assetsUrlStr','')
            .set('assetFinishCount',0)
            .set('isReplying',false)
            .set('replyError',error)
            return nextState;
        }
            break;
        case SUBJECT_REPLY_FAILURE: {
            let nextState = state.set('isReplying',false).set('replyError',action.payload);
            return nextState;
        }
            break;
        case SUBJECT_REPLY_UPDATE: {
            let {newStr,newCount} = action.payload;
            let nextState = state.set('assetsUrlStr',newStr).set('assetFinishCount',newCount);
            return nextState;
        }
        case SUBJECT_LIKE_REQUEST: {
            return state.set('hasPraise','Y');
        }

        case SUBJECT_UNLIKE_REQUEST: {
            return state.set('hasPraise','N');;
        }
            break;
        case SUBJECT_DO_NOTHING:
            return state;
            break;
        case GO_TO_LIKE_LIST:
            return state;
            break;
        default:
            return initialState;
            break;
    }
}