Skip to content

Commit f5329b0

Browse files
committed
Refactored Notifications.js
1 parent d08eda6 commit f5329b0

File tree

3 files changed

+48
-49
lines changed

3 files changed

+48
-49
lines changed

src/pages/Notifications.js

Lines changed: 2 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,8 @@ import { navigate } from '@reach/router'
44

55
import Navigation from '../components/Navigation'
66
import LoadingIndicator from '../modules/LoadingIndicator'
7-
import axios from 'axios'
8-
import { apiURL } from '../config.json'
97
import { Helmet } from 'react-helmet'
8+
import userStory from '../services/user_story'
109

1110
const Notifications = () => {
1211
const userId = localStorage.getItem('id')
@@ -17,32 +16,7 @@ const Notifications = () => {
1716

1817
useEffect(() => {
1918
const fetchNotifications = async () => {
20-
const response = await axios.post(
21-
`${apiURL}/graphql`,
22-
{
23-
query: `query {
24-
userStoryNotifications (where: {
25-
users: {
26-
id: "${userId}"
27-
}
28-
}){
29-
message
30-
id
31-
users {
32-
id
33-
}
34-
seenBy {
35-
id
36-
}
37-
date
38-
link
39-
}
40-
}`
41-
},
42-
{
43-
withCredentials: true
44-
}
45-
)
19+
const response = await userStory.getNotifications(userId)
4620
setNotifications(response.data.data.userStoryNotifications)
4721
}
4822
trackPromise(fetchNotifications())

src/services/gql_fragments.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
export const BASIC_STORY_INFO_FRAGMENT = `fragment BasicStoryInfo on UserStory {
2+
id
3+
Title
4+
Description
5+
followers {
6+
id
7+
username
8+
}
9+
}`
10+
11+
export const NOTIFICATION_DATA_FRAGMENT = `fragment NotificationData on UserStoryNotification {
12+
message
13+
id
14+
users {
15+
id
16+
}
17+
seenBy {
18+
id
19+
}
20+
date
21+
link
22+
}`

src/services/user_story.js

Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,8 @@
11
import apiCall from './api'
2-
3-
const BASIC_STORY_INFO_FRAGMENT = `fragment BasicStoryInfo on UserStory {
4-
id
5-
Title
6-
Description
7-
followers {
8-
id
9-
username
10-
}
11-
}`
2+
import {
3+
BASIC_STORY_INFO_FRAGMENT,
4+
NOTIFICATION_DATA_FRAGMENT
5+
} from './gql_fragments'
126

137
const userStory = {
148
createStory: ({ description, title, category, product, priority }) => {
@@ -181,22 +175,31 @@ const userStory = {
181175
}
182176
return apiCall('/graphql', productQuery)
183177
},
178+
getNotifications: (userId) => {
179+
const notificationQuery = {
180+
query: `query {
181+
userStoryNotifications (where: {
182+
users: {
183+
id: "${userId}"
184+
}
185+
}){
186+
...NotificationData
187+
}
188+
}
189+
${NOTIFICATION_DATA_FRAGMENT}
190+
`
191+
}
192+
return apiCall('/graphql', notificationQuery)
193+
},
184194
getPolicyNotifications: () => {
185195
const policyQuery = {
186196
query: `query {
187197
userStoryNotifications(where: {message: "User story privacy policy has been updated"}) {
188-
message
189-
id
190-
users {
191-
id
192-
}
193-
seenBy {
194-
id
195-
}
196-
date
197-
link
198+
...NotificationData
198199
}
199-
}`
200+
}
201+
${NOTIFICATION_DATA_FRAGMENT}
202+
`
200203
}
201204
return apiCall('/graphql', policyQuery)
202205
},

0 commit comments

Comments
 (0)