Skip to content

Commit 851c640

Browse files
Merge pull request #34 from EOS-uiux-Solutions/refactor-services
Refactor GraphQL queries and mutations for Home.js
2 parents fc26519 + 6ecf608 commit 851c640

File tree

9 files changed

+226
-159
lines changed

9 files changed

+226
-159
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*

README.md

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,34 @@
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-
[![Website shields.io](https://img.shields.io/website-up-down-green-red/http/shields.io.svg)](https://suse-uiux.gitlab.io/eos-feature-request/)
3+
# User Story
44

5-
# EOS Feature Request
5+
![](./static/user_story.png)
6+
# Introduction
67

7-
# Guideline to develop this site
8+
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**.
89

9-
In the project directory, you can run:
10+
# How it works
1011

11-
### Starting up the project
12+
![](./static/user_story_workflow.png)
13+
14+
# Guidelines for development
15+
### Setting up the project
1216

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

15-
There are two options to set up the `backend` for this repository:
19+
User Story uses [this repository](https://github.com/EOS-uiux-Solutions/strapi) as its `backend`. There are two ways to set up the `backend`:
1620

17-
1. If you are planning to work only on the UI/UX of `eos-user-story`, then follow along. Skip to point 2 if you need to setup backend as well. For setting up using `docker`, first make sure you have `docker` & `docker-compose` installed ([Install instructions](https://docs.docker.com/engine/install/)). Next, run `docker-compose up` and wait for the server to start on `http://localhost:1337`. Visit [http://localhost:1337/admin/auth/](http://localhost:1337/admin/auth/) and create the admin user for Strapi. Then set the roles and permissions for the api endpoints. That's it, the backend is up and running. Proceed to the next paragraph for further instructions.
21+
- You can use Docker to setup backend by following [these instructions](https://github.com/EOS-uiux-Solutions/strapi#using-docker) If you are planning to work only on the `frontend` of User Story, then follow along.
1822

19-
2. This repository uses [EOS-uiux-Solutions/strapi](https://github.com/EOS-uiux-Solutions/strapi) as the backend Strapi server. Follow [these](https://github.com/EOS-uiux-Solutions/strapi#how-to-install-it) instructions to set it up.
23+
- You can setup `backend` locally by following [these instructions](https://github.com/EOS-uiux-Solutions/strapi#locally).
2024

21-
After setting up the backend, you need to connect the frontend to the Strapi server. To do this, duplicate the file `src/config.json.template` into a new file called `src/config.json` and replace the content of the variable with your server URL.
25+
If you want to work only on the `frontend` then go to `src/config.json` and change the value of `apiURL` to `https://userstory-strapi.herokuapp.com`.
2226

23-
Then run: `npm start` to run the app in the development mode.
27+
Then run: `npm start` to run the app in development mode.
2428

2529
Open [http://localhost:3000](http://localhost:3000) to view it in the browser.
2630

27-
The page will reload if you make edits.<br />
28-
You will also see any lint errors in the console.
31+
The page will reload if you make edits. You will also see any lint errors in the console.
2932

3033
### JS and SCSS quality assurance
3134

@@ -38,10 +41,10 @@ To make sure your SCSS is compliant run: `npm run test:scss`
3841

3942
`npm run build`
4043

41-
Builds the app for production to the `build` folder.<br />
42-
It correctly bundles React in production mode and optimizes the build for the best performance.
44+
Builds the app for production to the `build` folder. It correctly bundles React in production mode and optimizes the build for the best performance.
4345

4446
The build is minified and the filenames include the hashes.<br />
47+
4548
Your app is ready to be deployed!
4649

4750
See the section about [deployment](https://facebook.github.io/create-react-app/docs/deployment) for more information.
@@ -50,7 +53,7 @@ See the section about [deployment](https://facebook.github.io/create-react-app/d
5053

5154
### Code contributions
5255

53-
1. Open a new or pick an open issue from the issue list and claim it in the comments. Make sure that the issue is confirmed so you don't work on something that will not be approved to be merged.
56+
1. Open a new issue or pick an open issue from the issue list and claim it in the comments. Make sure that the issue is confirmed so you don't work on something that will not be approved to be merged.
5457
2. Make sure you follow our best practices: [refer to our Wiki](https://gitlab.com/SUSE-UIUX/eos/-/wikis/home). You'll find information on writing code, how to name a branch, how we release, etc.
5558
3. Join Slack [optional] to get in touch with the maintainers if you have any doubt: [join slack](http://slack.eosdesignsystem.com/)
5659
4. Make sure you fork the project, cloning it will not give you the right access to open a PR/MR. [How to open a PR in open source](https://gitlab.com/SUSE-UIUX/eos/-/wikis/Basic-git-instructions-for-beginners)
@@ -59,7 +62,7 @@ See the section about [deployment](https://facebook.github.io/create-react-app/d
5962

6063
If you'd like to contribute with design changes, you'll have to do as follows:
6164

62-
1. [Open an issue](https://gitlab.com/SUSE-UIUX/eos-feature-request/-/issues/new)
65+
1. [Open an issue](https://github.com/EOS-uiux-Solutions/user-story/issues/new)
6366
2. Add all relevant information to the issue. Please be as descriptive as possible. Add links to references, images, videos, etc.
6467
3. While working on your design, please make sure you follow our [design guidelines](https://gitlab.com/SUSE-UIUX/eos/wikis/Design-process-and-feedback-gathering).
6568
4. Once the issue is approved by us, it'll be ready to be implemented.

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)