Guest User

Untitled

a guest
Jan 22nd, 2017
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.22 KB | None | 0 0
  1. //src/reducers/reducers.js
  2.  
  3. import * as types from '../actions';
  4.  
  5. //=================================
  6. // Assumptions Reducer
  7. //=================================
  8.  
  9. const initialAssumptionsState = {
  10. incomeSources: 1,
  11. income: 0,
  12. initialSavings: 0
  13. };
  14.  
  15. const assumptionsReducer = function(state=initialAssumptionsState, action){
  16. switch(action.type){
  17. case types.UPDATE_INCOME_SOURCES:
  18. return Object.assign({}, state, {incomeSources: action.sources})
  19.  
  20. case types.UPDATE_INCOME:
  21. return Object.assign({}, state, {income: action.income})
  22.  
  23. case types.UPDATE_SAVINGS:
  24. return Object.assign({}, state, {initialSavings: action.savings})
  25.  
  26. default: return state;
  27. }
  28. }
  29.  
  30. //=================================
  31. // Expenses Reducer
  32. //=================================
  33.  
  34. const initialExpensesState = [
  35. {
  36. id: 'exp_1',
  37. name: 'Rent',
  38. amount: 0
  39. },
  40.  
  41. {
  42. id: 'exp_2',
  43. name: 'Food',
  44. amount: 0
  45. },
  46.  
  47. {
  48. id: 'exp_3',
  49. name: 'Electric Bill',
  50. amount: 0
  51. }
  52. ]
  53.  
  54. const expensesReducer = function(state = initialExpensesState, action){
  55. switch(action.type){
  56. case types.ADD_EXPENSE:
  57. return Object.assign([], state,
  58. [
  59. {
  60. id: action.id,
  61. name: '',
  62. amount: 0
  63. },
  64.  
  65. ...state,
  66. ]
  67. )
  68.  
  69. case types.REMOVE_EXPENSE:
  70. return state.filter(expense => {
  71. return expense.id !== action.id;
  72. })
  73.  
  74. case types.UPDATE_EXPENSE_NAME:
  75. let {id, newName} = action.payload;
  76. return state.map(expense => {
  77. if(expense.id === id){
  78. expense.name = newName;
  79. }
  80.  
  81. return expense;
  82. })
  83.  
  84. case types.UPDATE_EXPENSE_AMOUNT:
  85. let id_1 = action.payload.id;
  86. let newAmount = action.payload.newAmount
  87.  
  88. return state.map(expense => {
  89. if(expense.id === id_1){
  90. expense.amount = newAmount;
  91. }
  92.  
  93. return expense;
  94. })
  95.  
  96. default: return state;
  97. }
  98. }
  99.  
  100. //=================================
  101. // SavingsPlan Reducer
  102. //=================================
  103.  
  104. const initialSavingsPlanState = {
  105. monthlySavings: 50
  106. }
  107.  
  108. const savingsPlanReducer = function(state=initialSavingsPlanState, action){
  109. switch(action.type){
  110. case types.UPDATE_MONTHLY_SAVINGS:
  111. return Object.assign({}, state, {monthlySavings: action.newAmount})
  112.  
  113. default: return state;
  114. }
  115. }
  116.  
  117.  
  118. export {assumptionsReducer, expensesReducer, savingsPlanReducer};
Add Comment
Please, Sign In to add comment