adminMainPageActions.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. import axios from "axios";
  2. import * as types from "../actionTypes/actionTypes"
  3. const eventURL = 'https://api-marathon.herokuapp.com/api/v1/event'
  4. export const editEvent = payload => {
  5. return {
  6. type: types.EDIT_EVENT,
  7. payload
  8. };
  9. };
  10. //POST
  11. const postNewEventRequest = payload => ({
  12. type: types.POST_NEW_EVENT_REQUEST,
  13. payload
  14. });
  15. const postNewEventRequestSuccess = payload => ({
  16. type: types.POST_NEW_EVENT_REQUEST_SUCCESS,
  17. payload
  18. });
  19. const postNewEventRequestFail = payload => ({
  20. type: types.POST_NEW_EVENT_REQUEST_FAIL,
  21. payload
  22. });
  23. export const postNewEvent = payload => {
  24. return async dispatch => {
  25. dispatch(postNewEventRequest());
  26. try {
  27. const { data } = await axios.post("https://api-marathon.herokuapp.com/api/v1/event", payload);
  28. dispatch(postNewEventRequestSuccess(data));
  29. console.log("New Event Posted", payload)
  30. } catch (error) {
  31. dispatch(postNewEventRequestFail(error));
  32. }
  33. };
  34. };
  35. //PUT
  36. const editEventRequest = payload => ({
  37. type: types.EDIT_EVENT_REQUEST,
  38. payload
  39. });
  40. const editEventRequestSuccess = payload => ({
  41. type: types.EDIT_EVENT_REQUEST_SUCCESS,
  42. payload
  43. });
  44. const editEventRequestFail = payload => ({
  45. type: types.EDIT_EVENT_REQUEST_FAIL,
  46. payload
  47. });
  48. export const changeEvent = (payload) => {
  49. return async dispatch => {
  50. dispatch(editEventRequest());
  51. try {
  52. await axios.put(`https://api-marathon.herokuapp.com/api/v1/event/${payload._id} `, payload);
  53. dispatch(editEventRequestSuccess({payload, id: payload._id}));
  54. console.log("Event Changed", payload)
  55. } catch (error) {
  56. console.log('edit payload',payload)
  57. dispatch(editEventRequestFail(error));
  58. }
  59. };
  60. };
  61. //GET
  62. const getEventsRequest = payload => ({
  63. type: types.GET_EVENTS_REQUEST,
  64. payload
  65. });
  66. const getEventsRequestSuccess = payload => ({
  67. type: types.GET_EVENTS_REQUEST_SUCCESS,
  68. payload
  69. });
  70. const getEventsRequestFail = payload => ({
  71. type: types.GET_EVENTS_REQUEST_FAIL,
  72. payload
  73. });
  74. export const getEvents = (type = 'Select Event Type') => dispatch => {
  75. dispatch(getEventsRequest());
  76. return axios
  77. .get( type !== 'Select Event Type' ? `${eventURL}/?eventType=${type}` : eventURL)
  78. .then(res => dispatch(getEventsRequestSuccess(res)))
  79. .catch(err => dispatch(getEventsRequestFail(err)));
  80. };
  81. //GET BY TITLE
  82. const getEventByTitleRequest = payload => ({
  83. type: types.GET_EVENT_BY_TITLE,
  84. payload
  85. });
  86. const getEventByTitleRequestSuccess = payload => ({
  87. type: types.GET_EVENT_BY_TITLE_SUCCESS,
  88. payload
  89. });
  90. const getEventByTitleRequestFail = payload => ({
  91. type: types.GET_EVENT_BY_TITLE_FAIL,
  92. payload
  93. });
  94. export const getEventByTitle = (title) => dispatch => {
  95. dispatch(getEventByTitleRequest());
  96. return axios
  97. .get( title && `${eventURL}/?title=${title}`)
  98. .then(res => dispatch(getEventByTitleRequestSuccess(res)))
  99. .catch(err => dispatch(getEventByTitleRequestFail(err)));
  100. };
  101. // REMOVE
  102. const removeEventRequest = payload => ({
  103. type: types.REMOVE_EVENT_REQUEST,
  104. payload
  105. });
  106. const removeEventSuccess = payload => ({
  107. type: types.REMOVE_EVENT_REQUEST_SUCCESS,
  108. payload
  109. });
  110. const removeEventFail = payload => ({
  111. type: types.REMOVE_EVENT_REQUEST_FAIL,
  112. payload
  113. });
  114. export const removeEvent = _id => dispatch => {
  115. dispatch(removeEventRequest());
  116. return axios
  117. .delete(` https://api-marathon.herokuapp.com/api/v1/event/${_id}`)
  118. .then(res => dispatch(removeEventSuccess({ res, _id })))
  119. .catch(err => dispatch(removeEventFail(err)));
  120. };