getAllEvents.js 809 B

12345678910111213141516171819202122232425262728293031
  1. import * as types from "../actionTypes/actionTypes";
  2. const eventsURL = 'https://api-marathon.herokuapp.com/api/v1/event';
  3. export const getEvents = payload => ({
  4. type: types.GET_REQUEST_EVENTS,
  5. payload
  6. });
  7. export const getEventsSuccess = payload => ({
  8. type: types.GET_REQUEST_SUCCESS_EVENTS,
  9. payload
  10. });
  11. export const getEventsError = payload => ({
  12. type: types.GET_REQUEST_ERROR_EVENTS,
  13. payload
  14. });
  15. export const getAllEvents = (type = 'All events') => {
  16. return dispatch => {
  17. let promise = fetch(type !== 'All events' ? `${eventsURL}/?eventType=${type}` : eventsURL)
  18. dispatch(getEvents())
  19. promise.then(
  20. data => data.json().then(data => dispatch(getEventsSuccess(data))),
  21. error => dispatch(getEventsError(error))
  22. )
  23. }
  24. }