Advertisement
Guest User

Untitled

a guest
May 24th, 2017
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.14 KB | None | 0 0
  1. import { ActionTypes } from '../util/ActionTypes';
  2. import {REHYDRATE} from 'redux-persist/constants';
  3. import update from 'immutability-helper';
  4. import { remove } from 'lodash';
  5.  
  6. export const initialState = {
  7. userGoals: [],
  8. categoryGoals: {},
  9. goalsMap: {},
  10. };
  11.  
  12. export function GoalReducer(state = initialState, action) {
  13. switch (action.type) {
  14.  
  15. case ActionTypes.FETCH_GOALS_REQUEST_SUCCESS: {
  16. const userGoals = [];
  17. const goalsMap = {};
  18. action.data.goalSeries.map((item) => {
  19. let key = item.id;
  20. goalsMap[key] = item;
  21. userGoals.push(key);
  22. });
  23. return {
  24. ...state,
  25. userGoals,
  26. goalsMap,
  27. }
  28. }
  29.  
  30. case ActionTypes.FETCH_CATEGORY_GOALS_REQUEST_SUCCESS: {
  31. const goals = [];
  32. const goalsMap = {};
  33. action.data.goalSeries.map((item) => {
  34. let key = item.id;
  35. goalsMap[key] = item;
  36. goals.push(key);
  37. });
  38. let newCategoryGoals = {};
  39. newCategoryGoals[action.category] = goals;
  40. return update(state, {
  41. categoryGoals: {$merge: newCategoryGoals},
  42. goalsMap: {$merge: goalsMap},
  43. });
  44. }
  45.  
  46. case ActionTypes.ADD_GOAL_REQUEST_SUCCESS: {
  47. let goalId = action.goalId;
  48. let userGoalId = action.data.id;
  49.  
  50. if (!state.goalsMap[goalId]) {
  51. // Most likely from onboarding
  52. return state;
  53. }
  54.  
  55. return update(state, {
  56. userGoals: {$unshift: [goalId]},
  57. goalsMap: {
  58. [goalId]: {
  59. isUserGoal: {$set: true},
  60. matchingUserGoalId: {$set: userGoalId}
  61. }
  62. },
  63. });
  64. }
  65.  
  66. // eager delete for fast UI
  67. case ActionTypes.DELETE_GOAL_REQUEST: {
  68. const goalId = action.goalId;
  69. let userGoals = state.userGoals.slice();
  70. remove(userGoals, (item) => {
  71. return item === goalId;
  72. });
  73.  
  74. return update(state, {
  75. userGoals: {$set: userGoals},
  76. goalsMap: {
  77. [goalId]: {
  78. isUserGoal: {$set: false},
  79. matchingUserGoalId: {$set: ''}
  80. }
  81. },
  82. });
  83. }
  84.  
  85.  
  86. case ActionTypes.LOGOUT:
  87. return initialState;
  88.  
  89. default:
  90. return state;
  91. }
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement