Skip to content

Commit 9eb55f8

Browse files
pdotsanilpatmo
authored andcommitted
tested on mock data, working without useeffect
1 parent 0c0caf4 commit 9eb55f8

File tree

2 files changed

+98
-8
lines changed

2 files changed

+98
-8
lines changed

src/components/Resources/Resources.js

+95
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
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;

src/pages/Resources/Resources.js

+3-8
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { useEffect, useState } from 'react';
1+
import React, { useState } from 'react';
22
import PropTypes from 'prop-types';
33
import { useQuery } from 'react-query';
44
import Search from '../../components/Search';
@@ -22,12 +22,6 @@ function Resources() {
2222

2323
const { isLoading, data, error } = useQuery([params], getResources);
2424

25-
useEffect(() => {
26-
return () => {
27-
triggerPagination(false);
28-
};
29-
});
30-
3125
if (isLoading) {
3226
return <p>Loading...</p>;
3327
}
@@ -36,6 +30,7 @@ function Resources() {
3630
if (searchValue.length === 0) {
3731
setSearchValue();
3832
}
33+
triggerPagination(false);
3934
setSearchValue(searchValue);
4035
};
4136

@@ -77,8 +72,8 @@ function Resources() {
7772
shape="rounded"
7873
size="large"
7974
count={Math.floor(count / 10)}
75+
page={goToPage || 1}
8076
onChange={(_, page) => {
81-
console.log(page);
8277
setGoToPage(page);
8378
triggerPagination(true);
8479
}}

0 commit comments

Comments
 (0)