This repository has been archived by the owner on Mar 12, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 22
/
reducer.ts
80 lines (76 loc) · 2.26 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
import { combineReducers } from 'redux'
import { ActionType, getType } from 'typesafe-actions'
import * as actions from './actions'
import { Notification, LocalAlert } from './models'
import { Alert } from 'react-native'
export interface UpdatesState {
readonly notifications: {
readonly results: ReadonlyArray<Notification>
readonly refreshing: boolean
}
readonly alerts: {
readonly results: ReadonlyArray<LocalAlert>
}
}
export const initialState: UpdatesState = {
notifications: {
results: [],
refreshing: false
},
alerts: {
results: []
}
}
export type UpdatesAction = ActionType<typeof actions>
export default combineReducers<UpdatesState, UpdatesAction>({
notifications: (state = { results: [], refreshing: false }, action) => {
switch (action.type) {
case getType(actions.refreshNotificationsStart):
return { ...state, refreshing: true }
case getType(actions.refreshNotificationsSuccess): {
// Add it to our list for display
const { results } = action.payload
return { ...state, results, refreshing: false }
}
case getType(actions.refreshNotificationsFailure):
return { ...state, refreshing: false }
case getType(actions.newNotificationRequest): {
// Useful so that new notifications you receive while staring at the Feed will just pop in
const results = state.results.slice(0, 99)
results.unshift(action.payload.notification)
return { ...state, results }
}
default:
return state
}
},
alerts: (state = { results: [] }, action) => {
switch (action.type) {
case getType(actions.insertAlert): {
const { type, weight } = action.payload
if (state.results.find(alert => alert.type === type)) {
return state
} else {
return {
...state,
results: [
...state.results,
{
type,
weight
}
]
}
}
}
case getType(actions.removeAlert): {
const results = state.results.filter(result => {
return result.type !== action.payload.type
})
return { ...state, results }
}
default:
return state
}
}
})