Skip to content

Commit 35272d4

Browse files
Refactor GraphQL queries and mutations for Home.js
1 parent fc26519 commit 35272d4

File tree

6 files changed

+207
-143
lines changed

6 files changed

+207
-143
lines changed

.eslintrc.yml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,4 @@ rules:
1515
prefer-const: 'error'
1616
prefer-template: 'error'
1717
prefer-destructuring: ['error', { 'object': true, 'array': false }]
18-
object-curly-spacing: [2, 'always']
19-
no-console: 'error'
18+
object-curly-spacing: [2, 'always']

.gitignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030
.env.development.local
3131
.env.test.local
3232
.env.production.local
33-
config.json
3433
.vscode/
3534
npm-debug.log*
3635
yarn-debug.log*

src/config.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"apiURL": "http://localhost:1337",
3+
"APP_ENV": "dev"
4+
}

src/pages/Home.js

Lines changed: 17 additions & 140 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,7 @@ import React, {
55
useCallback,
66
useContext
77
} from 'react'
8-
import axios from 'axios'
98
import { Link } from '@reach/router'
10-
import { apiURL } from '../config.json'
119
import { trackPromise, usePromiseTracker } from 'react-promise-tracker'
1210
import { Helmet } from 'react-helmet'
1311

@@ -23,6 +21,7 @@ import UsersSuggestionDropdown from '../components/UsersSuggestionDropdown'
2321
import Lists from '../utils/Lists'
2422
import useAuth from '../hooks/useAuth'
2523
import Context from '../modules/Context'
24+
import userStory from '../services/user_story'
2625

