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

    SUBJECT_DEL_COMMENT_REQUEST,
    SUBJECT_DEL_COMMENT_SUCCESS,
    SUBJECT_DEL_COMMENT_FAILURE,

    SUBJECT_SHARE_DATA_PREPARED,
    SUBJECT_CLEAN,
} = 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: {
            let currentSid = state.sid;
			let newItems = state.items;
			let newSid = currentSid;
			// componentWillUnmount未调用,导致clean操作未进行
            if (currentSid == -1) {
                newItems = List();
            }

			if (action.payload.tryToClean && currentSid > -1 && !state.cleaned) {
				newItems = newItems.pop();
				newSid = newSid - 1;
			}

            let sid = newSid + 1;
			// 获取新的初始状态item
			let item = initialState.item;
			// 设置item的value
			item = item.set('id', action.payload.id);
			// items新增item
			newItems = newItems.push(item);
			let nextState =  state.set('sid', sid)
				.set('items', newItems)
                .set('cleaned', false);
			return nextState;
        }

        case SUBJECT_CONTENT_REQUEST: {
            let sid = action.payload;
            let item = state.items.get(sid);
            item =  item.set('isContentFetching', true).set('contentError', null);
            let nextState = state.setIn(['items', sid], item);
			return nextState;
        }
            break;
        case SUBJECT_CONTENT_SUCCESS:{
            let {sid,json} = action.payload;
            let item = state.items.get(sid);
            item = postContentSuccess(item, json);
            let nextState = state.setIn(['items', sid], item);
            return nextState;
        }
            break;
        case SUBJECT_CONTENT_FAILURE: {

            let {sid,error} = action.payload;
            let item = state.items.get(sid);
            item =  item.set('isContentFetching', false).set('contentError', error);
            let nextState = state.setIn(['items', sid], item);
			return nextState;
        }
            break;
        case SUBJECT_COMMENTS_SUCCESS:{
            let {sid, json} = action.payload;
            let item = state.items.get(sid);
            let {lastedTime, list, endReached, total} = json;
            item = item.set('isCommentsFetching', false)
                .set('commentsError', null)
                .set('commentList', Immutable.fromJS(list))
                .set('lastedTime', lastedTime)
                .set('commentCount', total)
                .set('commentReachEnd', endReached);
            let nextState = state.setIn(['items', sid], item);
            return nextState;
        }
            break;
        case SUBJECT_COMMENTS_REQUEST:{

            let sid = action.payload;
            let item = state.items.get(sid);
            item =  item.set('isCommentsFetching', true).set('commentsError', null);
            let nextState = state.setIn(['items', sid], item);
			return nextState;
        }
            break;
        case SUBJECT_COMMENTS_FAILURE:
            let {sid,error} = action.payload;
            let item = state.items.get(sid);
            item =  item.set('isCommentsFetching', false).set('commentsError', error);
            let nextState = state.setIn(['items', sid], item);
            return nextState;
            break;
        case SUBJECT_ASSETS_SELECTED: {
            let {sid,assets} = action.payload;
            let data = assets||[];
            let item = state.items.get(sid);
            item = item.set('assets',Immutable.fromJS(data));
            let nextState = state.setIn(['items', sid], item);
            return nextState;
        }
            break;
        case SUBJECT_REPLY_REQUEST: {
            let sid = action.payload;
            let item = state.items.get(sid);
            item = item.set('isReplying',true).set('replyError',null);
            let nextState = state.setIn(['items', sid], item);
            return nextState;
        }
            break;
        case SUBJECT_REPLY_SUCCESS:{
            let {sid, json, newList} = action.payload;
            let item = state.items.get(sid);
            item = item.set('assets',Immutable.fromJS([]))
            .set('assetsUrlStr','')
            .set('assetFinishCount',0)
            .set('isReplying',false)
            .set('replyError',error)
            .set('commentList',Immutable.fromJS(newList));
            let nextState = state.setIn(['items', sid], item);
            return nextState;
        }
            break;
        case SUBJECT_REPLY_FAILURE: {
            let {sid,error} = action.payload;
            let item = state.items.get(sid);
            item = item.set('isReplying',false).set('replyError',action.payload);
            let nextState = state.setIn(['items', sid], item);
            return nextState;
        }
            break;
        case SUBJECT_REPLY_UPDATE: {
            let {sid, newStr,newCount} = action.payload;
            let item = state.items.get(sid);
            item = item.set('assetsUrlStr',newStr).set('assetFinishCount',newCount);
            let nextState = state.setIn(['items', sid], item);
            return nextState;
        }
        case SUBJECT_LIKE_REQUEST: {
            let {sid,json} = action.payload;
            let item = state.items.get(sid);
            let users = item.praiseUsers.toJS();
            let newUsers = [...users,{headIcon:json}];
            item = item.set('hasPraise','Y').set('praiseUsers',Immutable.fromJS(newUsers));
            let nextState = state.setIn(['items', sid], item);
            return nextState;
        }

        case SUBJECT_UNLIKE_REQUEST: {
            let {sid,json} = action.payload;
            let item = state.items.get(sid);
            let users = item.praiseUsers.toJS();
            let newUsers = [];
            for (var i = 0; i < users.length; i++) {
                let item = users[i];
                if (item.headIcon != json) {
                    newUsers.push(item);
                }
            }
            item = item.set('hasPraise','N').set('praiseUsers',Immutable.fromJS(newUsers));
            let nextState = state.setIn(['items', sid], item);
            return nextState;
        }
            break;
        case SUBJECT_DEL_COMMENT_REQUEST: {
            let {sid, newArray} = action.payload;
            let item = state.items.get(sid);
            item = item.set('commentList',Immutable.fromJS(newArray));
            let nextState = state.setIn(['items', sid], item);
            return nextState;
        }
            break;
        case SUBJECT_SHARE_DATA_PREPARED: {
            let {sid, json} = action.payload;
            let {shareUrl, shareImgUrl} = json
            let item = state.items.get(sid);
            item = item.set('shareUrl',shareUrl).set('defaultShareImage', shareImgUrl);
            let nextState = state.setIn(['items', sid], item);
            return nextState;
        }
            break;
        case SUBJECT_CLEAN:{
            let sid = action.payload;
            if (state.items.size > sid) {
                let items = state.items.pop();
                let nextState =  state.set('sid', sid - 1)
                    .set('items', items)
                    .set('cleaned', true);
                return nextState;
            }
            return state;
        }
            break;
        case SUBJECT_DO_NOTHING:
            return state;
            break;
        case GO_TO_LIKE_LIST:
            return state;
            break;
        default:
            return state;
            break;
    }

    return state;
}

