Skip to content

Commit 9f43d90

Browse files
committed
Refactored Story.js
1 parent 7c8b99c commit 9f43d90

File tree

2 files changed

+43
-55
lines changed

2 files changed

+43
-55
lines changed

src/pages/Story.js

Lines changed: 4 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
import React, { useEffect, useState } from 'react'
2-
import axios from 'axios'
3-
import { apiURL } from '../config.json'
42
import { trackPromise, usePromiseTracker } from 'react-promise-tracker'
53
import {
64
FacebookShareButton,
@@ -25,6 +23,7 @@ import Navigation from '../components/Navigation'
2523
import { Link, navigate } from '@reach/router'
2624
import Vote from '../components/Vote'
2725
import Modal from '../components/Modal'
26+
import userStory from '../services/user_story'
2827

2928
const Story = (props) => {
3029
const { storyId } = props
@@ -49,46 +48,12 @@ const Story = (props) => {
4948

5049
useEffect(() => {
5150
const fetchStory = async () => {
52-
const response = await axios.post(
53-
`${apiURL}/graphql`,
54-
{
55-
query: `query {
56-
userStory(id: "${storyId}") {
57-
id
58-
Title
59-
Description
60-
user_story_status {
61-
Status
62-
}
63-
author {
64-
id
65-
username
66-
}
67-
followers {
68-
id
69-
username
70-
}
71-
}
72-
}`
73-
},
74-
{
75-
withCredentials: true
76-
}
77-
)
51+
const response = await userStory.getStory(storyId)
7852
setStory(response.data.data.userStory)
7953
}
8054
trackPromise(fetchStory())
8155
const editStory = async () => {
82-
const check = await axios.post(
83-
`${apiURL}/checkAuthor`,
84-
{
85-
id: userId,
86-
storyId: storyId
87-
},
88-
{
89-
withCredentials: true
90-
}
91-
)
56+
const check = await userStory.checkAuthor(userId, storyId)
9257
if (check.data) {
9358
setEditMode(true)
9459
}
@@ -105,23 +70,7 @@ const Story = (props) => {
10570
event.preventDefault()
10671
const combinedDescription = story.Description + editDescription
10772
const filteredDescription = filterDescriptionText(combinedDescription)
108-
await axios.post(
109-
`${apiURL}/graphql`,
110-
{
111-
query: `mutation {
112-
updateUserStory(
113-
input: { where: { id: "${storyId}" }, data: { Description: "${filteredDescription}" } }
114-
) {
115-
userStory {
116-
updatedAt
117-
}
118-
}
119-
}`
120-
},
121-
{
122-
withCredentials: true
123-
}
124-
)
73+
await userStory.updateUserStoryDescription(storyId, filteredDescription)
12574
setEditor(false)
12675
setStory({
12776
...story,

src/services/user_story.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,12 @@ const userStory = {
2828
}
2929
return apiCall('/graphql', createQuery)
3030
},
31+
checkAuthor: (userId, storyId) => {
32+
return apiCall('/checkAuthor', {
33+
id: userId,
34+
storyId: storyId
35+
})
36+
},
3137
getAllStories: () => {
3238
const query = {
3339
query: `query {
@@ -113,6 +119,25 @@ const userStory = {
113119
}
114120
return apiCall('/graphql', storiesQuery)
115121
},
122+
getStory: (storyId) => {
123+
const storyQuery = {
124+
query: `query {
125+
userStory(id: "${storyId}") {
126+
...BasicStoryInfo
127+
user_story_status {
128+
Status
129+
}
130+
author {
131+
id
132+
username
133+
}
134+
}
135+
}
136+
${BASIC_STORY_INFO_FRAGMENT}
137+
`
138+
}
139+
return apiCall('/graphql', storyQuery)
140+
},
116141
getStoryCount: (
117142
currentStateSelected,
118143
authorQuery,
@@ -141,6 +166,20 @@ const userStory = {
141166
}
142167
return apiCall('/graphql', storyCountQuery)
143168
},
169+
updateUserStoryDescription: (storyId, description) => {
170+
const updateQuery = {
171+
query: `mutation {
172+
updateUserStory(
173+
input: { where: { id: "${storyId}" }, data: { Description: "${description}" } }
174+
) {
175+
userStory {
176+
updatedAt
177+
}
178+
}
179+
}`
180+
}
181+
return apiCall('/graphql', updateQuery)
182+
},
144183
getCategories: () => {
145184
const categoryQuery = {
146185
query: '{ __type(name: "ENUM_USERSTORY_CATEGORY") {enumValues {name}}}'

0 commit comments

Comments
 (0)