Skip to content

Commit d5b3845

Browse files
Merge pull request #68 from SundeepChand/bugs/notif
Mark Individual Notifications as read
2 parents 43e8b2b + 6f90868 commit d5b3845

File tree

6 files changed

+49
-82
lines changed

6 files changed

+49
-82
lines changed

src/assets/scss/components/notifications.scss

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
1-
.notifications-dropdown {
2-
max-height: 512px;
3-
width: 256px;
4-
}
1+
.notifications {
2+
&-dropdown {
3+
max-height: 512px;
4+
overflow-y: scroll;
5+
width: 256px;
6+
}
57

6-
.notifications-options {
7-
padding: 4px 8px;
8+
&-options {
9+
padding: 4px 8px;
10+
}
811
}
912

1013
@media only screen and (max-width: 768px) {

src/components/Notifications.js

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -55,21 +55,30 @@ const Notifications = () => {
5555
}
5656
}, [userId, notificationsSeen])
5757

58+
const markNotificationAsRead = async (notification) => {
59+
const seenBy = notification.seenBy.map((seen) => seen.id)
60+
if (!seenBy.includes(userId)) {
61+
seenBy.push(userId)
62+
await userStory.markNotificationAsRead(notification.id, seenBy)
63+
}
64+
}
65+
5866
const markAllNotificationsAsRead = () => {
5967
if (notifications) {
6068
notifications.forEach(async (notification) => {
61-
const seenBy = notification.seenBy.map((seen) => seen.id)
62-
if (!seenBy.includes(userId)) {
63-
seenBy.push(userId)
64-
await userStory.markNotificationAsRead(notification.id, seenBy)
65-
setNotifications([])
66-
setNotificationCount(0)
67-
setNotificationsSeen(false)
68-
}
69+
await markNotificationAsRead(notification)
70+
setNotifications([])
71+
setNotificationCount(0)
72+
setNotificationsSeen(false)
6973
})
7074
}
7175
}
7276

77+
const onNotificationClick = async (notification) => {
78+
await markNotificationAsRead(notification)
79+
navigate(`/${notification.link}`)
80+
}
81+
7382
return (
7483
state.auth && (
7584
<div
@@ -103,7 +112,7 @@ const Notifications = () => {
103112
? notifications.map((notification, key) => (
104113
<li
105114
className='dropdown-element'
106-
onClick={() => navigate(`/${notification.link}`)}
115+
onClick={() => onNotificationClick(notification)}
107116
key={key}
108117
>
109118
{notification.message}

src/pages/Home.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ const Home = () => {
217217
const acceptUpdatedPolicy = async () => {
218218
const seenBy = policyUpdate.seenBy.map((seen) => seen.id)
219219
seenBy.push(userId)
220-
await userStory.updateNotifications(policyUpdate.id, seenBy)
220+
await userStory.markNotificationAsRead(policyUpdate.id, seenBy)
221221
setModal(false)
222222
}
223223

src/pages/Notifications.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ const Notifications = () => {
1616

1717
useEffect(() => {
1818
const fetchNotifications = async () => {
19-
const response = await userStory.getNotifications(userId)
19+
const response = await userStory.getNotificationsByUserId(userId)
2020
setNotifications(response.data.data.userStoryNotifications)
2121
}
2222
trackPromise(fetchNotifications())

src/services/gql_fragments.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@ export const NOTIFICATION_DATA_FRAGMENT = `fragment NotificationData on UserStor
1212
message
1313
id
1414
users {
15-
id
15+
id
1616
}
1717
seenBy {
18-
id
18+
id
1919
}
2020
date
2121
link

src/services/user_story.js

Lines changed: 18 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ const userStory = {
195195
}
196196
return apiCall('/graphql', productQuery)
197197
},
198-
getNotifications: (userId) => {
198+
getNotificationsByUserId: (userId) => {
199199
const notificationQuery = {
200200
query: `query {
201201
userStoryNotifications (where: {
@@ -223,27 +223,27 @@ const userStory = {
223223
}
224224
return apiCall('/graphql', policyQuery)
225225
},
226-
updateNotifications: (policyId, seenBy) => {
227-
const notificationQuery = {
228-
query: `mutation updateNotifications($seenBy: [ID]){
229-
updateUserStoryNotification(input: {
230-
where: {
231-
id: "${policyId}"
232-
}
233-
data: {
234-
seenBy: $seenBy
235-
}
236-
}) {
237-
userStoryNotification {
238-
id
239-
}
240-
}
241-
}`,
226+
markNotificationAsRead: (notificationId, seenBy) => {
227+
const markNotificationAsReadQuery = {
228+
query: `mutation updateNotifications($seenBy: [ID]) {
229+
updateUserStoryNotification(input: {
230+
where: {
231+
id: "${notificationId}"
232+
}
233+
data: {
234+
seenBy: $seenBy
235+
}
236+
}) {
237+
userStoryNotification {
238+
id
239+
}
240+
}
241+
}`,
242242
variables: {
243243
seenBy: seenBy
244244
}
245245
}
246-
return apiCall('/graphql', notificationQuery)
246+
return apiCall('/graphql', markNotificationAsReadQuery)
247247
},
248248
getUserDetails: (userId) => {
249249
const userQuery = {
@@ -332,51 +332,6 @@ const userStory = {
332332
postCommentReply: (data) => {
333333
return apiCall('/user-story-comment-threads', data)
334334
},
335-
getNotificationsByUserId: (userId) => {
336-
const getNotificationsByUserIdQuery = {
337-
query: `query {
338-
userStoryNotifications (where: {
339-
users: {
340-
id: "${userId}"
341-
}
342-
}){
343-
message
344-
id
345-
users {
346-
id
347-
}
348-
seenBy {
349-
id
350-
}
351-
date
352-
link
353-
}
354-
}`
355-
}
356-
return apiCall('/graphql', getNotificationsByUserIdQuery)
357-
},
358-
markNotificationAsRead: (notificationId, seenBy) => {
359-
const markNotificationAsReadQuery = {
360-
query: `mutation updateNotifications($seenBy: [ID]){
361-
updateUserStoryNotification(input: {
362-
where: {
363-
id: "${notificationId}"
364-
}
365-
data: {
366-
seenBy: $seenBy
367-
}
368-
}) {
369-
userStoryNotification {
370-
id
371-
}
372-
}
373-
}`,
374-
variables: {
375-
seenBy: seenBy
376-
}
377-
}
378-
return apiCall('/graphql', markNotificationAsReadQuery)
379-
},
380335
updateVotes: (storyId, updatedFollowerIds) => {
381336
const updateVotesQuery = {
382337
query: `

0 commit comments

Comments
 (0)