Skip to content

Commit 9383640

Browse files
committed
Refactored MyStories.js
1 parent ecabf4d commit 9383640

File tree

6 files changed

+36
-228
lines changed

6 files changed

+36
-228
lines changed

src/assets/scss/pages/profileRelated.scss

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,10 @@
66
margin: 16px 0px;
77
}
88

9-
.my-stories .flex-row {
10-
margin-bottom: 20px;
9+
.my-stories {
10+
.roadmap-one {
11+
margin-bottom: 20px;
12+
}
1113
}
1214

1315
.profile-picture {

src/components/Stories.js

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import SearchInput from '../modules/SearchInput'
1111
import Lists from '../utils/Lists'
1212
import userStory from '../services/user_story'
1313

14-
const Stories = ({ profileId }) => {
14+
const Stories = ({ authorId, followerId }) => {
1515
const [currentStateSelected, selectState] = useState('Under consideration')
1616

1717
const [page, setPage] = useState(1)
@@ -50,8 +50,6 @@ const Stories = ({ profileId }) => {
5050

5151
const [authorQuery, setAuthorQuery] = useState('')
5252

53-
const authorId = useRef(profileId)
54-
5553
const getPage = useCallback((page) => {
5654
setPage(page)
5755
}, [])
@@ -60,11 +58,12 @@ const Stories = ({ profileId }) => {
6058
const fetchStoryCount = async () => {
6159
const response = await userStory.getStoryCount(
6260
currentStateSelected,
63-
authorId.current,
61+
authorId,
6462
authorQuery,
6563
categoryQuery,
6664
productQuery,
67-
searchQuery
65+
searchQuery,
66+
followerId
6867
)
6968
setStoryCount(response.data.data.userStoriesConnection.aggregate.count)
7069
}
@@ -75,8 +74,9 @@ const Stories = ({ profileId }) => {
7574
categoryQuery,
7675
productQuery,
7776
searchQuery,
77+
authorQuery,
7878
authorId,
79-
authorQuery
79+
followerId
8080
])
8181

8282
useEffect(() => {
@@ -103,11 +103,12 @@ const Stories = ({ profileId }) => {
103103
const response = await userStory.getStories(
104104
page,
105105
currentStateSelected,
106-
authorId.current,
106+
authorId,
107107
authorQuery,
108108
categoryQuery,
109109
productQuery,
110-
searchQuery
110+
searchQuery,
111+
followerId
111112
)
112113
setStories(response.data.data.userStories)
113114
}
@@ -118,8 +119,9 @@ const Stories = ({ profileId }) => {
118119
page,
119120
productQuery,
120121
searchQuery,
122+
authorQuery,
121123
authorId,
122-
authorQuery
124+
followerId
123125
])
124126

125127
useEffect(() => {
@@ -212,13 +214,15 @@ const Stories = ({ profileId }) => {
212214
curr={product}
213215
setCurr={setProduct}
214216
itemList={products}
217+
data-cy='product-dropdown'
215218
/>
216219
<Dropdown
217220
title='Categories'
218221
reference={categoryDropdownContainer}
219222
curr={category}
220223
setCurr={setCategory}
221224
itemList={categories}
225+
data-cy='category-dropdown'
222226
/>
223227
<Dropdown
224228
title='Sort By'

src/modules/SearchInput.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ function SearchInput(props) {
4848
name='search'
4949
placeholder='Search'
5050
autoComplete='off'
51+
data-cy='search-input'
5152
value={fieldToSearch === 'Title' ? searchTerm : userTerm}
5253
onChange={(event) => {
5354
if (fieldToSearch === 'Title') {
@@ -91,6 +92,7 @@ function SearchInput(props) {
9192
curr={fieldToSearch}
9293
setCurr={setFieldToSearch}
9394
itemList={['Title', 'Author']}
95+
data-cy='toggle-title-dropdown'
9496
/>
9597
</div>
9698
<Button

src/pages/MyStories.js

Lines changed: 7 additions & 213 deletions
Original file line numberDiff line numberDiff line change
@@ -1,179 +1,21 @@
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'
42
import { Helmet } from 'react-helmet'
53

6-
import axios from 'axios'
7-
8-
import LoadingIndicator from '../modules/LoadingIndicator'
94
import Button from '../components/Button'
10-
import StoriesList from '../components/StoriesList'
115
import Navigation from '../components/Navigation'
12-
import Dropdown from '../components/Dropdown'
136

14-
import Lists from '../utils/Lists'
157
import Context from '../modules/Context'
168
import Login from './Login'
179

18-
import userStory from '../services/user_story'
10+
import Stories from '../components/Stories'
1911

2012
const MyStories = () => {
2113
const { state } = useContext(Context)
2214

23-
const [stories, setStories] = useState([])
24-
2515
const [currentStateSelected, selectState] = useState('My Submissions')
2616

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-
4117
const id = localStorage.getItem('id')
4218

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-
17719
return state.auth ? (
17820
<>
17921
<Helmet>
@@ -207,59 +49,11 @@ const MyStories = () => {
20749
Following
20850
</Button>
20951
</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+
/>
26357
</div>
26458
</div>
26559
</>

src/pages/Profile.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ const Profile = (props) => {
5252
)}
5353
<div className='flex flex-column'>
5454
<h3>Stories by this user</h3>
55-
<Stories profileId={profileId} />
55+
<Stories authorId={profileId} />
5656
</div>
5757
</div>
5858
</div>

0 commit comments

Comments
 (0)