Passed
Push — task/explore-skills-ui ( 6a5680...55c906 )
by Yonathan
04:30
created

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

Complexity

Total Complexity 1
Complexity/F 0

Size

Lines of Code 53
Function Count 0

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 47
dl 0
loc 53
rs 10
c 0
b 0
f 0
wmc 1
mnd 1
bc 1
fnc 0
bpm 0
cpm 0
noi 0
1
import { createSelector } from "reselect";
2
import createCachedSelector from "re-reselect";
3
import { RootState } from "../store";
4
import { AssessmentPlanNotificationState } from "./assessmentPlanNotificationReducer";
5
import { AssessmentPlanNotification } from "../../models/types";
6
import { hasKey } from "../../helpers/queries";
7
8
const stateSlice = (state: RootState): AssessmentPlanNotificationState =>
9
  state.assessmentPlanNotification;
10
11
const getNotificationState = (
12
  state: RootState,
13
): {
14
  byId: { [id: number]: AssessmentPlanNotification };
15
  allIds: number[];
16
} => stateSlice(state).entities.notifications;
17
18
export const getNotifications = createSelector(
19
  getNotificationState,
20
  (notificationState): AssessmentPlanNotification[] =>
21
    notificationState.allIds.map(
22
      (id): AssessmentPlanNotification => notificationState.byId[id],
23
    ),
24
);
25
26
export const getNotificationsByJob = createCachedSelector(
27
  getNotifications,
28
  (state: RootState, ownProps: { jobId: number }): number => ownProps.jobId,
29
  (notifications, jobId): AssessmentPlanNotification[] =>
30
    notifications.filter(
31
      (notification): boolean => notification.job_poster_id === jobId,
32
    ),
33
)((state, ownProps): number => ownProps.jobId);
34
35
export const getUnreadNotificationsByJob = createCachedSelector(
36
  getNotificationsByJob,
37
  (notifications): AssessmentPlanNotification[] =>
38
    notifications.filter(
39
      (notification): boolean => notification.acknowledged === false,
40
    ),
41
)((state, ownProps): number => ownProps.jobId);
42
43
export const notificationIsUpdating = (
44
  state: RootState,
45
  id: number,
46
): boolean => {
47
  const { updatingById } = stateSlice(state).ui;
48
  return hasKey(updatingById, id) ? updatingById[id] : false;
49
};
50
51
export const notificationsAreFetching = (state: RootState): boolean =>
52
  stateSlice(state).ui.fetching;
53