Skip to content

Commit 5457b4b

Browse files
Backend Integration + Fixes + Broadcast type
1 parent 9b24c41 commit 5457b4b

File tree

5 files changed

+99
-237
lines changed

5 files changed

+99
-237
lines changed

dist/dev/index.js

+3-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/prod/index.js

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/actions/notifications.js

+46-24
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,31 @@
33
* @desc Actions related to notifications data.
44
*/
55

6+
import _ from 'lodash';
67
import { createActions } from 'redux-actions';
78
import { getService } from '../services/notifications';
89

10+
/**
11+
* TODO: We will need to change this based on API and
12+
* frontend mapping we need later.
13+
*/
14+
function processData(data) {
15+
const retData = _.map(data, (item) => {
16+
const object = {};
17+
object.id = item.id;
18+
object.sourceId = item.contents.id;
19+
object.sourceName = item.contents.name || item.contents.title;
20+
object.eventType = item.type;
21+
object.isRead = item.read;
22+
object.isSeen = item.seen;
23+
object.contents = item.contents.message || item.contents.title;
24+
object.version = item.version;
25+
object.date = item.createdAt;
26+
return object;
27+
});
28+
return retData;
29+
}
30+
931
/**
1032
* @static
1133
* @desc Creates an action that signals beginning of notifications
@@ -19,17 +41,17 @@ function getNotificationsInit() {
1941
/**
2042
* @static
2143
* @desc Creates an action that loads member achievements.
44+
* @param {String} tokenV3 v3 auth token.
2245
* @return {Action}
2346
*/
24-
async function getNotificationsDone(item) {
47+
async function getNotificationsDone(tokenV3) {
2548
let data;
2649
try {
27-
data = await getService().getNotifications(item);
50+
data = await getService(tokenV3).getNotifications();
2851
} catch (e) {
2952
data = [];
3053
}
31-
32-
return data;
54+
return processData(data.items || []);
3355
}
3456

3557
/**
@@ -45,16 +67,16 @@ function markNotificationAsReadInit() {
4567
/**
4668
* @static
4769
* @desc Creates an action that marks notification as read.
70+
* @param {String} tokenV3 v3 auth token.
4871
* @return {Action}
4972
*/
50-
async function markNotificationAsReadDone(item) {
51-
let data;
73+
async function markNotificationAsReadDone(item, tokenV3) {
5274
try {
53-
data = await getService().markNotificationAsRead(item);
75+
await getService(tokenV3).markNotificationAsRead(item.id);
5476
} catch (e) {
55-
data = [];
77+
return e;
5678
}
57-
return data;
79+
return item;
5880
}
5981

6082
/**
@@ -70,16 +92,16 @@ function markAllNotificationAsReadInit() {
7092
/**
7193
* @static
7294
* @desc Creates an action that marks all notification as read.
95+
* @param {String} tokenV3 v3 auth token.
7396
* @return {Action}
7497
*/
75-
async function markAllNotificationAsReadDone() {
76-
let data;
98+
async function markAllNotificationAsReadDone(tokenV3) {
7799
try {
78-
data = await getService().markAllNotificationAsRead();
100+
await getService(tokenV3).markAllNotificationAsRead();
79101
} catch (e) {
80-
data = [];
102+
return e;
81103
}
82-
return data;
104+
return true;
83105
}
84106

85107

@@ -96,16 +118,16 @@ function markAllNotificationAsSeenInit() {
96118
/**
97119
* @static
98120
* @desc Creates an action that marks all notification as seen.
121+
* @param {String} tokenV3 v3 auth token.
99122
* @return {Action}
100123
*/
101-
async function markAllNotificationAsSeenDone() {
102-
let data;
124+
async function markAllNotificationAsSeenDone(items, tokenV3) {
103125
try {
104-
data = await getService().markAllNotificationAsSeen();
126+
await getService(tokenV3).markAllNotificationAsSeen(items);
105127
} catch (e) {
106-
data = [];
128+
return e;
107129
}
108-
return data;
130+
return items;
109131
}
110132

111133

@@ -122,16 +144,16 @@ function dismissChallengeNotificationsInit() {
122144
/**
123145
* @static
124146
* @desc Creates an action that dismisses all challenge notifications
147+
* @param {String} tokenV3 v3 auth token.
125148
* @return {Action}
126149
*/
127-
async function dismissChallengeNotificationsDone(challengeId) {
128-
let data;
150+
async function dismissChallengeNotificationsDone(challengeId, tokenV3) {
129151
try {
130-
data = await getService().dismissChallengeNotifications(challengeId);
152+
await getService(tokenV3).dismissChallengeNotifications(challengeId);
131153
} catch (e) {
132-
data = [];
154+
return e;
133155
}
134-
return data;
156+
return true;
135157
}
136158

137159

src/reducers/notifications.js

+21-9
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414

1515

1616
import { handleActions } from 'redux-actions';
17-
1817
import actions from '../actions/notifications';
1918
import logger from '../utils/logger';
2019
import { fireErrorMessage } from '../utils/errors';
@@ -81,14 +80,17 @@ function onMarkNotificationAsReadDone(state, { error, payload }) {
8180
return {
8281
...state,
8382
fetchNotificationsFailure: true,
84-
items: [],
8583
};
8684
}
8785

86+
const notifications = state.items;
87+
const itemIndex = state.items.findIndex(item => item.id === payload.id);
88+
notifications[itemIndex].isRead = true;
89+
8890
return {
8991
...state,
90-
items: payload,
9192
fetchNotificationsFailure: false,
93+
items: notifications,
9294
};
9395
}
9496

@@ -118,14 +120,18 @@ function onMarkAllNotificationAsReadDone(state, { error, payload }) {
118120
return {
119121
...state,
120122
fetchNotificationsFailure: true,
121-
items: [],
122123
};
123124
}
124125

126+
const notifications = state.items;
127+
notifications.forEach((item, index) => {
128+
notifications[index].isRead = true;
129+
});
130+
125131
return {
126132
...state,
127-
items: payload,
128-
fetchNotificationsFailure: false,
133+
fetchNotificationsFailure: true,
134+
items: notifications,
129135
};
130136
}
131137

@@ -154,14 +160,21 @@ function onMarkAllNotificationAsSeenDone(state, { error, payload }) {
154160
return {
155161
...state,
156162
fetchNotificationsFailure: true,
157-
items: [],
158163
};
159164
}
160165

166+
const items = payload.split('-');
167+
const notifications = state.items;
168+
state.items.forEach((item, index) => {
169+
if (items.includes(String(item.id))) {
170+
notifications[index].isSeen = true;
171+
}
172+
});
173+
161174
return {
162175
...state,
163-
items: payload,
164176
fetchNotificationsFailure: false,
177+
items: notifications,
165178
};
166179
}
167180

@@ -196,7 +209,6 @@ function onDismissChallengeNotificationsDone(state, { error, payload }) {
196209

197210
return {
198211
...state,
199-
items: payload,
200212
fetchNotificationsFailure: false,
201213
};
202214
}

0 commit comments

Comments
 (0)