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

fix: pagination on commune page #44

Merged
merged 1 commit into from
Oct 17, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
18 changes: 16 additions & 2 deletions components/editable-list.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, {useState, useMemo} from 'react'
import React, {useState, useMemo, useEffect} from 'react'
import Pagination from 'react-js-pagination'

type EditableListProps<T> = {
Expand Down Expand Up @@ -32,6 +32,7 @@ export const EditableList = <T extends unknown>({
actions,
}: EditableListProps<T>) => {
const [filterInput, setFilterInput] = useState('')
const pageinationRef = React.createRef<HTMLDivElement>()

const filteredData = useMemo(() => {
if (!filterInput || !filter?.property) {
Expand All @@ -41,6 +42,19 @@ export const EditableList = <T extends unknown>({
return data.filter(item => item[filter.property].toLowerCase().includes(filterInput.toLowerCase()))
}, [data, filterInput, filter])

useEffect(() => {
if (pageinationRef.current) {
const paginationLinks = pageinationRef.current.querySelectorAll('.fr-pagination__link')
paginationLinks.forEach((link: HTMLAnchorElement, index) => {
if (index - 1 === page?.current) {
link.setAttribute('aria-current', 'page')
} else {
link.removeAttribute('aria-current')
}
})
}
}, [pageinationRef, page])

return (
<div className='fr-container fr-py-12v'>
{filter?.property && (
Expand Down Expand Up @@ -73,7 +87,7 @@ export const EditableList = <T extends unknown>({
) : <p>Aucun résultat</p>}
{(page !== undefined && filteredData.length > 0) && (
<div className='pagination fr-mx-auto fr-my-2w'>
<nav role='navigation' className='fr-pagination' aria-label='Pagination'>
<nav ref={pageinationRef} role='navigation' className='fr-pagination' aria-label='Pagination'>
<Pagination
activePage={page.current}
itemsCountPerPage={page.limit}
Expand Down
File renamed without changes.
29 changes: 17 additions & 12 deletions pages/communes/[code].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,19 +30,19 @@ const CommuneSource = (
const [balToDeleted, setBalToDeleted] = useState(null)

const [pageApiDepot, setPageApiDepot] = useState({
limit: 5,
count: initialRevisionsApiDepot.length,
limit: 10,
count: 0,
current: 1,
})

const [pageMoissonneur, setPageMoissonneur] = useState({
limit: 5,
count: initialRevisionsMoissonneur.length,
limit: 10,
count: 0,
current: 1,
})

const [pageMesAdresses, setPageMesAdresses] = useState({
limit: 5,
limit: 10,
count: balsPage.count,
current: 1,
})
Expand All @@ -59,25 +59,30 @@ const CommuneSource = (
setPageApiDepot(pageApiDepot => ({...pageApiDepot, current: newPage}))
}

const fetchBals = async (commune: string) => {
const fetchBals = useCallback(async (commune: string) => {
const res = await searchBasesLocales({commune, page: pageMesAdresses.current, limit: pageMesAdresses.limit})
setBals(res.results)
}
}, [pageMesAdresses])

useEffect(() => {
const fetchData = async () => {
const initialRevisionsApiDepot = await getAllRevisionByCommune(code)
const initialRevisionsMoissonneur = await getRevisionsByCommune(code)
setInitialRevisionsApiDepot(initialRevisionsApiDepot)
setInitialRevisionsMoissonneur(initialRevisionsMoissonneur)
const initialRevisionsApiDepot: RevisionApiDepotType[] = await getAllRevisionByCommune(code)
const initialRevisionsMoissonneur: RevisionMoissoneurType[] = await getRevisionsByCommune(code)

// Show the last revisions first
setInitialRevisionsApiDepot(initialRevisionsApiDepot.reverse())
setPageApiDepot(pageApiDepot => ({...pageApiDepot, count: initialRevisionsApiDepot.length}))

setInitialRevisionsMoissonneur(initialRevisionsMoissonneur.reverse())
setPageMoissonneur(pageMoissonneur => ({...pageMoissonneur, count: initialRevisionsMoissonneur.length}))
}

fetchData().catch(console.error)
}, [code])

useEffect(() => {
fetchBals(code).catch(console.error)
}, [pageMesAdresses, code])
}, [pageMesAdresses, code, fetchBals])

const revisionsApiDepot = useMemo(() => {
const start = (pageApiDepot.current - 1) * pageApiDepot.limit
Expand Down