2726
const Home = () => {
2827
const { logout } = useAuth()
@@ -104,55 +103,13 @@ const Home = () => {
104103

105104
useEffect(() => {
106105
const fetchStories = async () => {
107-
const response = await axios.post(
108-
`${apiURL}/graphql`,
109-
{
110-
query: `query {
111-
userStories(sort: "createdAt:desc", limit: 5, start: ${
112-
(page - 1) * 5
113-
}, where: {
114-
user_story_status : {
115-
Status: "${currentStateSelected}"
116-
},
117-
author: {
118-
username_contains: "${userQuery}"
119-
}
120-
${categoryQuery}
121-
${productQuery}
122-
${searchQuery}
123-
}) {
124-
id
125-
Title
126-
Description
127-
user_story_status {
128-
Status
129-
}
130-
user_story_comments {
131-
Comments
132-
}
133-
product {
134-
Name
135-
}
136-
author {
137-
id
138-
username
139-
profilePicture {
140-
id
141-
url
142-
}
143-
}
144-
followers {
145-
id
146-
username
147-
}
148-
Category
149-
}
150-
}
151-
`
152-
},
153-
{
154-
withCredentials: true
155-
}
106+
const response = await userStory.getStories(
107+
page,
108+
currentStateSelected,
109+
userQuery,
110+
categoryQuery,
111+
productQuery,
112+
searchQuery
156113
)
157114
setStories(response.data.data.userStories)
158115
}
@@ -168,30 +125,11 @@ const Home = () => {
168125

169126
useEffect(() => {
170127
const fetchStoryCount = async () => {
171-
const response = await axios.post(
172-
`${apiURL}/graphql`,
173-
{
174-
query: `
175-
query {
176-
userStoriesConnection(where: {
177-
user_story_status: {
178-
Status: "${currentStateSelected}"
179-
},
180-
author: {
181-
username_contains: "${userQuery}"
182-
}
183-
${productQuery},
184-
${searchQuery}
185-
}) {
186-
aggregate {
187-
count
188-
}
189-
}
190-
}`
191-
},
192-
{
193-
withCredentials: true
194-
}
128+
const response = await userStory.getStoryCount(
129+
currentStateSelected,
130+
userQuery,
131+
productQuery,
132+
searchQuery
195133
)
196134
setStoryCount(response.data.data.userStoriesConnection.aggregate.count)
197135
}
@@ -200,20 +138,7 @@ const Home = () => {
200138

201139
useEffect(() => {
202140
const fetchProducts = async () => {
203-
const response = await axios.post(
204-
`${apiURL}/graphql`,
205-
{
206-
query: `query {
207-
products {
208-
Name
209-
}
210-
}`
211-
},
212-
{
213-
withCredentials: true
214-
}
215-
)
216-
141+
const response = await userStory.getProducts()
217142
return response.data.data.product !== null
218143
? setProducts([
219144
'All',
@@ -228,9 +153,7 @@ const Home = () => {
228153

229154
useEffect(() => {
230155
const fetchCategories = async () => {
231-
const response = await axios.post(`${apiURL}/graphql`, {
232-
query: '{ __type(name: "ENUM_USERSTORY_CATEGORY") {enumValues {name}}}'
233-
})
156+
const response = await userStory.getCategories()
234157
setCategories([
235158
'All',
236159
...response.data.data.__type.enumValues.map((ele) => {
@@ -264,29 +187,7 @@ const Home = () => {
264187

265188
useEffect(() => {
266189
const fetchPolicyNotifications = async () => {
267-
const response = await axios.post(
268-
`${apiURL}/graphql`,
269-
{
270-
query: `
271-
query {
272-
userStoryNotifications(where: {message: "User story privacy policy has been updated"}) {
273-
message
274-
id
275-
users {
276-
id
277-
}
278-
seenBy {
279-
id
280-
}
281-
date
282-
link
283-
}
284-
}`
285-
},
286-
{
287-
withCredentials: true
288-
}
289-
)
190+
const response = await userStory.getPolicyNotifications()
290191
if (response.data.data.userStoryNotifications) {
291192
const seenBy = response.data.data.userStoryNotifications[0]?.seenBy.map(
292193
(seen) => seen.id
@@ -308,31 +209,7 @@ const Home = () => {
308209
const acceptUpdatedPolicy = async () => {
309210
const seenBy = policyUpdate.seenBy.map((seen) => seen.id)
310211
seenBy.push(userId)
311-
await axios.post(
312-
`${apiURL}/graphql`,
313-
{
314-
query: `mutation updateNotifications($seenBy: [ID]){
315-
updateUserStoryNotification(input: {
316-
where: {
317-
id: "${policyUpdate.id}"
318-
}
319-
data: {
320-
seenBy: $seenBy
321-
}
322-
}) {
323-
userStoryNotification {
324-
id
325-
}
326-
}
327-
}`,
328-
variables: {
329-
seenBy: seenBy
330-
}
331-
},
332-
{
333-
withCredentials: true
334-
}
335-
)
212+
await userStory.updateNotifications(policyUpdate.id, seenBy)
336213
setModal(false)
337214
}
338215

src/services/api.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import axios from 'axios'
2+
import { apiURL, APP_ENV } from '../config.json'
3+
4+
const config = {
5+
baseURL: apiURL,
6+
withCredentials: true
7+
}
8+
9+
const apiClient = axios.create(config)
10+
11+
apiClient.interceptors.response.use(
12+
(response) => {
13+
if (APP_ENV !== 'prod') {
14+
console.log('RESPONSE: ', response)
15+
}
16+
return response
17+
},
18+
(error) => {
19+
if (error.response) {
20+
// The request was made and the server responded with a status code
21+
console.log('RESPONSE_ERROR: ', error.response)
22+
return Promise.reject(error.response)
23+
} else if (error.request) {
24+
// The request was made but no response was received
25+
console.log('NO_RESPONSE: ', error.request)
26+
} else {
27+
// Something happened in setting up the request that triggered an error
28+
console.log('REQUEST_ERROR: ', error.message)
29+
}
30+
}
31+
)
32+
33+
function apiCall(url, data) {
34+
return apiClient.request({
35+
url,
36+
method: 'post',
37+
data,
38+
...config
39+
})
40+
}
41+
42+
export default apiCall

0 commit comments

Comments
 (0)