-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Flyttet kode for å sjekke om api-queries og query-params er klare (#3187
) * Flyttet kode for å sjekke om api-queries og query-params er klare * fjernet gamle kommentarer * endret hook-navn fordi den skal bli true kun hvis query-params og api-kall ikke allerede var ferdig i utgangspunktet * flyttet eslint-kommentar pga prettier endret linje-breaks * la til en forklarende kommentar til hooken * added important detail to comment * fixed format * endret filnavnet * oppdaterte import * change variable name to make its meaning clearer * endret flere variablenavn til mer forklarende navn * endret funksjonsnavn til mer forklarende * changing the return a bit * Bedre kommentar til funksjon * format --------- Co-authored-by: jo-inge-arnes <[email protected]>
- Loading branch information
1 parent
96db356
commit ddcba4c
Showing
2 changed files
with
56 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
packages/qmongjs/src/helpers/hooks/useShouldReinitialize.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import { UseQueryResult } from "@tanstack/react-query"; | ||
import { useRouter } from "next/router"; | ||
import { useEffect, useState } from "react"; | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
const anyQueriesLoading = (queries: UseQueryResult<any, unknown>[]) => { | ||
return queries.some((query) => query.isLoading); | ||
}; | ||
|
||
/** | ||
* | ||
* The variable from useShouldReinitialize becomes true either: | ||
* 1) The first time the page has finished "pre-rendering" and the API calls have already finished loading. At this point, the query/get parameters from the URL are also ready. | ||
* 2) The first time the API calls finish loading and the page is already "pre-rendered". That is, pre-rendering finishes first and then the API calls. At this point, the query/get parameters from the URL are also ready. | ||
* | ||
* @param queries An array of tanstack queries of type UseQueryResults | ||
* @returns True the first time the page is finished hydrated and all queries have finished loading | ||
*/ | ||
export default function useShouldReinitialize( | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
queries: UseQueryResult<any, unknown>[], | ||
) { | ||
const router = useRouter(); | ||
|
||
const [previousPrerenderingStatus, setPreviousPrerenderingStatus] = useState( | ||
router.isReady, | ||
); | ||
|
||
const prerenderingJustCompleted = | ||
previousPrerenderingStatus !== router.isReady; | ||
|
||
useEffect(() => { | ||
setPreviousPrerenderingStatus(router.isReady); | ||
}, [router.isReady]); | ||
|
||
const areQueriesStillLoading = anyQueriesLoading(queries); | ||
|
||
const [previousQueryLoadingStatus, setPreviousQueryLoadingStatus] = useState( | ||
areQueriesStillLoading, | ||
); | ||
|
||
const queriesJustCompleted = | ||
previousQueryLoadingStatus && !anyQueriesLoading(queries); | ||
|
||
useEffect(() => { | ||
setPreviousQueryLoadingStatus(areQueriesStillLoading); | ||
}, [areQueriesStillLoading]); | ||
|
||
return ( | ||
(prerenderingJustCompleted && !areQueriesStillLoading) || | ||
queriesJustCompleted | ||
); | ||
} |