From c629a75466037de6141f1735f257b87a95f37673 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9my=20Rialland?= Date: Wed, 7 Jun 2023 16:56:45 +0200 Subject: [PATCH] Fix async bug when click on next button --- .../_components/Navigation.tsx | 54 +++++++++++++------ .../choix-du-statut/détails-activité.tsx | 8 ++- .../choix-du-statut/recherche-activité.tsx | 9 ++-- 3 files changed, 49 insertions(+), 22 deletions(-) diff --git a/site/source/pages/assistants/choix-du-statut/_components/Navigation.tsx b/site/source/pages/assistants/choix-du-statut/_components/Navigation.tsx index 9b963da6d..08b40da21 100644 --- a/site/source/pages/assistants/choix-du-statut/_components/Navigation.tsx +++ b/site/source/pages/assistants/choix-du-statut/_components/Navigation.tsx @@ -4,9 +4,12 @@ import styled from 'styled-components' import { Button } from '@/design-system/buttons' import { Grid } from '@/design-system/layout' -import { useSitePaths } from '@/sitePaths' +import { RelativeSitePaths, useSitePaths } from '@/sitePaths' -export const stepOrder = [ +type ChoixStatut = RelativeSitePaths['assistants']['choix-du-statut'] +type Step = keyof ChoixStatut + +export const stepOrder: readonly Step[] = [ 'recherche-activité', 'détails-activité', 'département', @@ -17,8 +20,6 @@ export const stepOrder = [ 'résultat', ] as const -type Step = (typeof stepOrder)[number] - function useCurrentStep() { const { relativeSitePaths, absoluteSitePaths } = useSitePaths() const localizedStep = useMatch( @@ -28,17 +29,18 @@ function useCurrentStep() { return null } - const currentStep = Object.entries( + const entries = Object.entries( relativeSitePaths.assistants['choix-du-statut'] - ).find( - ([, value]) => value === localizedStep - )?.[0] as keyof (typeof relativeSitePaths.assistants)['choix-du-statut'] + ) as [Step, ChoixStatut[Step]][] - if (!stepOrder.includes(currentStep as Step)) { + const [currentStep] = + entries.find(([, value]) => value === localizedStep) ?? [] + + if (!currentStep || !stepOrder.includes(currentStep)) { return null } - return currentStep as Step + return currentStep } export default function Navigation({ @@ -48,7 +50,7 @@ export default function Navigation({ }: { currentStepIsComplete: boolean nextStepLabel?: false | string - onNextStep?: () => void + onNextStep?: (next: { nextStep: string; nextAbsolutePath: string }) => void }) { const { t } = useTranslation() const { absoluteSitePaths } = useSitePaths() @@ -57,8 +59,10 @@ export default function Navigation({ if (!currentStep) { return null } - const nextStep = stepOrder[stepOrder.indexOf(currentStep) + 1] - const previousStep = stepOrder[stepOrder.indexOf(currentStep) - 1] + + type PartialStep = Step | undefined + const nextStep = stepOrder[stepOrder.indexOf(currentStep) + 1] as PartialStep + const prevStep = stepOrder[stepOrder.indexOf(currentStep) - 1] as PartialStep return ( @@ -69,7 +73,7 @@ export default function Navigation({ color={'secondary'} to={ absoluteSitePaths.assistants['choix-du-statut'][ - previousStep || 'index' + prevStep || 'index' ] } > @@ -79,8 +83,24 @@ export default function Navigation({ {nextStep && (