Advertisement
Fahim_7861

Untitled

Mar 7th, 2021
36
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.05 KB | None | 0 0
  1. console.clear();
  2.  
  3. // People dropping off a form (Action Creators)
  4. const createPolicy = (name, amount) => {
  5. return { // Action (a form in our analogy)
  6. type: 'CREATE_POLICY',
  7. payload: {
  8. name: name,
  9. amount: amount
  10. }
  11. };
  12. };
  13.  
  14. const deletePolicy = (name) => {
  15. return {
  16. type: 'DELETE_POLICY',
  17. payload: {
  18. name: name
  19. }
  20. };
  21. };
  22.  
  23. const createClaim = (name, amountOfMoneyToCollect) => {
  24. return {
  25. type: 'CREATE_CLAIM',
  26. payload: {
  27. name: name,
  28. amountOfMoneyToCollect: amountOfMoneyToCollect
  29. }
  30. };
  31. };
  32.  
  33.  
  34. // Reducers (Departments!)
  35. const claimsHistory = (oldListOfClaims = [], action) => {
  36. if (action.type === 'CREATE_CLAIM') {
  37. // we care about this action (FORM!)
  38. return [...oldListOfClaims, action.payload];
  39. }
  40.  
  41. // we don't care the action (form!!)
  42. return oldListOfClaims;
  43. };
  44.  
  45. const accounting = (bagOfMoney = 100, action) => {
  46. if (action.type === 'CREATE_CLAIM') {
  47. return bagOfMoney - action.payload.amountOfMoneyToCollect;
  48. } else if (action.type === 'CREATE_POLICY') {
  49. return bagOfMoney + action.payload.amount;
  50. }
  51.  
  52. return bagOfMoney;
  53. };
  54.  
  55. const policies = (listOfPolicies = [], action) => {
  56. if (action.type === 'CREATE_POLICY') {
  57. return [...listOfPolicies, action.payload.name];
  58. } else if (action.type === 'DELETE_POLICY') {
  59. return listOfPolicies.filter(name => name !== action.payload.name);
  60. }
  61.  
  62. return listOfPolicies;
  63. };
  64.  
  65. const { createStore, combineReducers } = Redux;
  66.  
  67. const ourDepartments = combineReducers({
  68. accounting: accounting,
  69. claimsHistory: claimsHistory,
  70. policies: policies
  71. });
  72.  
  73. const store = createStore(ourDepartments);
  74.  
  75. createPolicy('Alex', 20)
  76. createClaim('Alex', 120)
  77. deletePolicy('Alex')
  78.  
  79. store.dispatch(createPolicy('Alex', 20));
  80. store.dispatch(createPolicy('Jim', 30));
  81. store.dispatch(createPolicy('Bob', 40));
  82.  
  83. // store.dispatch(createClaim('Alex', 120));
  84. // store.dispatch(createClaim('Jim', 50));
  85.  
  86. // store.dispatch(deletePolicy('Bob'));
  87.  
  88. console.log(store.getState());
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement