Guest User

Untitled

a guest
Jul 22nd, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.35 KB | None | 0 0
  1. import uuid from "uuid/v4";
  2. import { combineReducers } from "redux";
  3.  
  4. export const types = {
  5. CREATE: "CHARACTERS_CREATE",
  6. UPDATE: "CHARACTERS_UPDATE",
  7. DELETE: "CHARACTERS_DELETE"
  8. };
  9.  
  10. const byId = (state = {}, action) => {
  11. switch (action.type) {
  12. case types.CREATE:
  13. case types.UPDATE:
  14. const characterData = action.payload;
  15. return { ...state, [characterData.id]: characterData };
  16. case types.DELETE:
  17. const deleteId = action.payload;
  18. const { [`${deleteId}`]: deleted, ...newState } = state;
  19. return newState;
  20. default:
  21. return state;
  22. }
  23. };
  24.  
  25. const allIds = (state = [], action) => {
  26. switch (action.type) {
  27. case types.CREATE:
  28. const { id } = action.payload;
  29. return [...state, id];
  30. case types.DELETE:
  31. const deleteId = action.payload;
  32. return state.filter(id => id !== deleteId);
  33. default:
  34. return state;
  35. }
  36. };
  37.  
  38. export const reducer = combineReducers({ byId, allIds });
  39.  
  40. const createAction = newCharacter => {
  41. const id = newCharacter.id || uuid();
  42. return {
  43. type: types.CREATE,
  44. payload: { ...newCharacter, id }
  45. };
  46. };
  47.  
  48. const updateAction = updateCharacter => ({
  49. type: types.UPDATE,
  50. payload: updateCharacter
  51. });
  52.  
  53. const deleteAction = id => ({ type: types.DELETE, payload: id });
  54.  
  55. export const actions = {
  56. create: createAction,
  57. update: updateAction,
  58. delete: deleteAction
  59. };
Add Comment
Please, Sign In to add comment