Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes PaginationControl upper page limit is always 100 #151

Merged
merged 6 commits into from
Oct 7, 2021
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 18 additions & 7 deletions frontend/src/components/Home.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ export default function Home() {

const [page, setPage] = useState(1);
const [perpage, setPerpage] = useState(10);
const [maxpages, setMaxPages] = useState(100);
const [search, setSearch] = useState('');

const [leads, setLeads] = useState([]);

const history = useHistory();
Expand All @@ -83,25 +83,36 @@ export default function Home() {

useEffect(() => {
let url = `${API_HOST}/leads?page=${page}&perpage=${perpage}`;
const n_pagesUrl = `${API_HOST}/leads/n_pages`;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you check the swagger docs, there is a perpage query parameter to /leads/n_pages. It should save you from redoing the math that is done on the backend.

Suggested change
const n_pagesUrl = `${API_HOST}/leads/n_pages`;
const n_pagesUrl = `${API_HOST}/leads/n_pages?perpage=${perpage}`;

if (search) {
url += `&search=${search}`;
}
const token = localStorage.getItem('partnerFinderToken');
if (!token) {
history.push('/login');
}
const headers = {
'Content-Type': 'application/json',
Authorization: `Bearer ${token}`,
};
fetch(url, {
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${token}`,
},
headers: headers,
})
.then((response) => checkForErrors(response))
.then((data) => setLeads(data.leads))
// TODO: create state for error and set state instead of just console.error
// conditional rendering if there is an error
.catch((error) => console.error(error.message));
}, [page, perpage, search]);

fetch(n_pagesUrl, {
headers: headers,
})
.then((response) => checkForErrors(response))
.then((data) =>
setMaxPages(Math.ceil((data.pages * data.query.perpage) / perpage))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This math is already done on the backend. See my first comment.

Suggested change
setMaxPages(Math.ceil((data.pages * data.query.perpage) / perpage))
setMaxPages(data.pages)

)
.catch((error) => console.error(error.message));
}, [page, perpage, maxpages, search]);

return (
<div id="home">
Expand Down Expand Up @@ -132,7 +143,7 @@ export default function Home() {
<PaginationControl
page={page}
perpage={perpage}
maxpages={100}
maxpages={maxpages}
setPage={setPage}
setPerpage={setPerpage}
/>
Expand Down
5 changes: 4 additions & 1 deletion frontend/src/components/PaginationControl.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { DEBOUNCE_TIME_MS } from '../constants';
const useStyles = makeStyles((theme) => ({
paginationTextField: {
width: '115px',
marginLeft: '5px',
},
}));

Expand Down Expand Up @@ -66,7 +67,9 @@ export default function PaginationControl({
<Typography>
{page} / {maxpages}
</Typography>
<Button onClick={() => setPage(page + 1 <= 100 ? page + 1 : 100)}>
<Button
onClick={() => setPage(page + 1 <= maxpages ? page + 1 : maxpages)}
>
<ChevronRightIcon></ChevronRightIcon>
</Button>
</Fragment>
Expand Down