forked from WorldBrain/Memex
-
Notifications
You must be signed in to change notification settings - Fork 0
/
reducer.ts
114 lines (98 loc) · 3.08 KB
/
reducer.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
import { createReducer } from 'redux-act'
import { Notification } from './types'
import * as actions from './actions'
export interface State {
notificationsList: Notification[]
resultExhausted: boolean
showMoreIndex?: string
currentPage: number
isLoading: boolean
isReadExpanded: boolean
showInbox: boolean
unreadNotifCount: number
localStorageNotif: any
}
const defaultState: State = {
notificationsList: [],
resultExhausted: false,
showMoreIndex: undefined,
currentPage: 0,
isLoading: true,
isReadExpanded: false,
showInbox: false,
unreadNotifCount: 0,
localStorageNotif: {},
}
const nextPage = () => (state: State) => ({
...state,
currentPage: state.currentPage + 1,
})
// Updates notifications result state by either overwriting or appending
const handleNotificationResult = ({ overwrite }) => (
state,
newNotificationResult,
) => {
const readNotificationList = overwrite
? newNotificationResult
: {
...newNotificationResult,
notifications: [
...state.readNotificationList.notifications,
...newNotificationResult.notifications,
],
}
return { ...state, readNotificationList }
}
const handleReadNotification = () => (state, index) => {
const notification = state.notificationsList[index]
return {
...state,
notificationsList: [
...state.notificationsList.slice(0, index),
{
...notification,
isRead: true,
},
...state.notificationsList.slice(index + 1),
],
}
}
const appendNotifications = () => (state, newNotifications) => {
return {
...state,
notificationsList: [
...state.notificationsList,
...newNotifications.notifications,
],
resultExhausted: newNotifications.resultExhausted,
}
}
const setNotifications = () => (state, notifications) => ({
...state,
notificationsList: notifications.notifications,
resultExhausted: notifications.resultExhausted,
})
const toggleExpand = () => state => ({
...state,
isReadExpanded: !state.isReadExpanded,
})
const toggleShowInbox = () => state => ({
...state,
showInbox: !state.showInbox,
})
const initState = <T>(key) => (state: State, payload: T) => ({
...state,
[key]: payload,
})
const reducer = createReducer<State>({}, defaultState)
reducer.on(actions.setNotificationsResult, setNotifications())
reducer.on(actions.setShowMoreIndex, initState<boolean>('showMoreIndex'))
reducer.on(actions.nextPage, nextPage())
reducer.on(actions.setLoading, initState<boolean>('isLoading'))
reducer.on(actions.toggleReadExpand, toggleExpand())
reducer.on(actions.toggleInbox, toggleShowInbox())
reducer.on(actions.setUnreadCount, initState<number>('unreadNotifCount'))
reducer.on(actions.setStorageKeys, initState<any>('localStorageNotif'))
reducer.on(actions.handleReadNotification, handleReadNotification())
reducer.on(actions.appendResult, appendNotifications())
export default reducer