contentReducer.js 6.57 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,

    //main评论内容
    FETCH_COMMENT_LIST_REQUEST,
    FETCH_COMMENT_LIST_SUCCESS,
    FETCH_COMMENT_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,
    UPDATE_TIP_FLAG_SUCCESS,

    SHOW_TOAST,
	  HIDE_TOAST,

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

const initialState = new InitialState;

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_COMMENT_LIST_REQUEST: {
        return state.setIn(['commentList', 'isFetching'], true)
            .setIn(['commentList', 'isPullToRefresh'], action.isRefresh == true)
      }

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

          return state;
      }

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

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

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

          return state;
      }

      case FETCH_CONTENT_LIST_FAILURE: {
        return state.setIn(['contentList', 'isFetching'], false)
            .setIn(['contentList', 'isPullToRefresh'], false)
            .setIn(['contentList', 'error'], action.payload)
            .setIn(['contentList', '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;
            })));
      }

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

      case SHOW_TOAST:
  			  return state.set('isShowToast', true).set('toastMessage', action.payload)
  		case HIDE_TOAST:
  			  return state.set('isShowToast', false).set('toastMessage', '')

    }
    return state;
  }