Passed
Push — feature/redux-experience-skill ( 70f135 )
by Tristan
06:23
created

resources/assets/js/store/AssessmentPlanNotification/assessmentPlanNotificationReducer.ts   A

Complexity

Total Complexity 2
Complexity/F 0

Size

Lines of Code 156
Function Count 0

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 118
dl 0
loc 156
rs 10
c 0
b 0
f 0
wmc 2
mnd 2
bc 2
fnc 0
bpm 0
cpm 0
noi 0
1
import { combineReducers, Reducer } from "redux";
2
import { AssessmentPlanNotification } from "../../models/types";
3
import {
4
  FETCH_ASSESSMENT_PLAN_NOTIFICATIONS_SUCCEEDED,
5
  AssessmentPlanNotificationAction,
6
  UPDATE_ASSESSMENT_PLAN_NOTIFICATION_SUCCEEDED,
7
  UPDATE_ASSESSMENT_PLAN_NOTIFICATION_STARTED,
8
  UPDATE_ASSESSMENT_PLAN_NOTIFICATION_FAILED,
9
  FETCH_ASSESSMENT_PLAN_NOTIFICATIONS_STARTED,
10
  FETCH_ASSESSMENT_PLAN_NOTIFICATIONS_FAILED,
11
} from "./assessmentPlanNotificationActions";
12
import { mapToObject, getId } from "../../helpers/queries";
13
14
interface EntityState {
15
  notifications: {
16
    byId: {
17
      [id: number]: AssessmentPlanNotification;
18
    };
19
    allIds: number[];
20
  };
21
}
22
23
interface UiState {
24
  updatingById: {
25
    [id: number]: boolean;
26
  };
27
  fetching: boolean;
28
}
29
30
export interface AssessmentPlanNotificationState {
31
  entities: EntityState;
32
  ui: UiState;
33
}
34
35
const initEntityState = (): EntityState => ({
36
  notifications: {
37
    byId: {},
38
    allIds: [],
39
  },
40
});
41
42
const initUiState = (): UiState => ({ updatingById: {}, fetching: false });
43
44
export const initState = (): AssessmentPlanNotificationState => ({
45
  entities: initEntityState(),
46
  ui: initUiState(),
47
});
48
49
export const sortNotifications = (notificationsById: {
50
  [id: number]: AssessmentPlanNotification;
51
}): number[] => {
52
  const notifications = Object.values(notificationsById);
53
  const comparator = (
54
    a: AssessmentPlanNotification,
55
    b: AssessmentPlanNotification,
56
  ): number => {
57
    return a.created_at.getTime() - b.created_at.getTime();
58
  };
59
  const sortedIds = notifications.sort(comparator).map(getId);
60
  return sortedIds;
61
};
62
63
const entityReducer = (
64
  state = initEntityState(),
65
  action: AssessmentPlanNotificationAction,
66
): EntityState => {
67
  switch (action.type) {
68
    case FETCH_ASSESSMENT_PLAN_NOTIFICATIONS_SUCCEEDED: {
69
      const newById = {
70
        ...state.notifications.byId,
71
        ...mapToObject(action.payload, getId),
72
      };
73
      return {
74
        ...state,
75
        notifications: {
76
          byId: newById,
77
          allIds: sortNotifications(newById),
78
        },
79
      };
80
    }
81
    case UPDATE_ASSESSMENT_PLAN_NOTIFICATION_SUCCEEDED: {
82
      const newById = {
83
        ...state.notifications.byId,
84
        [action.payload.id]: action.payload,
85
      };
86
      return {
87
        ...state,
88
        notifications: {
89
          byId: newById,
90
          allIds: sortNotifications(newById),
91
        },
92
      };
93
    }
94
    default:
95
      return state;
96
  }
97
};
98
99
const uiReducer = (
100
  state = initUiState(),
101
  action: AssessmentPlanNotificationAction,
102
): UiState => {
103
  switch (action.type) {
104
    case FETCH_ASSESSMENT_PLAN_NOTIFICATIONS_STARTED:
105
      return {
106
        ...state,
107
        fetching: true,
108
      };
109
    case FETCH_ASSESSMENT_PLAN_NOTIFICATIONS_SUCCEEDED:
110
      return {
111
        ...state,
112
        fetching: false,
113
      };
114
    case FETCH_ASSESSMENT_PLAN_NOTIFICATIONS_FAILED:
115
      return {
116
        ...state,
117
        fetching: false,
118
      };
119
    case UPDATE_ASSESSMENT_PLAN_NOTIFICATION_STARTED:
120
      return {
121
        ...state,
122
        updatingById: {
123
          ...state.updatingById,
124
          [action.payload.id]: true,
125
        },
126
      };
127
    case UPDATE_ASSESSMENT_PLAN_NOTIFICATION_SUCCEEDED:
128
      return {
129
        ...state,
130
        updatingById: {
131
          ...state.updatingById,
132
          [action.payload.id]: false,
133
        },
134
      };
135
    case UPDATE_ASSESSMENT_PLAN_NOTIFICATION_FAILED:
136
      return {
137
        ...state,
138
        updatingById: {
139
          ...state.updatingById,
140
          [action.meta.id]: false,
141
        },
142
      };
143
    default:
144
      return state;
145
  }
146
};
147
148
export const assessmentPlanNotificationReducer: Reducer<AssessmentPlanNotificationState> = combineReducers(
149
  {
150
    entities: entityReducer,
151
    ui: uiReducer,
152
  },
153
);
154
155
export default assessmentPlanNotificationReducer;
156