@@ -5,9 +5,7 @@ import React, {
55 useCallback ,
66 useContext
77} from 'react'
8- import axios from 'axios'
98import { Link } from '@reach/router'
10- import { apiURL } from '../config.json'
119import { trackPromise , usePromiseTracker } from 'react-promise-tracker'
1210import { Helmet } from 'react-helmet'
1311
@@ -23,6 +21,7 @@ import UsersSuggestionDropdown from '../components/UsersSuggestionDropdown'
2321import Lists from '../utils/Lists'
2422import useAuth from '../hooks/useAuth'
2523import Context from '../modules/Context'
24+ import userStory from '../services/user_story'
2625
2726const 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
0 commit comments