contentReducer.js 5.41 KB
'use strict';

import InitialState from './contentInitialState';
import Immutable, {Map} from 'immutable';

const {
    SET_CONTENT_LIST_ID,
    SET_CONTENT_CATEGORY_NAME,
    SET_CONTENT_TIP_FLAG,

    FETCH_CONTENT_TYPE_REQUEST,
    FETCH_CONTENT_TYPE_SUCCESS,
    FETCH_CONTENT_TYPE_FAILURE,

    FETCH_CONTENT_LIST_REQUEST,
    FETCH_CONTENT_LIST_SUCCESS,
    FETCH_CONTENT_LIST_FAILURE,

    //回复评论
    ADD_ARTICLE_COMMENT_REQUEST,
    ADD_ARTICLE_COMMENT_SUCCESS,
    ADD_ARTICLE_COMMENT_FAILURE,

    //关注用户
    UPDATE_ATTENTION_REQUEST,
    UPDATE_ATTENTION_SUCCESS,
    UPDATE_ATTENTION_FAILURE,

    //清除消息内容
    MESSAGE_CLEAR_OTHER_LIST,
    MESSAGE_CLEAR_COMMENT_LIST,

    //回复成功提示
    SHOW_REPLY_TIP_MESSAGE,
    HIDDEN_REPLY_TIP_MESSAGE,

    CLEAR_BADGE_NUM_SUCCESS,

} = require('../../constants/actionTypes').default;

const initialState = new InitialState;

function getContentListObject(isLatest) {
    return isLatest ? 'commentList' : 'contentList';
}

export default function grassReducer(state=initialState, action) {
    switch(action.type) {
			case SET_CONTENT_LIST_ID: {
    		return state.set('contentListId', action.payload);
    	}

			case SET_CONTENT_CATEGORY_NAME: {
				return state.set('contentCategoryName', action.payload);
			}

			case SET_CONTENT_TIP_FLAG: {
				return state.set('contentTipFlag', action.payload);
			}

      case FETCH_CONTENT_TYPE_REQUEST: {
        return state.setIn(['contentType', 'isFetching'], true)
      }

      case FETCH_CONTENT_TYPE_SUCCESS: {
        return state.setIn(['contentType', 'isFetching'], false)
            .setIn(['contentType', 'list'], Immutable.fromJS(action.payload))
      }

      case FETCH_CONTENT_TYPE_FAILURE: {
        return state.setIn(['contentType', 'isFetching'], false)
            .setIn(['contentType', 'error'], action.payload)
      }

      case FETCH_CONTENT_LIST_REQUEST: {
        return state.setIn([getContentListObject(action.isLatest), 'isFetching'], true)
            .setIn([getContentListObject(action.isLatest), 'isPullToRefresh'], action.isRefresh == true)
      }

      case FETCH_CONTENT_LIST_SUCCESS: {
        state = state
            .setIn([getContentListObject(action.isLatest), 'isFetching'], false)
            .setIn([getContentListObject(action.isLatest), 'list'], Immutable.fromJS((action.payload.list)))
            .setIn([getContentListObject(action.isLatest), 'currentPage'], action.payload.currentPage)
            .setIn([getContentListObject(action.isLatest), 'endReached'], action.payload.endReached)
            .setIn([getContentListObject(action.isLatest), 'isPullToRefresh'], false)
            .setIn([getContentListObject(action.isLatest), 'pageCount'], action.payload.pageCount)
            .setIn([getContentListObject(action.isLatest), 'shouldShowEmpty'], action.payload.shouldShowEmpty)

          return state;
      }

      case FETCH_CONTENT_LIST_FAILURE: {
        return state.setIn([getContentListObject(action.isLatest), 'isFetching'], false)
            .setIn([getContentListObject(action.isLatest), 'isPullToRefresh'], false)
            .setIn([getContentListObject(action.isLatest), 'error'], action.payload)
            .setIn([getContentListObject(action.isLatest), 'shouldShowEmpty'], true);
      }

      case  ADD_ARTICLE_COMMENT_REQUEST: {
            return state.setIn(['addCommentContent', 'isFetching'], true);
        }

      case ADD_ARTICLE_COMMENT_SUCCESS: {
          return state.setIn(['addCommentContent', 'isFetching'], false);
      }

      case ADD_ARTICLE_COMMENT_FAILURE: {
          return state.setIn(['addCommentContent', 'isFetching'], false)
              .setIn(['addCommentContent', 'error'], action.payload);
      }

      case  UPDATE_ATTENTION_REQUEST: {
            return state.setIn(['updateAttentionContent', 'isFetching'], true);
        }

      case UPDATE_ATTENTION_SUCCESS: {
          state = state.setIn(['updateAttentionContent', 'isFetching'], false);

          let list = state.contentList.get('list').toJS();
          return state.setIn(['contentList', 'list'], Immutable.fromJS(list.map((item)=> {
              if (item.uid == action.optUid) {
                  return Object.assign({}, item, {
                      isMutualAttention: !item.isMutualAttention
                  })
              }
              return item;
          })))
      }

      case UPDATE_ATTENTION_FAILURE: {
          return state.setIn(['updateAttentionContent', 'isFetching'], false)
              .setIn(['updateAttentionContent', 'error'], action.payload);
      }

      case MESSAGE_CLEAR_OTHER_LIST: {
        return state.set('contentList', initialState.get('contentList'))
      }

      case MESSAGE_CLEAR_COMMENT_LIST: {
        return state.set('commentList', initialState.get('commentList'))
      }

      case SHOW_REPLY_TIP_MESSAGE: {
          return state.set('tipMessage', Immutable.fromJS(action.payload));
      }

      case HIDDEN_REPLY_TIP_MESSAGE: {
          return state.set('tipMessage', '');
      }

      case CLEAR_BADGE_NUM_SUCCESS: {
            let typeList = state.contentType.get('list').toJS();
            return state.setIn(['contentType', 'list'], Immutable.fromJS(typeList.map((item)=> {
                if (item.type == action.payload) {
                    return Object.assign({}, item, {
                        unReadCount: 0
                    })
                }
                return item;
            })));
      }

    }
    return state;
  }