subjectPostReducer.js 10.2 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,
} = 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 sid = state.sid + 1;
			// 获取新的初始状态item
			let item = initialState.item;
			// 设置item的value
			item = item.set('id', action.payload);
			// items新增item
			let newItems = state.items.push(item);
			let nextState =  state.set('sid', sid)
				.set('items', newItems);
			return nextState;
            // return state.set('id', action.payload);
        }

        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 {
                authorInfo,
                blocks,//   contentData  order templateKey
                browse,
                createTime,
                forumCode,
                forumName,
                id,
                isForumTop,
                isHot,
                isIndexTop,
                postsTitle,
                praise,
                praiseUsers,//headIcon  nickName  uid
                shareGoods,
                publishTimeString,
                shareProductSkn,
                revieweState,
                hasPraise,
                largeImages,
                LZ,
            } = json;
            let item = state.items.get(sid);
            item = item.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.reverse()))
            .set('publishTimeString',publishTimeString)
            .set('shareProductSkn',shareProductSkn)
            .set('LZ',LZ)
            .set('isContentFetching', false)
            .set('contentError', null)
            .set('revieweState',revieweState)
            .set('largeImages',largeImages)
            .set('hasPraise',hasPraise||'N');
            if (shareGoods) {
                let imgUrl = '';
                if (shareGoods.goodsImage.length) {
                    let ary=shareGoods.goodsImage.split('?');
                    imgUrl = ary[0];
                }
                item = item.setIn(['shareGoods','productName'],shareGoods.productName||'')
                .setIn(['shareGoods','productUrl'],shareGoods.productUrl||'')
                .setIn(['shareGoods','salesPrice'],shareGoods.salesPrice||'')
                .setIn(['shareGoods','goodsImage'],imgUrl||'')
                .setIn(['shareGoods','productId'],shareGoods.productId)
            }
            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, total, list, pageSize} = json;
            let tailCount = (total%pageSize)>0?1:0;
            let totalPages = total/pageSize + tailCount;

            let existedAry = item.commentList.toJS();
            let nextData = [...existedAry,...list];
            let page = item.currentPage;
            page++;
            let endReached = list.length<10;
            item = item.set('commentList',Immutable.fromJS(nextData))
            .set('lastedTime',lastedTime)
            .set('totalPages',totalPages)
            .set('currentPage',page)
            .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_DO_NOTHING:
            return state;
            break;
        case GO_TO_LIKE_LIST:
            return state;
            break;
        default:
            return state;
            break;
    }
}