forked from WorldBrain/Memex
-
Notifications
You must be signed in to change notification settings - Fork 0
/
actions.ts
167 lines (139 loc) · 5.21 KB
/
actions.ts
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
import { createAction } from 'redux-act'
import { browser } from 'webextension-polyfill-ts'
import { remoteFunction } from '../util/webextensionRPC'
import * as selectors from './selectors'
import * as constants from './constants'
import { NotifDefinition } from './types'
import * as storageKeys from './storage-keys-notif'
export const setShowMoreIndex = createAction('notifications/setShowMoreIndex')
export const nextPage = createAction('notifications/nextPage')
export const setLoading = createAction<boolean>('notifications/setLoading')
export const toggleReadExpand = createAction('notifications/toggleReadExpand')
export const setNotificationsResult = createAction<NotifDefinition[]>(
'notifications/setNotificationsResult',
)
export const appendResult = createAction<NotifDefinition[]>(
'notifications/appendResult',
)
export const toggleInbox = createAction('notifications/toggleInbox')
export const setUnreadCount = createAction<number>(
'notifications/setUnreadCount',
)
export const setStorageKeys = createAction<any>('notifications/setStorageKeys')
export const handleReadNotification = createAction<number>(
'notifications/handleReadNotification',
)
const fetchUnreadNotifications = remoteFunction('fetchUnreadNotifications')
const fetchReadNotifications = remoteFunction('fetchReadNotifications')
const fetchUnreadCount = remoteFunction('fetchUnreadCount')
const readNotification = remoteFunction('readNotification')
const processEvent = remoteFunction('processEvent')
export const init = () => async (dispatch, getState) => {
const storage = await initStorageValue()
dispatch(setStorageKeys(storage))
dispatch(handleResults())
}
export const initStorageValue = async () => {
const keys = {}
for (const key of Object.keys(storageKeys)) {
const value = (await browser.storage.local.get(storageKeys[key]))[
storageKeys[key]
]
// Special case for SHOULD_TRACK_STORAGE_KEY, becuase the default value is not false
keys[storageKeys[key]] =
key === 'SHOULD_TRACK_STORAGE_KEY' ? value === true : value
}
return keys
}
export const handleResults = () => async (dispatch, getState) => {
dispatch(setLoading(true))
const readNotifications = await getReadNotifications({
notificationsSkip: selectors.notificationsSkip(getState()),
})
const unreadNotifications = await getUnreadNotifications()
const notifications = {
...readNotifications,
notifications: [
...unreadNotifications,
...readNotifications['notifications'],
],
}
dispatch(setNotificationsResult(notifications))
dispatch(setLoading(false))
}
export const getReadNotifications = async ({ notificationsSkip }) => {
const readNotifications = await fetchReadNotifications({
limit: constants.NOTIFICATIONS_PAGE_SIZE,
skip: notificationsSkip,
})
return {
...readNotifications,
notifications: readNotifications['notifications'].map(notification => ({
...notification,
isRead: true,
})),
}
}
export const getUnreadNotifications = async () => {
// dispatch(setLoading(true))
const unreadNotifications = await fetchUnreadNotifications()
// dispatch(setUnreadNotificationList(unreadNotifications))
return unreadNotifications
// dispatch(setLoading(false))
}
export const handleReadNotif = notification => async (dispatch, getState) => {
await readNotification(notification.id)
const notificationsList = selectors.notificationsList(getState())
const index = await new Promise((resolve, reject) => {
for (let i = 0; i < notificationsList.length; i++) {
if (notificationsList[i].id === notification.id) {
resolve(i)
break
}
}
})
processEvent({
type: 'readNotificationOverview',
details: {
notificationId: notification.id,
},
})
dispatch(handleReadNotification(Number(index)))
}
export const handleMoreResult = () => async (dispatch, getState) => {
dispatch(setLoading(true))
let readNotifications = await fetchReadNotifications({
limit: constants.NOTIFICATIONS_PAGE_SIZE,
skip: selectors.notificationsSkip(getState()),
})
readNotifications = {
...readNotifications,
notifications: readNotifications['notifications'].map(notification => ({
...notification,
isRead: true,
})),
}
processEvent({
type: 'readNotificationPagination',
})
dispatch(appendResult(readNotifications))
dispatch(setLoading(false))
}
/**
* Increments the page state before scheduling another notifications result.
*/
export const getMoreNotifications = () => async (dispatch, getState) => {
dispatch(nextPage())
dispatch(handleMoreResult())
}
export const updateUnreadNotif = () => async (dispatch, getState) => {
const unreadNotifs = await fetchUnreadCount()
dispatch(setUnreadCount(unreadNotifs))
}
export const toggleInboxMid = () => (dispatch, getState) => {
const showInbox = selectors.showInbox(getState())
processEvent({
type: !showInbox ? 'openInboxOveview' : 'closeInboxOveview',
})
dispatch(toggleInbox())
}