import * as types from "../actionTypes/actionTypes";

export const regEvent = payload => ({
    type: types.POST_REQUEST_CHECKIN,
    payload
});

export const regEventSuccess = payload => ({
    type: types.POST_REQUEST_SUCCESS_CHECKIN,
    payload
});

export const regEventError = payload => ({
    type: types.POST_REQUEST_ERROR_CHECKIN,
    payload
});

export const regEventSubmit = payload => {
    return dispatch => {
        let promise = fetch("https://api-marathon.herokuapp.com/api/v1/eventUsers",
            {
                method: 'POST',
                body: JSON.stringify(payload),
                headers: {
                    "Content-type": "application/json"
                }
            }
        )

        dispatch(regEvent())

        promise.then(
            data => data.json().then(data => dispatch(regEventSuccess(data))),
            error => dispatch(regEventError(error))
        )
    }
}