function postContentSuccess(item, json) {
    let {
        authorInfo,
        blocks,//   contentData  order templateKey
        browse,
        createTime,
        forumCode,
        forumName,
        id,
        isForumTop,
        isHot,
        isIndexTop,
        postsTitle,
        praise,
        praiseUsers,//headIcon  nickName  uid
        shareGoods,
        revieweState,
        hasPraise,
        largeImages,
        LZ,
    } = json;

    item = item.set('isContentFetching', false)
    .set('contentError', null)
    .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.reverse()))
    .set('LZ',LZ)
    .set('revieweState',revieweState)
    .set('largeImages',largeImages)
    .set('hasPraise',hasPraise||'N')
    .setIn(['shareGoods','productName'],shareGoods.productName)
    .setIn(['shareGoods','productUrl'],shareGoods.productUrl)
    .setIn(['shareGoods','salesPrice'],shareGoods.salesPrice)
    .setIn(['shareGoods','goodsImage'],shareGoods.goodsImage)
    .setIn(['shareGoods','productId'],shareGoods.productId)
    .setIn(['authorInfo','headIcon'],authorInfo.headIcon)
    .setIn(['authorInfo','nickName'],authorInfo.nickName)
    .setIn(['authorInfo','uid'],authorInfo.uid);

    return item;
}