Skip to content

Commit 55721ed

Browse files
committed
Refactored Profile.js
1 parent f5329b0 commit 55721ed

File tree

2 files changed

+38
-78
lines changed

2 files changed

+38
-78
lines changed

src/pages/Profile.js

Lines changed: 7 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
import React, { useState, useEffect, useRef } from 'react'
2-
import axios from 'axios'
3-
import { apiURL } from '../config.json'
42
import { trackPromise, usePromiseTracker } from 'react-promise-tracker'
53
import { Helmet } from 'react-helmet'
64
import { navigate } from '@reach/router'
@@ -13,6 +11,9 @@ import Dropdown from '../components/Dropdown'
1311
import UserProfile from '../components/UserProfile'
1412
import Lists from '../utils/Lists'
1513

14+
import userStory from '../services/user_story'
15+
import User from '../services/user'
16+
1617
const Profile = (props) => {
1718
const { profileId } = props
1819
const [stories, setStories] = useState([])
@@ -35,19 +36,7 @@ const Profile = (props) => {
3536

3637
useEffect(() => {
3738
const fetchProducts = async () => {
38-
const response = await axios.post(
39-
`${apiURL}/graphql`,
40-
{
41-
query: `query {
42-
products {
43-
Name
44-
}
45-
}`
46-
},
47-
{
48-
withCredentials: true
49-
}
50-
)
39+
const response = await userStory.getProducts()
5140
setProducts(
5241
response.data.data.products.map((ele) => {
5342
return ele.Name
@@ -60,9 +49,7 @@ const Profile = (props) => {
6049

6150
useEffect(() => {
6251
const fetchCategories = async () => {
63-
const response = await axios.post(`${apiURL}/graphql`, {
64-
query: '{ __type(name: "ENUM_USERSTORY_CATEGORY") {enumValues {name}}}'
65-
})
52+
const response = await userStory.getCategories()
6653

6754
setCategories(
6855
response.data.data.__type.enumValues.map((ele) => {
@@ -76,30 +63,7 @@ const Profile = (props) => {
7663

7764
useEffect(() => {
7865
const fetchUserInfo = async () => {
79-
const response = await axios.post(
80-
`${apiURL}/graphql`,
81-
{
82-
query: `query {
83-
user(id: "${profileId}") {
84-
profilePicture {
85-
url
86-
}
87-
Name
88-
Bio
89-
username
90-
Company
91-
Profession
92-
email
93-
LinkedIn
94-
Twitter
95-
}
96-
}
97-
`
98-
},
99-
{
100-
withCredentials: true
101-
}
102-
)
66+
const response = await User.getInfo(profileId)
10367
setUser(response.data.data.user)
10468
}
10569
if (profileId) {
@@ -109,40 +73,7 @@ const Profile = (props) => {
10973

11074
useEffect(() => {
11175
const fetchMyStories = async () => {
112-
const response = await axios.post(
113-
`${apiURL}/graphql`,
114-
{
115-
query: `query {
116-
user(id: "${profileId}") {
117-
user_stories {
118-
id
119-
Title
120-
Description
121-
user_story_status {
122-
Status
123-
}
124-
followers {
125-
username
126-
}
127-
product {
128-
Name
129-
}
130-
author {
131-
id
132-
username
133-
}
134-
user_story_comments {
135-
Comments
136-
}
137-
Category
138-
}
139-
}
140-
}`
141-
},
142-
{
143-
withCredentials: true
144-
}
145-
)
76+
const response = await User.getUserStoriesByUser(profileId)
14677
setStories(response.data.data.user.user_stories)
14778
}
14879
trackPromise(fetchMyStories())

src/services/user.js

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import apiCall from './api'
2+
import { BASIC_STORY_INFO_FRAGMENT } from './gql_fragments'
23

34
const User = {
45
updateUser: (user) => {
@@ -25,10 +26,10 @@ const User = {
2526
}
2627
return apiCall('/graphql', updateQuery)
2728
},
28-
getInfo: (id) => {
29+
getInfo: (userId) => {
2930
const userQuery = {
3031
query: `query {
31-
user(id: "${id}") {
32+
user(id: "${userId}") {
3233
profilePicture {
3334
id
3435
url
@@ -46,6 +47,34 @@ const User = {
4647
`
4748
}
4849
return apiCall('/graphql', userQuery)
50+
},
51+
getUserStoriesByUser: (userId) => {
52+
const userQuery = {
53+
query: `query {
54+
user(id: "${userId}") {
55+
user_stories {
56+
...BasicStoryInfo
57+
user_story_status {
58+
Status
59+
}
60+
product {
61+
Name
62+
}
63+
author {
64+
id
65+
username
66+
}
67+
user_story_comments {
68+
Comments
69+
}
70+
Category
71+
}
72+
}
73+
}
74+
${BASIC_STORY_INFO_FRAGMENT}
75+
`
76+
}
77+
return apiCall('/graphql', userQuery)
4978
}
5079
}
5180

0 commit comments

Comments
 (0)