homeReducer.js 3.69 KB
/**
 * # guideReducer.js
 *
 * The reducer for all the actions from the various log states
 */
'use strict';
/**
 * ## Imports
 *
 * InitialState
 */
import InitialState from './homeInitialState';
import Immutable, {Map} from 'immutable';

const {

	HOME_BNS_REQUEST,
    HOME_BNS_SUCCESS,
    HOME_BNS_FAILURE,

	HOME_RECOMMENDATION_REQUEST,
    HOME_RECOMMENDATION_SUCCESS,
    HOME_RECOMMENDATION_FAILURE,

	POST_LIKE_REQUEST,
    POST_UNLIKE_REQUEST,

	END_REFRESHING,
	INCREASE_ERROR_COUNT,
    RESET_ERROR_COUNT,

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

const initialState = new InitialState;

/**
 * ## guideReducer function
 * @param {Object} state - initialState
 * @param {Object} action - type and payload
 */
export default function homeReducer(state = initialState, action) {
    if (!(state instanceof InitialState)) return initialState.merge(state);

    switch (action.type) {
		case HOME_BNS_REQUEST:
			return state.set('isFetching', true)
				.set('error', null)
				.set('ptr', true);

		case HOME_BNS_SUCCESS: {
			let {banner, notice, section} = action.payload;
			let nextState =  state.set('isFetching', false)
				.set('error', false)
				.set('ptr', false)
				.set('section', Immutable.fromJS(section))
				.setIn(['banner', 'duration'], banner.duration)
				.setIn(['banner', 'list'], Immutable.fromJS(banner.list))
				.setIn(['notice', 'duration'], notice.duration)
				.setIn(['notice', 'open'], notice.open)
				.setIn(['notice', 'list'], Immutable.fromJS(notice.list));
			return nextState;
		}

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

		case HOME_RECOMMENDATION_REQUEST: {
			let nextState =  state.setIn(['recommendation', 'isFetching'], true)
				.setIn(['recommendation', 'error'], null);
			return nextState;
		}

		case HOME_RECOMMENDATION_SUCCESS: {
			let {lastedTime, list, endReached} = action.payload;
			let nextState =  state.setIn(['recommendation', 'isFetching'], false)
				.setIn(['recommendation', 'error'], null)
				.setIn(['recommendation', 'lastedTime'], lastedTime)
				.setIn(['recommendation', 'endReached'], endReached)
				.setIn(['recommendation', 'list'], Immutable.fromJS(list));
			return nextState;
		}

		case HOME_RECOMMENDATION_FAILURE:
	    	return state.setIn(['recommendation', 'isFetching'], false)
				.setIn(['recommendation', 'error'], action.payload);

		case END_REFRESHING:
			return state.setIn(['recommendation', 'endRefreshing'], true);

		case INCREASE_ERROR_COUNT: {
			let count = state.recommendation.errorCount + 1;
			let time = state.recommendation.errorCount == 0 ? (new Date()).getTime() : state.recommendation.firstErrorTime;
			let nextState =  state.setIn(['recommendation', 'errorCount'], count)
				.setIn(['recommendation', 'firstErrorTime'], time);
			return nextState;
		}

		case RESET_ERROR_COUNT:
			return state.setIn(['recommendation', 'errorCount'], 0)
				.setIn(['recommendation', 'firstErrorTime'], 0);

		case POST_LIKE_REQUEST: {
			return newLikeState(state, action.payload, true);
		}

		case POST_UNLIKE_REQUEST: {
			return newLikeState(state, action.payload, false);
		}
    }

    return state;
}

function newLikeState(state, postId, like) {
	let list = state.recommendation.list;
	let index = list.findIndex((item) => {
		return item.get('id') == postId;
	})
	if (index == -1) {
		return state;
	}

	let likeCount = 0;
	if (like) {
		likeCount = list.get(index).get('likeCount') + 1;
	} else {
		likeCount = list.get(index).get('likeCount') - 1;
		likeCount = likeCount < 0 ? 0 : likeCount;
	}

	let nextState =  state.setIn(['recommendation', 'list', index, 'isLike'], like)
		.setIn(['recommendation', 'list', index, 'likeCount'], likeCount);
	return nextState;
}