postingReducer.js 2.85 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,
} = 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_TITLE_EDITED: {
            let dataValid=false;
            if (action.payload.length) {
                dataValid = state.currentBoardName.length>0;
            }
            let nextState = state.set('title',action.payload).set('dataValid',dataValid);
            return nextState;
        }

            break;
        case POSTING_CONTENT_EDITED: {
            let dataValid=false;
            if (action.payload.length) {
                dataValid = state.currentBoardName.length>0;
            }
            let nextState = state.set('content',action.payload).set('dataValid',dataValid);
            return nextState;
        }

            break;
        case POSTING_BOARD_REQUEST: {
			let nextState =  state.set('isFetching', true).set('error', null);
			return nextState;
		}
            break;
        case POSTING_BOARD_SUCCESS: {
            let boards = action.payload||[];
            let nextState = state.set('isFetching',false)
            .set('error', null)
            .set('boards', Immutable.fromJS(boards));
            return nextState;
        }
            break;
        case POSTING_BOARD_FAILURE: {
            let nextState = state.set('isFetching', false).set('error', action.payload);
            return nextState;
        }

            break;
        case POSTING_BOARD_SELECTED: {
            let dataValid=state.title.length>0||state.content.length>0;

            let nextState = state.set('currentBoardName',action.payload.boardName).set('currentBoardId',action.payload.boardCode).set('dataValid',dataValid);
            return nextState;
        }

            break;
        case POSTING_ASSETS_SELECTED: {
            let data = action.payload||[];
            let dataValid=false;
            if (data.length) {
                dataValid = state.currentBoardName.length>0;
            }
            let nextState = state.set('assets',Immutable.fromJS(data)).set('dataValid',dataValid);
            return nextState;
        }

            break;
        default:

        break;
    }

    return state;
}