Skip to content

Commit b5dd659

Browse files
Merge pull request #39 from EOS-uiux-Solutions/refactor
refactor graphql queries
2 parents d250f31 + fddeb7f commit b5dd659

File tree

6 files changed

+140
-148
lines changed

6 files changed

+140
-148
lines changed

.eslintrc.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,4 +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']
18+
object-curly-spacing: [2, 'always']

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
[![Open Source Love svg2](https://badges.frapsoft.com/os/v2/open-source.svg?v=103)](https://github.com/ellerbrock/open-source-badges/)
22
[![MIT license](http://img.shields.io/badge/license-MIT-brightgreen.svg)](https://choosealicense.com/licenses/mit/)
3+
34
# User Story
45

56
![](./static/user_story.png)
7+
68
# Introduction
79

810
The goal of **User Story** is to design and present a scalable backend infrastructure that delivers a web interface allowing users to request new features and give feedback in an easy and intuitive way. Users can attach files with their story to explain what they want. The admins can then resolve, close and update the status of these stories. Users can interact with other stories via comments and votes. This can also serve as an efficient feedback and response mechanism which is critical for any organization to improve and make progress. Simply, its a **product management tool**.
@@ -12,6 +14,7 @@ The goal of **User Story** is to design and present a scalable backend infrastru
1214
![](./static/user_story_workflow.png)
1315

1416
# Guidelines for development
17+
1518
### Setting up the project
1619

1720
After cloning this repository you will need to install all the dependencies: `npm i`

src/components/Comments.js

Lines changed: 4 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
import React, { useState, useEffect, useContext } from 'react'
22
import { Link } from '@reach/router'
33
import Button from './Button'
4-
import axios from 'axios'
5-
import { apiURL } from '../config.json'
64
import { useForm } from 'react-hook-form'
75

86
import Context from '../modules/Context'
97
import FormError from '../components/FormError'
8+
import userStory from '../services/user_story'
109

1110
const Comments = (props) => {
1211
const { storyId } = props
@@ -43,80 +42,14 @@ const Comments = (props) => {
4342

4443
useEffect(() => {
4544
const fetchComments = async () => {
46-
const response = await axios.post(
47-
`${apiURL}/graphql`,
48-
{
49-
query: `
50-
query {
51-
userStory(id: "${storyId}") {
52-
user_story_comments {
53-
id
54-
Comments
55-
user {
56-
id
57-
username
58-
}
59-
createdAt
60-
user_story_comment_replies {
61-
createdAt
62-
Comments
63-
user {
64-
id
65-
username
66-
}
67-
}
68-
}
69-
}
70-
}
71-
`
72-
},
73-
{
74-
withCredentials: true
75-
}
76-
)
45+
const response = await userStory.getComments(storyId)
7746
setComments(response.data.data.userStory.user_story_comments)
7847
}
7948
fetchComments()
8049
}, [storyId, fetchComments])
8150

8251
const addComment = async (data) => {
83-
const response = await axios.post(
84-
`${apiURL}/graphql`,
85-
{
86-
query: `
87-
mutation {
88-
createUserStoryComment(input: {
89-
data: {
90-
Comments: "${data.addComment}"
91-
user_story: "${storyId}"
92-
user: "${id}"
93-
}
94-
}) {
95-
userStoryComment {
96-
id
97-
user {
98-
id
99-
username
100-
}
101-
Comments
102-
createdAt
103-
user_story_comment_replies {
104-
createdAt
105-
Comments
106-
user {
107-
id
108-
username
109-
}
110-
}
111-
}
112-
}
113-
}
114-
`
115-
},
116-
{
117-
withCredentials: true
118-
}
119-
)
52+
const response = await userStory.postComment(data.addComment, storyId, id)
12053
setComments([
12154
...comments,
12255
response.data.data.createUserStoryComment.userStoryComment
@@ -125,27 +58,7 @@ const Comments = (props) => {
12558
}
12659

12760
const addCommentReply = async (data) => {
128-
await axios.post(
129-
`${apiURL}/graphql`,
130-
{
131-
query: `
132-
mutation {
133-
createUserStoryCommentThread (input: {
134-
data: {
135-
Comments: "${data.addReply}"
136-
user_story_comment: "${commentId}"
137-
user: "${id}"
138-
}
139-
}){
140-
userStoryCommentThread {
141-
createdAt
142-
}
143-
}
144-
}
145-
`
146-
},
147-
{ withCredentials: true }
148-
)
61+
await userStory.postCommentReply(data.addReply, commentId, id)
14962
setCommentReply('')
15063
setFetchComments((fetchComments) => !fetchComments)
15164
setRepliesToggled(null)

src/components/Notifications.js

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

76
const Notifications = () => {
87
const userId = localStorage.getItem('id')
@@ -39,32 +38,7 @@ const Notifications = () => {
3938

4039
useEffect(() => {
4140
const fetchNotifications = async () => {
42-
const response = await axios.post(
43-
`${apiURL}/graphql`,
44-
{
45-
query: `query {
46-
userStoryNotifications (where: {
47-
users: {
48-
id: "${userId}"
49-
}
50-
}){
51-
message
52-
id
53-
users {
54-
id
55-
}
56-
seenBy {
57-
id
58-
}
59-
date
60-
link
61-
}
62-
}`
63-
},
64-
{
65-
withCredentials: true
66-
}
67-
)
41+
const response = await userStory.getNotificationsByUserId(userId)
6842
let unseenNotifications = []
6943
if (response.data.data.userStoryNotifications) {
7044
unseenNotifications = response.data.data.userStoryNotifications
@@ -87,31 +61,7 @@ const Notifications = () => {
8761
const seenBy = notification.seenBy.map((seen) => seen.id)
8862
if (!seenBy.includes(userId)) {
8963
seenBy.push(userId)
90-
await axios.post(
91-
`${apiURL}/graphql`,
92-
{
93-
query: `mutation updateNotifications($seenBy: [ID]){
94-
updateUserStoryNotification(input: {
95-
where: {
96-
id: "${notification.id}"
97-
}
98-
data: {
99-
seenBy: $seenBy
100-
}
101-
}) {
102-
userStoryNotification {
103-
id
104-
}
105-
}
106-
}`,
107-
variables: {
108-
seenBy: seenBy
109-
}
110-
},
111-
{
112-
withCredentials: true
113-
}
114-
)
64+
await userStory.markNotificationAsRead(notification.id, seenBy)
11565
setNotifications([])
11666
setNotificationCount(0)
11767
setNotificationsSeen(false)

src/config.json

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

src/services/user_story.js

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,132 @@ const userStory = {
300300
}`
301301
}
302302
return apiCall('/graphql', updateQuery)
303+
},
304+
getComments: (storyId) => {
305+
const commentsQuery = {
306+
query: `
307+
query {
308+
userStory(id: "${storyId}") {
309+
user_story_comments {
310+
id
311+
Comments
312+
user {
313+
id
314+
username
315+
}
316+
createdAt
317+
user_story_comment_replies {
318+
createdAt
319+
Comments
320+
user {
321+
id
322+
username
323+
}
324+
}
325+
}
326+
}
327+
}`
328+
}
329+
return apiCall('/graphql', commentsQuery)
330+
},
331+
postComment: (addComment, storyId, id) => {
332+
const postCommentQuery = {
333+
query: `
334+
mutation {
335+
createUserStoryComment(input: {
336+
data: {
337+
Comments: "${addComment}"
338+
user_story: "${storyId}"
339+
user: "${id}"
340+
}
341+
}) {
342+
userStoryComment {
343+
id
344+
user {
345+
id
346+
username
347+
}
348+
Comments
349+
createdAt
350+
user_story_comment_replies {
351+
createdAt
352+
Comments
353+
user {
354+
id
355+
username
356+
}
357+
}
358+
}
359+
}
360+
}
361+
`
362+
}
363+
return apiCall('/graphql', postCommentQuery)
364+
},
365+
postCommentReply: (addReply, commentId, id) => {
366+
const postCommentReplyQuery = {
367+
query: `
368+
mutation {
369+
createUserStoryCommentThread (input: {
370+
data: {
371+
Comments: "${addReply}"
372+
user_story_comment: "${commentId}"
373+
user: "${id}"
374+
}
375+
}){
376+
userStoryCommentThread {
377+
createdAt
378+
}
379+
}
380+
}
381+
`
382+
}
383+
return apiCall('/graphql', postCommentReplyQuery)
384+
},
385+
getNotificationsByUserId: (userId) => {
386+
const getNotificationsByUserIdQuery = {
387+
query: `query {
388+
userStoryNotifications (where: {
389+
users: {
390+
id: "${userId}"
391+
}
392+
}){
393+
message
394+
id
395+
users {
396+
id
397+
}
398+
seenBy {
399+
id
400+
}
401+
date
402+
link
403+
}
404+
}`
405+
}
406+
return apiCall('/graphql', getNotificationsByUserIdQuery)
407+
},
408+
markNotificationAsRead: (notificationId, seenBy) => {
409+
const markNotificationAsReadQuery = {
410+
query: `mutation updateNotifications($seenBy: [ID]){
411+
updateUserStoryNotification(input: {
412+
where: {
413+
id: "${notificationId}"
414+
}
415+
data: {
416+
seenBy: $seenBy
417+
}
418+
}) {
419+
userStoryNotification {
420+
id
421+
}
422+
}
423+
}`,
424+
variables: {
425+
seenBy: seenBy
426+
}
427+
}
428+
return apiCall('/graphql', markNotificationAsReadQuery)
303429
}
304430
}
305431

0 commit comments

Comments
 (0)