Skip to content

Commit 0c0caf4

Browse files
pdotsanilpatmo
authored andcommitted
Added pagination component with useQuery call connected
1 parent 99255b3 commit 0c0caf4

File tree

1 file changed

+31
-5
lines changed

1 file changed

+31
-5
lines changed

src/pages/Resources/Resources.js

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,32 @@
1-
import React, { useState } from 'react';
1+
import React, { useEffect, useState } from 'react';
22
import PropTypes from 'prop-types';
33
import { useQuery } from 'react-query';
44
import Search from '../../components/Search';
55
import { Sidebar, Main } from '../pageSections';
66
import { Grid, Typography } from '@material-ui/core';
7+
import { Pagination } from '@material-ui/lab';
78
import { ResourceCard } from './ResourceCard';
89
import { getResources } from '../../utils/queries';
910

1011
function Resources() {
1112
const [searchValue, setSearchValue] = useState('');
12-
const { isLoading, data, error } = useQuery(
13-
[`?search=${searchValue}`],
14-
getResources
15-
);
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+
useEffect(() => {
26+
return () => {
27+
triggerPagination(false);
28+
};
29+
});
1630

1731
if (isLoading) {
1832
return <p>Loading...</p>;
@@ -58,6 +72,18 @@ function Resources() {
5872
</Typography>
5973
)}
6074
<br />
75+
<Pagination
76+
variant="outlined"
77+
shape="rounded"
78+
size="large"
79+
count={Math.floor(count / 10)}
80+
onChange={(_, page) => {
81+
console.log(page);
82+
setGoToPage(page);
83+
triggerPagination(true);
84+
}}
85+
/>
86+
<br />
6187
{error && <div className="errorMessage">{error}</div>}
6288
{results && renderResults()}
6389
</Main>

0 commit comments

Comments
 (0)