|
1 | | -import React, { useState, useEffect, useRef, useContext } from 'react' |
2 | | -import { apiURL } from '../config.json' |
3 | | -import { trackPromise, usePromiseTracker } from 'react-promise-tracker' |
| 1 | +import React, { useState, useContext } from 'react' |
4 | 2 | import { Helmet } from 'react-helmet' |
5 | 3 |
|
6 | | -import axios from 'axios' |
7 | | - |
8 | | -import LoadingIndicator from '../modules/LoadingIndicator' |
9 | 4 | import Button from '../components/Button' |
10 | | -import StoriesList from '../components/StoriesList' |
11 | 5 | import Navigation from '../components/Navigation' |
12 | | -import Dropdown from '../components/Dropdown' |
13 | 6 |
|
14 | | -import Lists from '../utils/Lists' |
15 | 7 | import Context from '../modules/Context' |
16 | 8 | import Login from './Login' |
17 | 9 |
|
18 | | -import userStory from '../services/user_story' |
| 10 | +import Stories from '../components/Stories' |
19 | 11 |
|
20 | 12 | const MyStories = () => { |
21 | 13 | const { state } = useContext(Context) |
22 | 14 |
|
23 | | - const [stories, setStories] = useState([]) |
24 | | - |
25 | 15 | const [currentStateSelected, selectState] = useState('My Submissions') |
26 | 16 |
|
27 | | - const [storyStateSelected, selectStoryState] = useState('Under consideration') |
28 | | - |
29 | | - const [product, setProduct] = useState('All') |
30 | | - const [sort, setSort] = useState('Most Voted') |
31 | | - const [category, setCategory] = useState('All') |
32 | | - |
33 | | - const productDropdownContainer = useRef() |
34 | | - const sortDropdownContainer = useRef() |
35 | | - const categoryDropdownContainer = useRef() |
36 | | - |
37 | | - const [categories, setCategories] = useState([]) |
38 | | - |
39 | | - const { promiseInProgress } = usePromiseTracker() |
40 | | - |
41 | 17 | const id = localStorage.getItem('id') |
42 | 18 |
|
43 | | - const [products, setProducts] = useState([]) |
44 | | - |
45 | | - useEffect(() => { |
46 | | - const comparatorVotes = (a, b) => { |
47 | | - return a.followers.length > b.followers.length ? -1 : 1 |
48 | | - } |
49 | | - const comparatorComments = (a, b) => { |
50 | | - return a.user_story_comments.length > b.user_story_comments.length |
51 | | - ? -1 |
52 | | - : 1 |
53 | | - } |
54 | | - |
55 | | - const updateStories = async () => { |
56 | | - if (sort === 'Most Voted') { |
57 | | - setStories(stories.sort(comparatorVotes)) |
58 | | - } |
59 | | - if (sort === 'Most Discussed') { |
60 | | - setStories(stories.sort(comparatorComments)) |
61 | | - } |
62 | | - } |
63 | | - trackPromise(updateStories()) |
64 | | - }, [sort, stories, setStories]) |
65 | | - |
66 | | - useEffect(() => { |
67 | | - const fetchMyStories = async () => { |
68 | | - const response = await axios.post( |
69 | | - `${apiURL}/graphql`, |
70 | | - { |
71 | | - query: `query { |
72 | | - userStories(where: { author: "${id}" }) { |
73 | | - id |
74 | | - Title |
75 | | - Description |
76 | | - followers { |
77 | | - id |
78 | | - username |
79 | | - } |
80 | | - user_story_comments { |
81 | | - Comments |
82 | | - } |
83 | | - product { |
84 | | - Name |
85 | | - } |
86 | | - Attachment { |
87 | | - id |
88 | | - url |
89 | | - } |
90 | | - author { |
91 | | - id |
92 | | - username |
93 | | - } |
94 | | - user_story_status { |
95 | | - Status |
96 | | - } |
97 | | - Category |
98 | | - } |
99 | | - }` |
100 | | - }, |
101 | | - { |
102 | | - withCredentials: true |
103 | | - } |
104 | | - ) |
105 | | - setStories(response.data.data.userStories) |
106 | | - } |
107 | | - const fetchFollowingStories = async () => { |
108 | | - const response = await axios.post( |
109 | | - `${apiURL}/graphql`, |
110 | | - { |
111 | | - query: `query { |
112 | | - userStories(where: { followers: "${id}" }) { |
113 | | - id |
114 | | - Title |
115 | | - Description |
116 | | - followers { |
117 | | - id |
118 | | - } |
119 | | - user_story_comments { |
120 | | - Comments |
121 | | - } |
122 | | - product { |
123 | | - Name |
124 | | - } |
125 | | - Attachment { |
126 | | - id |
127 | | - url |
128 | | - } |
129 | | - author { |
130 | | - id |
131 | | - username |
132 | | - } |
133 | | - user_story_status { |
134 | | - Status |
135 | | - } |
136 | | - } |
137 | | - }` |
138 | | - }, |
139 | | - { |
140 | | - withCredentials: true |
141 | | - } |
142 | | - ) |
143 | | - setStories(response.data.data.userStories) |
144 | | - } |
145 | | - if (currentStateSelected === 'My Submissions') |
146 | | - trackPromise(fetchMyStories()) |
147 | | - else trackPromise(fetchFollowingStories()) |
148 | | - }, [currentStateSelected, id]) |
149 | | - |
150 | | - useEffect(() => { |
151 | | - const fetchProducts = async () => { |
152 | | - const response = await userStory.getProducts() |
153 | | - setProducts( |
154 | | - response.data.data.products.map((ele) => { |
155 | | - return ele.Name |
156 | | - }) |
157 | | - ) |
158 | | - setProducts((products) => ['All', ...products]) |
159 | | - } |
160 | | - trackPromise(fetchProducts()) |
161 | | - }, [id]) |
162 | | - |
163 | | - useEffect(() => { |
164 | | - const fetchCategories = async () => { |
165 | | - const response = await userStory.getCategories() |
166 | | - |
167 | | - setCategories( |
168 | | - response.data.data.__type.enumValues.map((ele) => { |
169 | | - return ele.name |
170 | | - }) |
171 | | - ) |
172 | | - setCategories((categories) => ['All', ...categories]) |
173 | | - } |
174 | | - trackPromise(fetchCategories()) |
175 | | - }, []) |
176 | | - |
177 | 19 | return state.auth ? ( |
178 | 20 | <> |
179 | 21 | <Helmet> |
@@ -207,59 +49,11 @@ const MyStories = () => { |
207 | 49 | Following |
208 | 50 | </Button> |
209 | 51 | </div> |
210 | | - <div className='flex flex-row flex-space-between roadmap-one'> |
211 | | - {Lists.stateList && |
212 | | - Lists.stateList.map((state, key) => { |
213 | | - return ( |
214 | | - <Button |
215 | | - className={ |
216 | | - storyStateSelected === state.status |
217 | | - ? 'btn btn-tabs btn-tabs-selected' |
218 | | - : 'btn btn-tabs' |
219 | | - } |
220 | | - key={key} |
221 | | - onClick={() => selectStoryState(state.status)} |
222 | | - > |
223 | | - <i className='eos-icons'>{state.icon}</i> |
224 | | - {state.status} |
225 | | - </Button> |
226 | | - ) |
227 | | - })} |
228 | | - </div> |
229 | | - <div className='flex flex-row options-bar'> |
230 | | - <Dropdown |
231 | | - title='Product' |
232 | | - reference={productDropdownContainer} |
233 | | - curr={product} |
234 | | - setCurr={setProduct} |
235 | | - itemList={products} |
236 | | - /> |
237 | | - <Dropdown |
238 | | - title='Categories' |
239 | | - reference={categoryDropdownContainer} |
240 | | - curr={category} |
241 | | - setCurr={setCategory} |
242 | | - itemList={categories} |
243 | | - /> |
244 | | - <Dropdown |
245 | | - title='Sort By' |
246 | | - reference={sortDropdownContainer} |
247 | | - curr={sort} |
248 | | - setCurr={setSort} |
249 | | - itemList={Lists.sortByList} |
250 | | - /> |
251 | | - </div> |
252 | | - {promiseInProgress ? ( |
253 | | - <LoadingIndicator /> |
254 | | - ) : ( |
255 | | - <div className='flex flex-column'> |
256 | | - <StoriesList |
257 | | - stories={stories} |
258 | | - state={storyStateSelected} |
259 | | - product={product} |
260 | | - /> |
261 | | - </div> |
262 | | - )} |
| 52 | + |
| 53 | + <Stories |
| 54 | + authorId={currentStateSelected === 'My Submissions' ? id : null} |
| 55 | + followerId={currentStateSelected === 'Following' ? id : null} |
| 56 | + /> |
263 | 57 | </div> |
264 | 58 | </div> |
265 | 59 | </> |
|
0 commit comments