reducers.ts 957 B

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. import { CreateState, CreateActionTypes, SCRAPE_RECIPE, RECEIVE_SCRAPE, INVALID_SCRAPE } from './types';
  2. const initialState: CreateState = {
  3. data: {},
  4. initialState: {},
  5. fromScrape: false,
  6. scrapeUrl: null,
  7. fetchingScrape: false,
  8. invalidScrape: false,
  9. };
  10. export const createReducer = (
  11. state = initialState,
  12. action: CreateActionTypes
  13. ): CreateState => {
  14. switch (action.type) {
  15. case SCRAPE_RECIPE:
  16. return {
  17. ...state,
  18. fromScrape: true,
  19. fetchingScrape: true,
  20. invalidScrape: false,
  21. scrapeUrl: action.url,
  22. }
  23. case RECEIVE_SCRAPE:
  24. return {
  25. ...state,
  26. fetchingScrape: false,
  27. data: action.data,
  28. initialState: action.data,
  29. invalidScrape: false,
  30. }
  31. case INVALID_SCRAPE:
  32. return {
  33. ...state,
  34. fetchingScrape: false,
  35. invalidScrape: true,
  36. fromScrape: false,
  37. }
  38. default:
  39. return state
  40. }
  41. };