Skip to content

Commit

Permalink
Merge branch 'develop' into isoler-filter
Browse files Browse the repository at this point in the history
  • Loading branch information
Stein-Kato authored Sep 20, 2024
2 parents 4582ac5 + cfd6d30 commit e1092e1
Showing 1 changed file with 19 additions and 14 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { PropsWithChildren, useEffect, useState } from "react";
import { useRouter } from "next/router";
import {
ArrayParam,
DelimitedArrayParam,
Expand Down Expand Up @@ -106,10 +107,19 @@ export function TreatmentQualityFilterMenu({
? 5
: 10;

const [mounted, setMounted] = useState(false);
// When the user navigates to the page, it may contain query parameters for
// filtering indicators. Use NextRouter to get the current path containing the
// initial query parameters.
const router = useRouter();
// Next's prerender stage causes problems for the initial values given to
// useReducer, because they are only set once by the reducer and are missing
// during Next's prerender stage. Tell FilterMenu to refresh its state during
// the first call after the prerender is done.
const [prevReady, setPrevReady] = useState(router.isReady);
const prerenderFinished = prevReady !== router.isReady;
useEffect(() => {
setMounted(true);
}, []);
setPrevReady(router.isReady);
}, [router.isReady]);

// Map for filter options, defaults, and query parameter values and setters
const optionsMap = new Map<string, OptionsMapEntry>();
Expand Down Expand Up @@ -196,17 +206,14 @@ export function TreatmentQualityFilterMenu({
queryContext.type,
);

const [previouslyNotFetched, setPreviouslyNotFetched] = useState(
!unitNamesQuery.isFetched,
const [prevApiQueryLoading, setPrevApiQueryLoading] = useState(
unitNamesQuery.isLoading,
);

// Refresh initial state if query is fetched and was previously not fetched
const shouldRefreshInitialState =
unitNamesQuery.isFetched && previouslyNotFetched;
const apiQueriesCompleted = prevApiQueryLoading && !unitNamesQuery.isLoading;

useEffect(() => {
setPreviouslyNotFetched(!unitNamesQuery.isFetched);
}, [unitNamesQuery.isFetched]);
setPrevApiQueryLoading(unitNamesQuery.isLoading);
}, [unitNamesQuery.isLoading]);

const treatmentUnits = getTreatmentUnitsTree(unitNamesQuery);

Expand Down Expand Up @@ -354,9 +361,7 @@ export function TreatmentQualityFilterMenu({
return valueLabel;
};

if (!mounted) {
return null;
}
const shouldRefreshInitialState = prerenderFinished || apiQueriesCompleted;

if (register) {
return (
Expand Down

0 comments on commit e1092e1

Please sign in to comment.