import { SET_SPINNER, SHOW_FLASH_MESSAGE, CLEAN_ALERTS } from '../constants/common';

interface Action {
    type: string;
    showSpinner?: boolean;
    successMessage?: string;
    errorMessage?: string;
}

interface CommonReducer {
    showSpinner?: boolean;
    successMessage?: string;
    errorMessage?: string;
}
const initialState = {
    showSpinner: false,
    errorMessage: '',
    successMessage: '',
};

export const commonReducer = (state = initialState, action: Action): CommonReducer => {
    switch (action.type) {
        case SET_SPINNER:
            return {
                ...state,
                showSpinner: action.showSpinner,
            };
        case SHOW_FLASH_MESSAGE:
            return Object.assign({}, state, {
                successMessage: action.successMessage,
                errorMessage: action.errorMessage,
            });
        case CLEAN_ALERTS:
            return Object.assign({}, state, {
                successMessage: null,
                errorMessage: null,
            });

        default:
            return state;
    }
};

export default commonReducer;
