-
Notifications
You must be signed in to change notification settings - Fork 785
/
Copy pathnotifications.reducer.js
174 lines (172 loc) · 4.64 KB
/
notifications.reducer.js
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
168
169
170
171
172
173
174
import {
GET_UNREAD_NOTIFICATIONS,
GET_PARTICIPATING_NOTIFICATIONS,
GET_ALL_NOTIFICATIONS,
MARK_NOTIFICATION_AS_READ,
MARK_REPO_AS_READ,
GET_NOTIFICATIONS_COUNT,
MARK_ALL_NOTIFICATIONS_AS_READ,
} from './notifications.type';
export const initialState = {
unread: [],
participating: [],
all: [],
isPendingUnread: false,
isPendingParticipating: false,
isPendingAll: false,
isPendingMarkNotificationAsRead: false,
isPendingRepoMarkAsRead: false,
isPendingMarkAllNotificationsAsRead: false,
error: '',
notificationsCount: null,
};
export const notificationsReducer = (state = initialState, action = {}) => {
switch (action.type) {
case GET_UNREAD_NOTIFICATIONS.PENDING:
return {
...state,
isPendingUnread: true,
};
case GET_UNREAD_NOTIFICATIONS.SUCCESS:
return {
...state,
unread: action.payload,
isPendingUnread: false,
};
case GET_UNREAD_NOTIFICATIONS.ERROR:
return {
...state,
error: action.payload,
isPendingUnread: false,
};
case GET_PARTICIPATING_NOTIFICATIONS.PENDING:
return {
...state,
isPendingParticipating: true,
};
case GET_PARTICIPATING_NOTIFICATIONS.SUCCESS:
return {
...state,
participating: action.payload,
isPendingParticipating: false,
};
case GET_PARTICIPATING_NOTIFICATIONS.ERROR:
return {
...state,
error: action.payload,
isPendingParticipating: false,
};
case GET_ALL_NOTIFICATIONS.PENDING:
return {
...state,
isPendingAll: true,
};
case GET_ALL_NOTIFICATIONS.SUCCESS:
return {
...state,
all: action.payload,
isPendingAll: false,
};
case GET_ALL_NOTIFICATIONS.ERROR:
return {
...state,
error: action.payload,
isPendingAll: false,
};
case MARK_NOTIFICATION_AS_READ.PENDING:
return {
...state,
isPendingMarkNotificationAsRead: true,
};
case MARK_NOTIFICATION_AS_READ.SUCCESS:
return {
...state,
unread: state.unread.filter(
notification => notification.id !== action.notificationID
),
participating: state.participating.filter(
notification => notification.id !== action.notificationID
),
all: state.all.map(
notification =>
notification.id === action.notificationID
? { ...notification, unread: false }
: notification
),
isPendingMarkNotificationAsRead: false,
notificationsCount:
state.notificationsCount && state.notificationsCount - 1,
};
case MARK_NOTIFICATION_AS_READ.ERROR:
return {
...state,
error: action.payload,
isPendingMarkNotificationAsRead: false,
};
case MARK_REPO_AS_READ.PENDING:
return {
...state,
isPendingRepoMarkAsRead: true,
};
case MARK_REPO_AS_READ.SUCCESS:
return {
...state,
unread: state.unread.filter(
notification =>
notification.repository.full_name !== action.repoFullName
),
participating: state.participating.filter(
notification =>
notification.repository.full_name !== action.repoFullName
),
all: state.all.map(
notification =>
notification.repository.full_name === action.repoFullName
? { ...notification, unread: false }
: notification
),
isPendingRepoMarkAsRead: false,
notificationsCount:
state.notificationsCount &&
state.notificationsCount - action.repoNotificationsCount,
};
case MARK_REPO_AS_READ.ERROR:
return {
...state,
error: action.payload,
isPendingRepoMarkAsRead: false,
};
case GET_NOTIFICATIONS_COUNT.PENDING:
return {
...state,
};
case GET_NOTIFICATIONS_COUNT.SUCCESS:
return {
...state,
notificationsCount: action.payload,
};
case GET_NOTIFICATIONS_COUNT.ERROR:
return {
...state,
error: action.payload,
};
case MARK_ALL_NOTIFICATIONS_AS_READ.PENDING:
return {
...state,
isPendingMarkAllNotificationsAsRead: true,
};
case MARK_ALL_NOTIFICATIONS_AS_READ.SUCCESS:
return {
...state,
isPendingMarkAllNotificationsAsRead: false,
};
case MARK_ALL_NOTIFICATIONS_AS_READ.ERROR:
return {
...state,
error: action.payload,
isPendingMarkAllNotificationsAsRead: false,
};
default:
return state;
}
};