|
1 | | -import React, { useState, useEffect } from 'react'; |
2 | | -import PropTypes from 'prop-types'; |
3 | | -import axios from 'axios'; |
4 | | -import PersonalMenu from '../PersonalMenu'; |
5 | | -import Search from '../Search'; |
6 | | -import { Grid, Typography } from '@material-ui/core'; |
| 1 | +import Resources from './Resources'; |
7 | 2 | import { ResourceCard } from './ResourceCard'; |
8 | | -import { buildQueryString } from '../../helpers'; |
9 | | - |
10 | | -function Resources({ getResourcesUrl }) { |
11 | | - const [resources, setResources] = useState([]); |
12 | | - const [searchValue, setSearchValue] = useState(''); |
13 | | - const [loading, setLoading] = useState(true); |
14 | | - const [errorMessage, setErrorMessage] = useState(null); |
15 | | - |
16 | | - useEffect(() => { |
17 | | - axios |
18 | | - .get(getResourcesUrl) |
19 | | - .then(function(response) { |
20 | | - // handle success |
21 | | - setResources(response.data); |
22 | | - setLoading(false); |
23 | | - }) |
24 | | - .catch(function(error) { |
25 | | - // handle error |
26 | | - console.log(error); |
27 | | - }); |
28 | | - }, [getResourcesUrl]); |
29 | | - |
30 | | - // TODO: Refactor search function into its own file |
31 | | - const search = searchValue => { |
32 | | - setSearchValue(searchValue); |
33 | | - setLoading(true); |
34 | | - setErrorMessage(null); |
35 | | - axios |
36 | | - .get(buildQueryString(getResourcesUrl, searchValue)) |
37 | | - .then(function(response) { |
38 | | - setResources(response.data); |
39 | | - setLoading(false); |
40 | | - }) |
41 | | - .catch(function(error) { |
42 | | - console.log(error); |
43 | | - }); |
44 | | - }; |
45 | | - |
46 | | - const { count = 0, results } = resources; |
47 | | - |
48 | | - return ( |
49 | | - <Grid container spacing={1}> |
50 | | - <Grid item lg={3}> |
51 | | - <PersonalMenu /> |
52 | | - </Grid> |
53 | | - <Grid item lg={9}> |
54 | | - <h2>Resources</h2> |
55 | | - <Search label="Search resources" search={search} /> |
56 | | - {searchValue && ( |
57 | | - <Typography> |
58 | | - You have searched for "<strong>{searchValue}</strong>" and gotten |
59 | | - <strong> {count}</strong> results. |
60 | | - </Typography> |
61 | | - )} |
62 | | - <br /> |
63 | | - {loading && !errorMessage ? ( |
64 | | - <span>loading...</span> |
65 | | - ) : errorMessage ? ( |
66 | | - <div className="errorMessage">{errorMessage}</div> |
67 | | - ) : ( |
68 | | - <Grid container spacing={1}> |
69 | | - {resources.length === 0 ? ( |
70 | | - <Typography>No resources found</Typography> |
71 | | - ) : ( |
72 | | - results.map(resource => ( |
73 | | - <Grid item lg={3} key={resource.guid}> |
74 | | - <ResourceCard {...resource} /> |
75 | | - </Grid> |
76 | | - )) |
77 | | - )} |
78 | | - </Grid> |
79 | | - )} |
80 | | - </Grid> |
81 | | - </Grid> |
82 | | - ); |
83 | | -} |
84 | | - |
85 | | -Resources.propTypes = { |
86 | | - getResourcesUrl: PropTypes.string, |
87 | | -}; |
88 | 3 |
|
89 | 4 | export default Resources; |
| 5 | +export { ResourceCard }; |
0 commit comments