Fix async bug when click on next button

pull/2782/head
Jérémy Rialland 2023-06-07 16:56:45 +02:00 committed by Johan Girod
parent d5a3337a03
commit c629a75466
3 changed files with 49 additions and 22 deletions

View File

@ -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 (
<StyledNavigation>
@ -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 && (
<Grid item>
<Button
onPress={onNextStep}
to={absoluteSitePaths.assistants['choix-du-statut'][nextStep]}
onPress={() =>
onNextStep?.({
nextStep,
nextAbsolutePath:
absoluteSitePaths.assistants['choix-du-statut'][nextStep],
})
}
// Probleme de desynchronisation entre le onpress et le to, le onpress n'est pas toujours appelé avant le to
// to={absoluteSitePaths.assistants['choix-du-statut'][nextStep]}
// est ce qu'on devrait pas utiliser les parametres de l'url comme ca ?
// to={{
// pathname:
// absoluteSitePaths.assistants['choix-du-statut'][nextStep],
// search: createSearchParams({
// codeAPE: '6201Z',
// }).toString(),
// }}
isDisabled={!currentStepIsComplete}
aria-label={t("Suivant, passer à l'étape suivante")}
>
@ -100,6 +120,6 @@ const StyledNavigation = styled.div`
margin: 0 -1rem;
bottom: 0;
background: ${({ theme }) => theme.colors.extended.grey[100]};
z-index: 1;
z-index: 2;
/* box-shadow: ${({ theme }) => theme.elevations[6]}; */
`

View File

@ -22,7 +22,7 @@ import {
import Layout from './_components/Layout'
import Navigation from './_components/Navigation'
export default function Activité() {
export default function DétailsActivité() {
const { t } = useTranslation()
const codeApe = useEngine().evaluate(
'entreprise . activités . principale . code APE'
@ -107,7 +107,11 @@ function GuichetSelection({
return (
<>
<Body>Sectionnez la description d'activité qui correspond le mieux.</Body>
<RadioCardGroup value={codeGuichet} onChange={onGuichetSelected}>
<RadioCardGroup
value={codeGuichet}
onChange={onGuichetSelected}
aria-label={'liste des activités'}
>
{entries.map((guichetEntry) => {
return (
<RadioCardSkeleton

View File

@ -1,6 +1,7 @@
import { useState } from 'react'
import { Trans, useTranslation } from 'react-i18next'
import { useDispatch } from 'react-redux'
import { useNavigate } from 'react-router-dom'
import { Message } from '@/design-system'
import { HelpButtonWithPopover } from '@/design-system/buttons'
@ -13,8 +14,9 @@ import SearchCodeAPE from '../recherche-code-ape/SearchCodeAPE'
import Layout from './_components/Layout'
import Navigation from './_components/Navigation'
export default function Activité() {
export default function RechercheActivité() {
const [codeApe, setCodeApe] = useState('')
const naviguate = useNavigate()
const { t } = useTranslation()
const dispatch = useDispatch()
@ -55,14 +57,15 @@ export default function Activité() {
<SearchCodeAPE hideGuichetUnique onCodeAPESelected={setCodeApe} />
<Navigation
currentStepIsComplete={!!codeApe}
onNextStep={() =>
onNextStep={({ nextAbsolutePath }) => {
dispatch(
updateSituation(
'entreprise . activités . principale . code APE',
`'${codeApe}'`
)
)
}
naviguate(nextAbsolutePath)
}}
/>
</Layout>
</>