Skip to content

Commit

Permalink
fix: fix new Map update
Browse files Browse the repository at this point in the history
  • Loading branch information
Benjamin Grilleres committed Oct 29, 2024
1 parent 1a3935b commit d0ee705
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 29 deletions.
36 changes: 10 additions & 26 deletions front/src/common/components/navigation/navigation.hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@ import { getNeighborsInfos } from "./navigation.utils"
* @returns An array containing all ids
*/
const mapToNeighbors = (map: Map<number, number[]>) =>
Array.from(map.entries())
.sort((a, b) => a[0] - b[0])
.reduce((acc, [, values]) => acc.concat(values), [] as number[])
[...map.entries()].sort((a, b) => a[0] - b[0]).flatMap(([, values]) => values)

/**
* Return an array of booleans to know if the current id has next/previous id
Expand Down Expand Up @@ -40,28 +38,19 @@ export function useNavigation(
hasPrevCurrentPage,
hasNextCurrentPage,
} = getNeighborsInfos(idsMap, current, currentPage, pageCount)
console.log("infos 1", {
idsMap,
current,
currentPage,
pageCount,
neighbors,
index,
isOut,
hasPrev,
hasNext,
res: JSON.stringify(idsMap.entries()),
})

const prev = useCallback(() => {
if (hasPrev && neighbors[index - 1]) {
const searchParams = new URLSearchParams(location.search)

const searchParamPage = searchParams.get("page")
// Change page query param when the next id is on a new page
if (searchParamPage && parseInt(searchParamPage) - 1 === 1) {
searchParams.delete("page")
} else if (!hasPrevCurrentPage && searchParamPage) {
searchParams.set("page", `${parseInt(searchParamPage) - 1}`)

// Update page query param when the current id is the last id of the current page
if (!hasPrevCurrentPage && searchParamPage) {
const prevPage = parseInt(searchParamPage) - 1

if (prevPage === 1) searchParams.delete("page")
else searchParams.set("page", prevPage.toString())
}

navigate({
Expand All @@ -75,12 +64,7 @@ export function useNavigation(
const next = useCallback(() => {
const searchParams = new URLSearchParams(location.search)
const searchParamPage = parseInt(searchParams.get("page") ?? "1")
console.log("infos 2", {
searchParams,
searchParamPage,
neighbors,
isOut,
})

if (isOut && neighbors[0]) {
// Remove page param to restart page
searchParams.delete("page")
Expand Down
6 changes: 3 additions & 3 deletions front/src/common/components/navigation/navigation.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,6 @@ const NavigationButtons = ({

return items
}
console.log("valeur de id map", JSON.stringify(idsMap.entries()))

const { loading } = useQuery(
// eslint-disable-next-line @typescript-eslint/no-unused-vars
(current: number) => fetchIdsForNavigation(page),
Expand All @@ -103,9 +101,11 @@ const NavigationButtons = ({
pageCount
)

// Fetch previous page only if the current id is first id of the current page and there is a previous page
if (infos.hasPrev && !infos.hasPrevCurrentPage) {
fetchIdsForNavigation(page - 1)
} else if (nav.hasNext && !infos.hasNextCurrentPage) {
} else if (infos.hasNext && !infos.hasNextCurrentPage) {
// Fetch next page only if the current id is last id of the current page and there is a next page
fetchIdsForNavigation(page + 1)
}
},
Expand Down

0 comments on commit d0ee705

Please sign in to comment.