Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import { ActionTypes } from '../util/ActionTypes';
- import {REHYDRATE} from 'redux-persist/constants';
- import update from 'immutability-helper';
- import { remove } from 'lodash';
- export const initialState = {
- userGoals: [],
- categoryGoals: {},
- goalsMap: {},
- };
- export function GoalReducer(state = initialState, action) {
- switch (action.type) {
- case ActionTypes.FETCH_GOALS_REQUEST_SUCCESS: {
- const userGoals = [];
- const goalsMap = {};
- action.data.goalSeries.map((item) => {
- let key = item.id;
- goalsMap[key] = item;
- userGoals.push(key);
- });
- return {
- ...state,
- userGoals,
- goalsMap,
- }
- }
- case ActionTypes.FETCH_CATEGORY_GOALS_REQUEST_SUCCESS: {
- const goals = [];
- const goalsMap = {};
- action.data.goalSeries.map((item) => {
- let key = item.id;
- goalsMap[key] = item;
- goals.push(key);
- });
- let newCategoryGoals = {};
- newCategoryGoals[action.category] = goals;
- return update(state, {
- categoryGoals: {$merge: newCategoryGoals},
- goalsMap: {$merge: goalsMap},
- });
- }
- case ActionTypes.ADD_GOAL_REQUEST_SUCCESS: {
- let goalId = action.goalId;
- let userGoalId = action.data.id;
- if (!state.goalsMap[goalId]) {
- // Most likely from onboarding
- return state;
- }
- return update(state, {
- userGoals: {$unshift: [goalId]},
- goalsMap: {
- [goalId]: {
- isUserGoal: {$set: true},
- matchingUserGoalId: {$set: userGoalId}
- }
- },
- });
- }
- // eager delete for fast UI
- case ActionTypes.DELETE_GOAL_REQUEST: {
- const goalId = action.goalId;
- let userGoals = state.userGoals.slice();
- remove(userGoals, (item) => {
- return item === goalId;
- });
- return update(state, {
- userGoals: {$set: userGoals},
- goalsMap: {
- [goalId]: {
- isUserGoal: {$set: false},
- matchingUserGoalId: {$set: ''}
- }
- },
- });
- }
- case ActionTypes.LOGOUT:
- return initialState;
- default:
- return state;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement