1
- import React , { useState , useEffect } from 'react' ;
1
+ import React , { useState } from 'react' ;
2
2
import PropTypes from 'prop-types' ;
3
- import axios from 'axios ' ;
3
+ import { useQuery } from 'react-query ' ;
4
4
import PersonalMenu from '../PersonalMenu' ;
5
5
import Search from '../Search' ;
6
6
import { Grid , Typography } from '@material-ui/core' ;
7
7
import { ResourceCard } from './ResourceCard' ;
8
- import { buildQueryString } from '../../helpers ' ;
8
+ import { getResources } from '../../utils/queries ' ;
9
9
10
- function Resources ( { getResourcesUrl } ) {
11
- const [ resources , setResources ] = useState ( [ ] ) ;
10
+ function Resources ( ) {
12
11
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
+ ) ;
15
16
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
+ }
29
20
30
- // TODO: Refactor search function into its own file
31
21
const search = searchValue => {
32
22
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
23
} ;
45
24
46
- const { count = 0 , results } = resources ;
47
-
48
25
return (
49
26
< Grid container spacing = { 1 } >
50
27
< Grid item lg = { 3 } >
@@ -56,20 +33,20 @@ function Resources({ getResourcesUrl }) {
56
33
{ searchValue && (
57
34
< Typography >
58
35
You have searched for "< strong > { searchValue } </ strong > " and gotten
59
- < strong > { count } </ strong > results.
36
+ < strong > { data . count } </ strong > results.
60
37
</ Typography >
61
38
) }
62
39
< br />
63
- { loading && ! errorMessage ? (
40
+ { isLoading && ! isError ? (
64
41
< span > loading...</ span >
65
- ) : errorMessage ? (
66
- < div className = "errorMessage" > { errorMessage } </ div >
42
+ ) : error ? (
43
+ < div className = "errorMessage" > { error } </ div >
67
44
) : (
68
45
< Grid container spacing = { 1 } >
69
- { resources . length === 0 ? (
46
+ { data . results . length === 0 ? (
70
47
< Typography > No resources found</ Typography >
71
48
) : (
72
- results . map ( resource => (
49
+ data . results . map ( resource => (
73
50
< Grid item lg = { 3 } key = { resource . guid } >
74
51
< ResourceCard { ...resource } />
75
52
</ Grid >
0 commit comments