-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsirenHook.ts
More file actions
104 lines (75 loc) · 2.79 KB
/
sirenHook.ts
File metadata and controls
104 lines (75 loc) · 2.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
import PubSub from 'pubsub-js';
import { errorMap, events, eventTypes } from './constants';
import { useSirenContext } from '../components/sirenProvider';
const useSiren = () => {
const { siren, id: providerId } = useSirenContext();
const markAsReadById = async (id: string) => {
if (siren)
if (id?.length > 0) {
const response = await siren?.markAsReadById(id);
if (response?.data) {
const payload = { id, action: eventTypes.MARK_ITEM_AS_READ };
PubSub.publish(`${events.NOTIFICATION_LIST_EVENT}${providerId}`, JSON.stringify(payload));
}
return response;
} else {
return { error: errorMap.MISSING_PARAMETER };
}
return { error: errorMap.SIREN_OBJECT_NOT_FOUND };
};
const markAsReadByDate = async (untilDate: string) => {
if (siren && untilDate) {
const response = await siren?.markAsReadByDate({startDate: untilDate});
if (response?.data) {
const payload = { action: eventTypes.MARK_ALL_AS_READ };
PubSub.publish(`${events.NOTIFICATION_LIST_EVENT}${providerId}`, JSON.stringify(payload));
}
return response;
}
return { error: errorMap.SIREN_OBJECT_NOT_FOUND };
};
const deleteById = async (id: string, shouldUpdateList: boolean = true) => {
if (siren)
if (id?.length > 0) {
const response = await siren?.deleteById(id);
if (response?.data && shouldUpdateList) {
const payload = { id, action: eventTypes.DELETE_ITEM };
PubSub.publish(`${events.NOTIFICATION_LIST_EVENT}${providerId}`, JSON.stringify(payload));
}
return response;
} else {
return { error: errorMap.MISSING_PARAMETER };
}
return { error: errorMap.SIREN_OBJECT_NOT_FOUND };
};
const deleteByDate = async (untilDate: string) => {
if (siren && untilDate) {
const response = await siren.deleteByDate({startDate: untilDate});
if (response?.data) {
const payload = { action: eventTypes.DELETE_ALL_ITEM };
PubSub.publish(`${events.NOTIFICATION_LIST_EVENT}${providerId}`, JSON.stringify(payload));
}
return response;
}
return { error: errorMap.SIREN_OBJECT_NOT_FOUND };
};
const markAllAsViewed = async (untilDate: string) => {
if (siren && untilDate) {
const response = await siren?.markAllAsViewed(untilDate);
if (response?.data) {
const payload = { unviewedCount: 0, action: eventTypes.UPDATE_NOTIFICATIONS_COUNT };
PubSub.publish(`${events.NOTIFICATION_COUNT_EVENT}${providerId}`, JSON.stringify(payload));
}
return response;
}
return { error: errorMap.SIREN_OBJECT_NOT_FOUND };
};
return {
markAsReadByDate,
markAsReadById,
deleteById,
deleteByDate,
markAllAsViewed
};
};
export default useSiren;