Advertisement
Guest User

Untitled

a guest
Aug 22nd, 2019
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.82 KB | None | 0 0
  1. // This is how I would traditionally do it
  2. export const selector = 'toasts'
  3.  
  4. export const actionTypes = {
  5. ADD: `${selector}/ADD`,
  6. SHOW: `${selector}/SHOW`,
  7. HIDE: `${selector}/HIDE`,
  8. }
  9.  
  10. export const reducer = (state = {}, action) => {
  11. switch (action.type) {
  12. case SHOW: {
  13. return {
  14. ...state,
  15. [action.id]: {
  16. message: action.message,
  17. }
  18. }
  19. }
  20. case HIDE: {
  21. const newState: State = { ...state }
  22. delete newState[action.id]
  23. return newState
  24. }
  25. default:
  26. return state
  27. }
  28. }
  29.  
  30. export const actions = {
  31. add: ({ message }) => ({
  32. type: ADD,
  33. message,
  34. }),
  35. show: ({ id, message }) => ({
  36. type: SHOW,
  37. id,
  38. message,
  39. }),
  40. hide: ({ id }) => ({
  41. type: HIDE,
  42. id,
  43. }),
  44. }
  45.  
  46. export const selectors = {
  47. get: ({ state }) => state[selector]
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement