Skip to content

Commit c24d2a6

Browse files
committed
refactor Vote.js
1 parent b5dd659 commit c24d2a6

File tree

2 files changed

+38
-39
lines changed

2 files changed

+38
-39
lines changed

src/components/Vote.js

Lines changed: 6 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
import React, { useState, useEffect } from 'react'
2-
import axios from 'axios'
3-
import { apiURL } from '../config.json'
42
import Modal from './Modal'
53
import { Link } from '@reach/router'
4+
import userStory from '../services/user_story'
65

76
const Vote = (props) => {
87
const { story } = props
@@ -40,50 +39,18 @@ const Vote = (props) => {
4039
let updatedFollowerIds = followers.filter(
4140
(id) => id !== JSON.stringify(userId)
4241
)
43-
const response = await axios.post(
44-
`${apiURL}/graphql`,
45-
{
46-
query: `
47-
mutation {
48-
updateUserStory(input: {where: {id: "${story.id}"} data: {followers: [${updatedFollowerIds}]}}){
49-
userStory{
50-
followers {
51-
id
52-
}
53-
}
54-
}
55-
}
56-
`
57-
},
58-
{
59-
withCredentials: true
60-
}
61-
)
42+
const response = await userStory.updateVotes(story.id, updatedFollowerIds)
6243
updatedFollowerIds = response.data.data.updateUserStory.userStory.followers.map(
6344
(follower) => JSON.stringify(follower.id)
6445
)
6546
setFollowers(updatedFollowerIds)
6647
setVoted(false)
6748
setVotes((votes) => votes - 1)
6849
} else {
69-
const response = await axios.post(
70-
`${apiURL}/graphql`,
71-
{
72-
query: `
73-
mutation {
74-
updateUserStory(input: {where: {id: "${story.id}"} data: {followers: [${followers}, "${userId}"]}}){
75-
userStory{
76-
followers {
77-
id
78-
}
79-
}
80-
}
81-
}
82-
`
83-
},
84-
{
85-
withCredentials: true
86-
}
50+
const response = await userStory.updateStoryVote(
51+
story.id,
52+
followers,
53+
userId
8754
)
8855
const updatedFollowerIds = response.data.data.updateUserStory.userStory.followers.map(
8956
(follower) => JSON.stringify(follower.id)

src/services/user_story.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -426,6 +426,38 @@ const userStory = {
426426
}
427427
}
428428
return apiCall('/graphql', markNotificationAsReadQuery)
429+
},
430+
updateVotes: (storyId, updatedFollowerIds) => {
431+
const updateVotesQuery = {
432+
query: `
433+
mutation {
434+
updateUserStory(input: {where: {id: "${storyId}"} data: {followers: [${updatedFollowerIds}]}}){
435+
userStory{
436+
followers {
437+
id
438+
}
439+
}
440+
}
441+
}
442+
`
443+
}
444+
return apiCall('/graphql', updateVotesQuery)
445+
},
446+
updateStoryVote: (storyId, followers, userId) => {
447+
const updateStoryVoteQuery = {
448+
query: `
449+
mutation {
450+
updateUserStory(input: {where: {id: "${storyId}"} data: {followers: [${followers}, "${userId}"]}}){
451+
userStory{
452+
followers {
453+
id
454+
}
455+
}
456+
}
457+
}
458+
`
459+
}
460+
return apiCall('/graphql', updateStoryVoteQuery)
429461
}
430462
}
431463

0 commit comments

Comments
 (0)