Branche la question sur l'association

pull/2782/head
Johan Girod 2023-06-19 11:56:38 +02:00
parent 1b48c36b68
commit f2fca74851
5 changed files with 140 additions and 81 deletions

View File

@ -1,72 +1,31 @@
import { Trans, useTranslation } from 'react-i18next'
import { useMatch } from 'react-router-dom'
import { useNavigate } from 'react-router-dom'
import styled from 'styled-components'
import { Button } from '@/design-system/buttons'
import { Grid, Spacing } from '@/design-system/layout'
import { RelativeSitePaths, useSitePaths } from '@/sitePaths'
import { useSitePaths } from '@/sitePaths'
type ChoixStatut = RelativeSitePaths['assistants']['choix-du-statut']
type Step = keyof ChoixStatut
export const stepOrder: readonly Step[] = [
'recherche-activité',
'détails-activité',
'commune',
'association',
'associé',
'rémunération',
'statuts',
'résultat',
] as const
function useCurrentStep() {
const { relativeSitePaths, absoluteSitePaths } = useSitePaths()
const localizedStep = useMatch(
`${absoluteSitePaths.assistants['choix-du-statut'].index}/:step`
)?.params.step
if (!localizedStep) {
return null
}
const entries = Object.entries(
relativeSitePaths.assistants['choix-du-statut']
) as [Step, ChoixStatut[Step]][]
const [currentStep] =
entries.find(([, value]) => value === localizedStep) ?? []
if (!currentStep || !stepOrder.includes(currentStep)) {
return null
}
return currentStep
}
import { useNextStep } from './useNextStep'
export default function Navigation({
currentStepIsComplete,
nextStepLabel,
onNextStep,
onPreviousStep,
assistantIsCompleted = false,
}: {
currentStepIsComplete: boolean
nextStepLabel?: false | string
onNextStep?: () => void
onPreviousStep?: () => void
assistantIsCompleted?: boolean
}) {
const { t } = useTranslation()
const { absoluteSitePaths } = useSitePaths()
const currentStep = useCurrentStep()
if (!currentStep) {
return null
}
type PartialStep = Step | undefined
const nextStep = stepOrder[stepOrder.indexOf(currentStep) + 1] as PartialStep
const previousStep = stepOrder[
stepOrder.indexOf(currentStep) - 1
] as PartialStep
const nextStep = useNextStep()
const navigate = useNavigate()
const resultatPath =
useSitePaths().absoluteSitePaths.assistants['choix-du-statut'].résultat
return (
<>
@ -77,30 +36,19 @@ export default function Navigation({
<Button
light
color={'secondary'}
onPress={onPreviousStep}
to={
absoluteSitePaths.assistants['choix-du-statut'][
previousStep || 'index'
]
}
onPress={() => {
onPreviousStep?.()
navigate(-1)
}}
>
<span aria-hidden></span> <Trans>Précédent</Trans>
</Button>
</Grid>
{nextStep && (
{nextStep && !assistantIsCompleted && (
<Grid item>
<Button
onPress={onNextStep}
// 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(),
// }}
to={nextStep}
isDisabled={!currentStepIsComplete}
aria-label={t("Suivant, passer à l'étape suivante")}
>
@ -111,6 +59,19 @@ export default function Navigation({
</Button>
</Grid>
)}
{assistantIsCompleted && (
<Grid item>
<Button
to={resultatPath}
aria-label={t('Suivant, voir le résultat')}
>
{nextStepLabel || (
<Trans>Enregistrer et voir le résultat</Trans>
)}{' '}
<span aria-hidden></span>
</Button>
</Grid>
)}
</Grid>
</StyledNavigation>
<Shadow />

View File

@ -82,7 +82,7 @@ const StyledMessage = styled(Message)`
function Statut({ statut }: { statut: DottedName }) {
const engine = useEngine()
const disabled =
engine.evaluate({ 'est non applicable': statut }).nodeValue === true
engine.evaluate({ '=': [statut, 'non'] }).nodeValue === true
const acronyme = (engine.getRule(statut).rawNode.acronyme ??
engine.getRule(statut).title.toLocaleLowerCase()) as Statut

View File

@ -0,0 +1,40 @@
import { useMatch } from 'react-router-dom'
import { RelativeSitePaths, useSitePaths } from '@/sitePaths'
type ChoixStatut = RelativeSitePaths['assistants']['choix-du-statut']
type Step = keyof ChoixStatut
const stepOrder: readonly Step[] = [
'recherche-activité',
'détails-activité',
'commune',
'association',
'associé',
'rémunération',
'statuts',
'résultat',
] as const
export function useNextStep() {
const { relativeSitePaths, absoluteSitePaths } = useSitePaths()
const localizedStep = useMatch(
`${absoluteSitePaths.assistants['choix-du-statut'].index}/:step`
)?.params.step
if (!localizedStep) {
return absoluteSitePaths.assistants['choix-du-statut'][stepOrder[0]]
}
const entries = Object.entries(
relativeSitePaths.assistants['choix-du-statut']
) as [Step, ChoixStatut[Step]][]
const [currentStep] =
entries.find(([, value]) => value === localizedStep) ?? []
const nextStep =
stepOrder[currentStep ? stepOrder.indexOf(currentStep) + 1 : 0]
console.log('huihuihi', nextStep)
return absoluteSitePaths.assistants['choix-du-statut'][nextStep]
}

View File

@ -1,15 +1,22 @@
import { Evaluation } from 'publicodes'
import { useState } from 'react'
import { Trans, useTranslation } from 'react-i18next'
import { useDispatch } from 'react-redux'
import { useEngine } from '@/components/utils/EngineContext'
import { RadioCard, RadioCardGroup } from '@/design-system'
import { HelpButtonWithPopover } from '@/design-system/buttons'
import { Strong } from '@/design-system/typography'
import { Body } from '@/design-system/typography/paragraphs'
import { batchUpdateSituation } from '@/store/actions/actions'
import Layout from './_components/Layout'
import Navigation from './_components/Navigation'
export default function Association() {
const { t } = useTranslation()
const [currentSelection, setCurrentSelection, reset] =
useAssociationSelection()
return (
<>
@ -42,8 +49,8 @@ export default function Association() {
'choix-statut.association.question',
'Pourquoi créez vous cette entreprise ?'
)}
// onChange={handleChange}
// value={currentSelection ?? undefined}
onChange={setCurrentSelection}
value={currentSelection}
>
<RadioCard
value={'gagner-argent'}
@ -52,8 +59,6 @@ export default function Association() {
Dans le but de <Strong>gagner de l'argent</Strong>
</Trans>
}
// emoji={node.rawNode.icônes}
// description={node.rawNode.description}
>
<Body></Body>
</RadioCard>
@ -64,15 +69,71 @@ export default function Association() {
Dans un but <Strong>non lucratif</Strong>
</Trans>
}
// emoji={node.rawNode.icônes}
description={t(
'choix-statut.association.question.non-lucratif.description',
'Par exemple, en créant une association'
)}
/>
</RadioCardGroup>
<Navigation currentStepIsComplete />
<Navigation
currentStepIsComplete={currentSelection !== undefined}
onPreviousStep={reset}
assistantIsCompleted={currentSelection === 'non-lucratif'}
/>
</Layout>
</>
)
}
type RadioOption = 'gagner-argent' | 'non-lucratif' | undefined
function useAssociationSelection(): [
RadioOption,
(value: RadioOption) => void,
() => void
] {
const isAssociation = useEngine().evaluate(
'entreprise . catégorie juridique . association'
).nodeValue as Evaluation<boolean>
const [currentSelection, setCurrentSelection] = useState<RadioOption>(
isAssociation === true
? 'non-lucratif'
: isAssociation === false
? 'gagner-argent'
: undefined
)
const dispatch = useDispatch()
const handleChange = (value: RadioOption) => {
setCurrentSelection(value)
if (value === 'gagner-argent') {
dispatch(
batchUpdateSituation({
'entreprise . catégorie juridique . association': 'non',
'entreprise . catégorie juridique': undefined,
})
)
} else if (value === 'non-lucratif') {
dispatch(
batchUpdateSituation({
'entreprise . catégorie juridique . association': undefined,
'entreprise . catégorie juridique': "'association'",
})
)
}
}
const reset = () => {
setCurrentSelection(undefined)
dispatch(
batchUpdateSituation({
'entreprise . catégorie juridique . association': undefined,
'entreprise . catégorie juridique': undefined,
})
)
}
return [currentSelection, handleChange, reset]
}

View File

@ -12,15 +12,15 @@ import { H3 } from '@/design-system/typography/heading'
import { Body, Intro, SmallBody } from '@/design-system/typography/paragraphs'
import useSimulatorsData from '@/hooks/useSimulatorsData'
import { SimulateurCard } from '@/pages/simulateurs-et-assistants'
import { useSitePaths } from '@/sitePaths'
import { stepOrder } from './_components/Navigation'
import { useNextStep } from './_components/useNextStep'
import créerSvg from './_illustrations/créer.svg'
export default function AccueilChoixStatut() {
const { t } = useTranslation()
const { relativeSitePaths } = useSitePaths()
const simulateurData = useSimulatorsData()
const nextStep = useNextStep()
return (
<>
@ -58,10 +58,7 @@ export default function AccueilChoixStatut() {
<Grid container spacing={3} style={{ alignItems: 'center' }}>
<Grid item xs={12} sm={'auto'}>
<Button
size="XL"
to={relativeSitePaths.assistants['choix-du-statut'][stepOrder[0]]}
>
<Button size="XL" to={nextStep}>
<Trans i18nKey="choix-statut.home.find-statut">
Trouver le bon statut
</Trans>