interestReducer.js 2.1 KB
'use strict';

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

const {
	SET_TYPE,

	RESET_LIST_PAGE_INFO,
	SHOW_LOGIN_TIP,

	INTEREST_LIST_REQUEST,
	INTEREST_LIST_SUCCESS,
	INTEREST_LIST_FAILURE,

	INTEREST_LIKE_STATE_CHANGE,

    INTEREST_LIKE_REQUEST,
    INTEREST_LIKE_SUCCESS,
    INTEREST_LIKE_FAILURE,
	INTEREST_LIKE_TIP_REMOVE,

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

const initialState = new InitialState;

export default function interestReducer(state=initialState, action) {
    switch(action.type) {

    	case SET_TYPE: {
			return state.set('type', action.payload);
		}

		case RESET_LIST_PAGE_INFO: {
    		return state.set('currentPage', 0)
				.set('pageCount', 0)
				.set('total', 0)
				.set('endReached', false)
				.set('addLikeTip', '');
    	}

    	case SHOW_LOGIN_TIP: {
    		return state.set('showLoginTip', action.payload);
    	}

		case INTEREST_LIST_REQUEST: {
    		return state.set('isFetching', true)
			.set('error', null)
			.set('ptr', action.payload);
    	}

    	case INTEREST_LIST_SUCCESS: {
    		let {
				list,
				currentPage,
				pageCount,
				total,
				endReached,
			} = action.payload;

			let newState = state.set('isFetching', false)
			.set('error', null)
			.set('ptr', false)
			.set('list', Immutable.fromJS(list))
			.set('currentPage', currentPage)
			.set('pageCount', pageCount)
			.set('total', total)
			.set('endReached', endReached);

			return newState;
		}

		case INTEREST_LIST_FAILURE: {
			return state.set('isFetching', false)
			.set('error', action.payload)
			.set('ptr', false);
		}

		case INTEREST_LIKE_STATE_CHANGE: {
			let {like, index} = action.payload;
			return state.setIn(['list', index, 'is_fav'], like)
			.set('addLikeTip', '收藏成功');
		}
		case INTEREST_LIKE_TIP_REMOVE: {
			return state.set('addLikeTip', '');
		}

		case INTEREST_LIKE_REQUEST:
		case INTEREST_LIKE_SUCCESS:
		case INTEREST_LIKE_FAILURE: {
			return state;
		}

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

    return state;
}