|
| 1 | +import React, { useState, useEffect, useRef, useCallback } from 'react' |
| 2 | +import { trackPromise, usePromiseTracker } from 'react-promise-tracker' |
| 3 | + |
| 4 | +import Button from './Button' |
| 5 | +import StoriesList from './StoriesList' |
| 6 | +import Pagination from './Pagination' |
| 7 | +import Dropdown from './Dropdown' |
| 8 | +import LoadingIndicator from '../modules/LoadingIndicator' |
| 9 | +import SearchInput from '../modules/SearchInput' |
| 10 | + |
| 11 | +import Lists from '../utils/Lists' |
| 12 | +import userStory from '../services/user_story' |
| 13 | + |
| 14 | +const Stories = ({ authorId, followerId }) => { |
| 15 | + const [currentStateSelected, selectState] = useState('Under consideration') |
| 16 | + |
| 17 | + const [page, setPage] = useState(1) |
| 18 | + |
| 19 | + const [product, setProduct] = useState('All') |
| 20 | + |
| 21 | + const [sort, setSort] = useState('Most Voted') |
| 22 | + |
| 23 | + const [category, setCategory] = useState('All') |
| 24 | + |
| 25 | + const [products, setProducts] = useState([]) |
| 26 | + |
| 27 | + const [categories, setCategories] = useState([]) |
| 28 | + |
| 29 | + const [searchTerm, setSearchTerm] = useState('') |
| 30 | + |
| 31 | + const { promiseInProgress } = usePromiseTracker({ area: 'stories-div' }) |
| 32 | + |
| 33 | + const [storyCount, setStoryCount] = useState() |
| 34 | + |
| 35 | + const [stories, setStories] = useState([]) |
| 36 | + |
| 37 | + const productDropdownContainer = useRef() |
| 38 | + |
| 39 | + const sortDropdownContainer = useRef() |
| 40 | + |
| 41 | + const categoryDropdownContainer = useRef() |
| 42 | + |
| 43 | + const [productQuery, setProductQuery] = useState(``) |
| 44 | + |
| 45 | + const [categoryQuery, setCategoryQuery] = useState(``) |
| 46 | + |
| 47 | + const [searchQuery, setSearchQuery] = useState('') |
| 48 | + |
| 49 | + const [userTerm, setUserTerm] = useState('') |
| 50 | + |
| 51 | + const [authorQuery, setAuthorQuery] = useState('') |
| 52 | + |
| 53 | + const getPage = useCallback((page) => { |
| 54 | + setPage(page) |
| 55 | + }, []) |
| 56 | + |
| 57 | + useEffect(() => { |
| 58 | + const fetchStoryCount = async () => { |
| 59 | + const response = await userStory.getStoryCount( |
| 60 | + currentStateSelected, |
| 61 | + authorId, |
| 62 | + authorQuery, |
| 63 | + categoryQuery, |
| 64 | + productQuery, |
| 65 | + searchQuery, |
| 66 | + followerId |
| 67 | + ) |
| 68 | + setStoryCount(response.data.data.userStoriesConnection.aggregate.count) |
| 69 | + } |
| 70 | + fetchStoryCount() |
| 71 | + }, [ |
| 72 | + currentStateSelected, |
| 73 | + product, |
| 74 | + categoryQuery, |
| 75 | + productQuery, |
| 76 | + searchQuery, |
| 77 | + authorQuery, |
| 78 | + authorId, |
| 79 | + followerId |
| 80 | + ]) |
| 81 | + |
| 82 | + useEffect(() => { |
| 83 | + if (product !== 'All') { |
| 84 | + setProductQuery(`product : {Name: "${product}"}`) |
| 85 | + } else { |
| 86 | + setProductQuery(``) |
| 87 | + } |
| 88 | + if (category !== 'All') { |
| 89 | + setCategoryQuery(`Category : "${category}"`) |
| 90 | + } else { |
| 91 | + setCategoryQuery(``) |
| 92 | + } |
| 93 | + if (searchTerm === '') { |
| 94 | + setSearchQuery('') |
| 95 | + } |
| 96 | + if (userTerm === '') { |
| 97 | + setAuthorQuery('') |
| 98 | + } |
| 99 | + }, [product, category, searchTerm, userTerm]) |
| 100 | + |
| 101 | + useEffect(() => { |
| 102 | + const fetchStories = async () => { |
| 103 | + const response = await userStory.getStories( |
| 104 | + page, |
| 105 | + currentStateSelected, |
| 106 | + authorId, |
| 107 | + authorQuery, |
| 108 | + categoryQuery, |
| 109 | + productQuery, |
| 110 | + searchQuery, |
| 111 | + followerId |
| 112 | + ) |
| 113 | + setStories(response.data.data.userStories) |
| 114 | + } |
| 115 | + trackPromise(fetchStories(), 'stories-div') |
| 116 | + }, [ |
| 117 | + categoryQuery, |
| 118 | + currentStateSelected, |
| 119 | + page, |
| 120 | + productQuery, |
| 121 | + searchQuery, |
| 122 | + authorQuery, |
| 123 | + authorId, |
| 124 | + followerId |
| 125 | + ]) |
| 126 | + |
| 127 | + useEffect(() => { |
| 128 | + const fetchProducts = async () => { |
| 129 | + const response = await userStory.getProducts() |
| 130 | + return response.data.data.product !== null |
| 131 | + ? setProducts([ |
| 132 | + 'All', |
| 133 | + ...response.data.data.products?.map((ele) => { |
| 134 | + return ele.Name |
| 135 | + }) |
| 136 | + ]) |
| 137 | + : setProducts(['All']) |
| 138 | + } |
| 139 | + fetchProducts() |
| 140 | + }, []) |
| 141 | + |
| 142 | + useEffect(() => { |
| 143 | + const fetchCategories = async () => { |
| 144 | + const response = await userStory.getCategories() |
| 145 | + setCategories([ |
| 146 | + 'All', |
| 147 | + ...response.data.data.__type.enumValues.map((ele) => { |
| 148 | + return ele.name |
| 149 | + }) |
| 150 | + ]) |
| 151 | + } |
| 152 | + fetchCategories() |
| 153 | + }, []) |
| 154 | + |
| 155 | + useEffect(() => { |
| 156 | + const comparatorVotes = (a, b) => { |
| 157 | + return a.followers.length > b.followers.length ? -1 : 1 |
| 158 | + } |
| 159 | + const comparatorComments = (a, b) => { |
| 160 | + return a.user_story_comments.length > b.user_story_comments.length |
| 161 | + ? -1 |
| 162 | + : 1 |
| 163 | + } |
| 164 | + |
| 165 | + const updateStories = async () => { |
| 166 | + if (sort === 'Most Voted') { |
| 167 | + setStories(stories.sort(comparatorVotes)) |
| 168 | + } |
| 169 | + if (sort === 'Most Discussed') { |
| 170 | + setStories(stories.sort(comparatorComments)) |
| 171 | + } |
| 172 | + } |
| 173 | + trackPromise(updateStories(), 'stories-div') |
| 174 | + }, [sort, stories, setStories]) |
| 175 | + |
| 176 | + return ( |
| 177 | + <div> |
| 178 | + <div className='roadmap'> |
| 179 | + {Lists.stateList && |
| 180 | + Lists.stateList.map((state, key) => { |
| 181 | + return ( |
| 182 | + <Button |
| 183 | + className={ |
| 184 | + currentStateSelected === state.status |
| 185 | + ? 'btn btn-tabs btn-tabs-selected' |
| 186 | + : 'btn btn-tabs' |
| 187 | + } |
| 188 | + key={key} |
| 189 | + onClick={() => { |
| 190 | + selectState(state.status) |
| 191 | + setPage(1) |
| 192 | + }} |
| 193 | + > |
| 194 | + <i className='eos-icons'>{state.icon}</i> |
| 195 | + {state.status} |
| 196 | + </Button> |
| 197 | + ) |
| 198 | + })} |
| 199 | + </div> |
| 200 | + |
| 201 | + <div className='flex flex-row search-bar'> |
| 202 | + <SearchInput |
| 203 | + searchTerm={searchTerm} |
| 204 | + setSearchTerm={setSearchTerm} |
| 205 | + userTerm={userTerm} |
| 206 | + setUserTerm={setUserTerm} |
| 207 | + setSearchQuery={setSearchQuery} |
| 208 | + setAuthorQuery={setAuthorQuery} |
| 209 | + /> |
| 210 | + <div className='flex flex-row options-bar'> |
| 211 | + <Dropdown |
| 212 | + title='Product' |
| 213 | + reference={productDropdownContainer} |
| 214 | + curr={product} |
| 215 | + setCurr={setProduct} |
| 216 | + itemList={products} |
| 217 | + data-cy='product-dropdown' |
| 218 | + /> |
| 219 | + <Dropdown |
| 220 | + title='Categories' |
| 221 | + reference={categoryDropdownContainer} |
| 222 | + curr={category} |
| 223 | + setCurr={setCategory} |
| 224 | + itemList={categories} |
| 225 | + data-cy='category-dropdown' |
| 226 | + /> |
| 227 | + <Dropdown |
| 228 | + title='Sort By' |
| 229 | + reference={sortDropdownContainer} |
| 230 | + curr={sort} |
| 231 | + setCurr={setSort} |
| 232 | + itemList={Lists.sortByList} |
| 233 | + /> |
| 234 | + </div> |
| 235 | + </div> |
| 236 | + {promiseInProgress ? ( |
| 237 | + <LoadingIndicator /> |
| 238 | + ) : ( |
| 239 | + <div className='stories-div'> |
| 240 | + <StoriesList |
| 241 | + stories={stories} |
| 242 | + state={currentStateSelected} |
| 243 | + product={product} |
| 244 | + /> |
| 245 | + </div> |
| 246 | + )} |
| 247 | + <Pagination |
| 248 | + getPage={getPage} |
| 249 | + storyCount={storyCount} |
| 250 | + status={currentStateSelected} |
| 251 | + product={product} |
| 252 | + /> |
| 253 | + </div> |
| 254 | + ) |
| 255 | +} |
| 256 | + |
| 257 | +export default Stories |
0 commit comments