|
| 1 | +import React, { useState } from 'react'; |
| 2 | +import PropTypes from 'prop-types'; |
| 3 | +import { useQuery } from 'react-query'; |
| 4 | +import PersonalMenu from '../PersonalMenu'; |
| 5 | +import Search from '../Search'; |
| 6 | +import { Grid, Typography } from '@material-ui/core'; |
| 7 | +import { Pagination } from '@material-ui/lab'; |
| 8 | +import { ResourceCard } from './ResourceCard'; |
| 9 | +import { getResources } from '../../utils/queries'; |
| 10 | + |
| 11 | +function Resources() { |
| 12 | + const [searchValue, setSearchValue] = useState(''); |
| 13 | + const [goToPage, setGoToPage] = useState(); |
| 14 | + const [isPagination, triggerPagination] = useState(); |
| 15 | + let params; |
| 16 | + |
| 17 | + if (isPagination) { |
| 18 | + params = `?page=${goToPage}`; |
| 19 | + } else { |
| 20 | + params = `?search=${searchValue}`; |
| 21 | + } |
| 22 | + |
| 23 | + const { isLoading, data, error } = useQuery([params], getResources); |
| 24 | + |
| 25 | + if (isLoading) { |
| 26 | + return <p>Loading...</p>; |
| 27 | + } |
| 28 | + |
| 29 | + const search = searchValue => { |
| 30 | + if (searchValue.length === 0) { |
| 31 | + setSearchValue(); |
| 32 | + } |
| 33 | + triggerPagination(false); |
| 34 | + setSearchValue(searchValue); |
| 35 | + }; |
| 36 | + |
| 37 | + const { results, count } = data; |
| 38 | + |
| 39 | + const renderResults = () => { |
| 40 | + return ( |
| 41 | + <Grid container spacing={1}> |
| 42 | + {results.length === 0 ? ( |
| 43 | + <Grid item lg={9}> |
| 44 | + <Typography>No resources found</Typography> |
| 45 | + </Grid> |
| 46 | + ) : ( |
| 47 | + results.map(resource => ( |
| 48 | + <Grid item lg={3} key={resource.guid}> |
| 49 | + <ResourceCard {...resource} /> |
| 50 | + </Grid> |
| 51 | + )) |
| 52 | + )} |
| 53 | + </Grid> |
| 54 | + ); |
| 55 | + }; |
| 56 | + |
| 57 | + return ( |
| 58 | + <Grid container spacing={1}> |
| 59 | + <Grid item lg={3}> |
| 60 | + <PersonalMenu /> |
| 61 | + </Grid> |
| 62 | + <Grid item lg={9}> |
| 63 | + <h2>Resources</h2> |
| 64 | + <Search label="Search resources" search={search} /> |
| 65 | + {searchValue && ( |
| 66 | + <Typography> |
| 67 | + You have searched for "<strong>{searchValue}</strong>" and gotten |
| 68 | + <strong> {count}</strong> results. |
| 69 | + </Typography> |
| 70 | + )} |
| 71 | + <br /> |
| 72 | + <Pagination |
| 73 | + variant="outlined" |
| 74 | + shape="rounded" |
| 75 | + size="large" |
| 76 | + count={Math.floor(count / 10)} |
| 77 | + page={goToPage || 1} |
| 78 | + onChange={(_, page) => { |
| 79 | + setGoToPage(page); |
| 80 | + triggerPagination(true); |
| 81 | + }} |
| 82 | + /> |
| 83 | + <br /> |
| 84 | + {error && <div className="errorMessage">{error}</div>} |
| 85 | + {results && renderResults()} |
| 86 | + </Grid> |
| 87 | + </Grid> |
| 88 | + ); |
| 89 | +} |
| 90 | + |
| 91 | +Resources.propTypes = { |
| 92 | + getResourcesUrl: PropTypes.string, |
| 93 | +}; |
| 94 | + |
| 95 | +export default Resources; |
0 commit comments