|
1 |
| -import React, { useState } from 'react'; |
| 1 | +import React, { useEffect, useState } from 'react'; |
2 | 2 | import PropTypes from 'prop-types';
|
3 | 3 | import { useQuery } from 'react-query';
|
4 | 4 | import Search from '../../components/Search';
|
5 | 5 | import { Sidebar, Main } from '../pageSections';
|
6 | 6 | import { Grid, Typography } from '@material-ui/core';
|
| 7 | +import { Pagination } from '@material-ui/lab'; |
7 | 8 | import { ResourceCard } from './ResourceCard';
|
8 | 9 | import { getResources } from '../../utils/queries';
|
9 | 10 |
|
10 | 11 | function Resources() {
|
11 | 12 | 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 | + }); |
16 | 30 |
|
17 | 31 | if (isLoading) {
|
18 | 32 | return <p>Loading...</p>;
|
@@ -58,6 +72,18 @@ function Resources() {
|
58 | 72 | </Typography>
|
59 | 73 | )}
|
60 | 74 | <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 /> |
61 | 87 | {error && <div className="errorMessage">{error}</div>}
|
62 | 88 | {results && renderResults()}
|
63 | 89 | </Main>
|
|
0 commit comments