postingReducer.js 6.21 KB
/**
 * # postingReducer.js
 *
 * The reducer for all the actions from the various log states
 */
'use strict';
/**
 * ## Imports
 *
 * InitialState
 */
import InitialState from './postingInitialState';
import Immutable, {List, Record} from 'immutable';

const {
    POSTING_BOARD_REQUEST,
    POSTING_BOARD_SUCCESS,
    POSTING_BOARD_FAILURE,

    POSTING_BOARD_SELECTED,
    POSTING_ASSETS_SELECTED,
    POSTING_TITLE_EDITED,
    POSTING_CONTENT_EDITED,

    POSTING_POST_START,
    POSTING_POST_SUCCESS,
    POSTING_POST_FAILURE,

    POSTING_IMAGE_UPLOAD_SUCCESS,
    POSTING_POST_FINISHED,
    POSTING_STATE_RESET,
    POSTING_STATE_MERGE,
    POSTING_EVENT_LOGOUT,

    USER_FORBID_STATE_REQUEST,
    USER_FORBID_STATE_SUCCESS,
    USER_FORBID_STATE_FAILURE,
} = 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 POSTING_STATE_MERGE: {
            let {canSubmit, userState, post, postState} = action.payload;
            let nextState = state.set('canSubmit', canSubmit)
                .setIn(['userState', 'forbidNickname'], userState.forbidNickname)
                .setIn(['userState', 'forbidSpeaking'], userState.forbidSpeaking)
                .setIn(['post', 'title'], post.title)
                .setIn(['post', 'content'], post.content)
                .setIn(['post', 'sectionId'], post.sectionId)
                .setIn(['post', 'sectionName'], post.sectionName)
                .setIn(['post', 'assets'], Immutable.fromJS(post.assets))
                .setIn(['postState', 'isPosting'], postState.isPosting)
                .setIn(['postState', 'error'], postState.error)
                .setIn(['postState', 'showProcess'], postState.showProcess)
                .setIn(['postState', 'status'], postState.status)
                .setIn(['postState', 'stage'], postState.stage)
                .setIn(['postState', 'uploadedImageCount'], postState.uploadedImageCount)
                .setIn(['postState', 'uploadedImagePercent'], postState.uploadedImagePercent);
            return nextState;
        }

        case POSTING_EVENT_LOGOUT:
        case POSTING_STATE_RESET: {
            return initialState;
        }

        case POSTING_TITLE_EDITED: {
            let {title, canSubmit} = action.payload;
            let nextState = state.set('canSubmit', canSubmit)
                .setIn(['post', 'title'], title);
            return nextState;
        }

        case POSTING_CONTENT_EDITED: {
            let {content, canSubmit} = action.payload;
            let nextState = state.set('canSubmit', canSubmit)
                .setIn(['post', 'content'], content);
            return nextState;
        }

        case POSTING_BOARD_SELECTED: {
            let {sectionName, sectionId, canSubmit} = action.payload;
            let nextState = state.set('canSubmit', canSubmit)
                .setIn(['post', 'sectionName'], sectionName)
                .setIn(['post', 'sectionId'], sectionId);
            return nextState;
        }

        case POSTING_ASSETS_SELECTED: {
            let assets = action.payload || [];
            let nextState = state.setIn(['post', 'assets'], Immutable.fromJS(assets));
            return nextState;
        }

        case POSTING_POST_START: {
            let nextState = state.setIn(['postState', 'isPosting'], true)
                .setIn(['postState', 'error'], null)
                .setIn(['postState', 'showProcess'], true)
                .setIn(['postState', 'status'], 1);
            return nextState;
        }

        case POSTING_POST_SUCCESS: {
            let nextState = state.setIn(['postState', 'isPosting'], false)
                .setIn(['postState', 'error'], null)
                .setIn(['postState', 'status'], 2);
            return nextState;
        }

        case POSTING_IMAGE_UPLOAD_SUCCESS: {
            let {assets, uploadedImageCount, uploadedImagePercent} = action.payload;
            let nextState = state.setIn(['post', 'assets'], Immutable.fromJS(assets))
                .setIn(['postState', 'uploadedImageCount'], uploadedImageCount)
                .setIn(['postState', 'uploadedImagePercent'], uploadedImagePercent);
            return nextState;
        }

        case POSTING_POST_FAILURE: {
            let nextState = state.setIn(['postState', 'isPosting'], false)
                .setIn(['postState', 'error'], action.payload)
                .setIn(['postState', 'status'], 3);
            return nextState;
        }

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

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

        case POSTING_BOARD_FAILURE: {
            let nextState =  state.setIn(['boards', 'isFetching'], false)
                .setIn(['boards', 'error'], action.payload);
            return nextState;
        }

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

        case USER_FORBID_STATE_SUCCESS: {
            let {forbidNickname, forbidSpeaking} = action.payload;
            let nextState =  state.setIn(['userState', 'isFetching'], false)
                .setIn(['userState', 'error'], null)
                .setIn(['userState', 'forbidNickname'], forbidNickname)
                .setIn(['userState', 'forbidSpeaking'], forbidSpeaking);
			return nextState;
        }

        case USER_FORBID_STATE_FAILURE: {
            let nextState =  state.setIn(['userState', 'isFetching'], false)
                .setIn(['userState', 'error'], action.payload);
			return nextState;
        }

        default:
        break;
    }

    return state;
}