import * as types from "../actionTypes/actionTypes"
import initialState from "../state/addEventInitialValue";


export default (state = initialState, action) => {

    switch (action.type) {

		case types.EDIT_EVENT: {
			const eventFormInitialValue = state.eventList.find(el => el._id === action.payload);
			console.log(eventFormInitialValue)
			return {
				...state,
				eventFormInitialValue: eventFormInitialValue,
				editFormFlag: true
			};
		}

		//POST
        case types.POST_NEW_EVENT_REQUEST_SUCCESS: {
			console.log('reducer add event success', action.payload)
			return {
				state,
				addEventMessage: "New event has been added"
			}
		}
		case types.POST_NEW_EVENT_REQUEST_FAIL: {
			console.log('reducer add event fail', action.payload)
			return { ...state, error: action.payload }
		}

		// GET ALL EVENTS
		case types.GET_EVENTS_REQUEST: {
			return state;
		}
		case types.GET_EVENTS_REQUEST_SUCCESS: {
			const { data } = action.payload;
			const eventList = data.events
			return { ...state, eventList };
		}
		case types.GET_EVENTS_REQUEST_FAIL: {
			return state;
		}

		// REMOVE
		case types.REMOVE_EVENT_REQUEST: {
			return state;
		}
		case types.REMOVE_EVENT_REQUEST_SUCCESS: {
			const { id } = action.payload;
			return {
				...state,
				eventList: state.eventList.filter(el => el.id !== id)
			};
		}

		case types.REMOVE_EVENT_REQUEST_FAIL: {
			return state;
		}

        default:
			return state; 
    }
}