Skip to content

Commit 0675010

Browse files
Linda PengLinda Peng
authored andcommitted
Refactor search and get resources page to use React Query
1 parent 359693d commit 0675010

File tree

3 files changed

+28
-41
lines changed

3 files changed

+28
-41
lines changed

src/components/Resources/index.js

Lines changed: 17 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,27 @@
1-
import React, { useState, useEffect } from 'react';
1+
import React, { useState } from 'react';
22
import PropTypes from 'prop-types';
3-
import axios from 'axios';
3+
import { useQuery } from 'react-query';
44
import PersonalMenu from '../PersonalMenu';
55
import Search from '../Search';
66
import { Grid, Typography } from '@material-ui/core';
77
import { ResourceCard } from './ResourceCard';
8-
import { buildQueryString } from '../../helpers';
8+
import { getResources } from '../../utils/queries';
99

10-
function Resources({ getResourcesUrl }) {
11-
const [resources, setResources] = useState([]);
10+
function Resources() {
1211
const [searchValue, setSearchValue] = useState('');
13-
const [loading, setLoading] = useState(true);
14-
const [errorMessage, setErrorMessage] = useState(null);
12+
const { isLoading, data, isError, error } = useQuery(
13+
['resource', searchValue],
14+
getResources
15+
);
1516

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]);
17+
if (isLoading) {
18+
return <p>Loading...</p>;
19+
}
2920

30-
// TODO: Refactor search function into its own file
3121
const search = searchValue => {
3222
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-
});
4423
};
4524

46-
const { count = 0, results } = resources;
47-
4825
return (
4926
<Grid container spacing={1}>
5027
<Grid item lg={3}>
@@ -56,20 +33,20 @@ function Resources({ getResourcesUrl }) {
5633
{searchValue && (
5734
<Typography>
5835
You have searched for "<strong>{searchValue}</strong>" and gotten
59-
<strong> {count}</strong> results.
36+
<strong> {data.count}</strong> results.
6037
</Typography>
6138
)}
6239
<br />
63-
{loading && !errorMessage ? (
40+
{isLoading && !isError ? (
6441
<span>loading...</span>
65-
) : errorMessage ? (
66-
<div className="errorMessage">{errorMessage}</div>
42+
) : error ? (
43+
<div className="errorMessage">{error}</div>
6744
) : (
6845
<Grid container spacing={1}>
69-
{resources.length === 0 ? (
46+
{data.results.length === 0 ? (
7047
<Typography>No resources found</Typography>
7148
) : (
72-
results.map(resource => (
49+
data.results.map(resource => (
7350
<Grid item lg={3} key={resource.guid}>
7451
<ResourceCard {...resource} />
7552
</Grid>

src/components/Resources/submitResource.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ const SubmitResource = () => {
8484
const classes = useStyles();
8585
const inputLabel = useRef(null);
8686
const [labelWidth, setLabelWidth] = useState(0);
87+
8788
useEffect(() => {
8889
setLabelWidth(inputLabel.current.offsetWidth);
8990
}, []);

src/utils/queries.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,13 @@ const getResource = async (_key, id) => {
77
return data;
88
};
99

10-
export { getResource };
10+
const getResources = async searchTerm => {
11+
const { data } = await axios.get(
12+
`${API_URL}/resources/?search=${searchTerm}`
13+
);
14+
console.log('get resources!!!!!');
15+
console.log(data);
16+
return data;
17+
};
18+
19+
export { getResource, getResources };

0 commit comments

Comments
 (0)