123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139 |
- import axios from "axios";
- import * as types from "../actionTypes/actionTypes"
- const eventURL = 'https://api-marathon.herokuapp.com/api/v1/event'
- export const editEvent = payload => {
- return {
- type: types.EDIT_EVENT,
- payload
- };
- };
- //POST
- const postNewEventRequest = payload => ({
- type: types.POST_NEW_EVENT_REQUEST,
- payload
- });
- const postNewEventRequestSuccess = payload => ({
- type: types.POST_NEW_EVENT_REQUEST_SUCCESS,
- payload
- });
- const postNewEventRequestFail = payload => ({
- type: types.POST_NEW_EVENT_REQUEST_FAIL,
- payload
- });
- export const postNewEvent = payload => {
- return async dispatch => {
- dispatch(postNewEventRequest());
- try {
- const { data } = await axios.post("https://api-marathon.herokuapp.com/api/v1/event", payload);
- dispatch(postNewEventRequestSuccess(data));
- console.log("New Event Posted", payload)
- } catch (error) {
- dispatch(postNewEventRequestFail(error));
- }
- };
- };
- //PUT
- const editEventRequest = payload => ({
- type: types.EDIT_EVENT_REQUEST,
- payload
- });
- const editEventRequestSuccess = payload => ({
- type: types.EDIT_EVENT_REQUEST_SUCCESS,
- payload
- });
- const editEventRequestFail = payload => ({
- type: types.EDIT_EVENT_REQUEST_FAIL,
- payload
- });
- export const changeEvent = (payload) => {
- return async dispatch => {
- dispatch(editEventRequest());
- try {
- await axios.put(`https://api-marathon.herokuapp.com/api/v1/event/${payload._id} `, payload);
- dispatch(editEventRequestSuccess({payload, id: payload._id}));
- console.log("Event Changed", payload)
- } catch (error) {
- console.log('edit payload',payload)
- dispatch(editEventRequestFail(error));
- }
- };
- };
- //GET
- const getEventsRequest = payload => ({
- type: types.GET_EVENTS_REQUEST,
- payload
- });
- const getEventsRequestSuccess = payload => ({
- type: types.GET_EVENTS_REQUEST_SUCCESS,
- payload
- });
- const getEventsRequestFail = payload => ({
- type: types.GET_EVENTS_REQUEST_FAIL,
- payload
- });
- export const getEvents = (type = 'Select Event Type') => dispatch => {
- dispatch(getEventsRequest());
- return axios
- .get( type !== 'Select Event Type' ? `${eventURL}/?eventType=${type}` : eventURL)
- .then(res => dispatch(getEventsRequestSuccess(res)))
- .catch(err => dispatch(getEventsRequestFail(err)));
- };
- //GET BY TITLE
- const getEventByTitleRequest = payload => ({
- type: types.GET_EVENT_BY_TITLE,
- payload
- });
- const getEventByTitleRequestSuccess = payload => ({
- type: types.GET_EVENT_BY_TITLE_SUCCESS,
- payload
- });
- const getEventByTitleRequestFail = payload => ({
- type: types.GET_EVENT_BY_TITLE_FAIL,
- payload
- });
- export const getEventByTitle = (title) => dispatch => {
- dispatch(getEventByTitleRequest());
- return axios
- .get( title && `${eventURL}/?title=${title}`)
- .then(res => dispatch(getEventByTitleRequestSuccess(res)))
- .catch(err => dispatch(getEventByTitleRequestFail(err)));
- };
- // REMOVE
- const removeEventRequest = payload => ({
- type: types.REMOVE_EVENT_REQUEST,
- payload
- });
- const removeEventSuccess = payload => ({
- type: types.REMOVE_EVENT_REQUEST_SUCCESS,
- payload
- });
- const removeEventFail = payload => ({
- type: types.REMOVE_EVENT_REQUEST_FAIL,
- payload
- });
- export const removeEvent = _id => dispatch => {
- dispatch(removeEventRequest());
- return axios
- .delete(` https://api-marathon.herokuapp.com/api/v1/event/${_id}`)
- .then(res => dispatch(removeEventSuccess({ res, _id })))
- .catch(err => dispatch(removeEventFail(err)));
- };
|