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
|
|
|
|