-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feat/33 refactor stepper #33 #61
base: main
Are you sure you want to change the base?
Changes from all commits
62b226e
18a6a40
aff6404
ded243a
b1d4141
c1a678e
f73a6dd
d3c7007
4af0d50
56ed9ac
37a0842
6997a37
53029b5
9a12d32
6327742
e9f5a52
d026384
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
'use client' | ||
|
||
import React, { useEffect, useState } from 'react' | ||
import { Button, Heading } from '@utrecht/component-library-react' | ||
import { useTranslations } from 'next-intl' | ||
import { useStepperStore } from '@/store/stepper_store' | ||
import { useFormStore } from '@/store/form_store' | ||
import { steps, useRouter } from '@/routing/navigation' | ||
import { FaChevronLeft, FaChevronRight } from 'react-icons/fa' | ||
import { FormStep } from '@/types/form' | ||
|
||
const FormProgress = () => { | ||
const t = useTranslations('stepper') | ||
const { | ||
step, | ||
goToStep, | ||
setNavToSummary, | ||
visitedSteps, | ||
resetVisitedSteps, | ||
setGoBack, | ||
} = useStepperStore() | ||
const { resetForm } = useFormStore() | ||
const router = useRouter() | ||
|
||
const [percentage, setPercentage] = useState<number>(1) | ||
|
||
useEffect(() => { | ||
setPercentage((step / 4) * 100) | ||
}, [step]) | ||
|
||
const resetState = () => { | ||
resetVisitedSteps() | ||
goToStep(FormStep.STEP_1_DESCRIPTION) | ||
resetForm() | ||
router.push(steps[FormStep.STEP_1_DESCRIPTION]) | ||
} | ||
|
||
if (!visitedSteps.includes(FormStep.STEP_4_SUMMARY)) { | ||
return ( | ||
<div className="relative flex flex-col-reverse sm:flex-col"> | ||
<div> | ||
{step > FormStep.STEP_1_DESCRIPTION && ( | ||
<Button | ||
appearance={'subtle-button'} | ||
className="sm:absolute sm:left-0 sm:-top-2 stepper-button-hover pl-0-overwrite" | ||
onClick={() => setGoBack(true)} | ||
> | ||
<FaChevronLeft /> | ||
{t('back')} | ||
</Button> | ||
)} | ||
|
||
{step < FormStep.STEP_4_SUMMARY && | ||
visitedSteps.includes(FormStep.STEP_3_CONTACT) && ( | ||
iehkaatee marked this conversation as resolved.
Show resolved
Hide resolved
|
||
<Button | ||
className="absolute right-0 sm:-top-2 stepper-button-hover pr-0-overwrite" | ||
appearance={'subtle-button'} | ||
onClick={() => setNavToSummary(true)} | ||
> | ||
{t('to_summary')} | ||
<FaChevronRight /> | ||
</Button> | ||
)} | ||
</div> | ||
|
||
<div className="flex flex-col justify-center sm:items-center gap-3 pb-2"> | ||
<Heading level={4}> | ||
{t('step', { currentStep: step, totalSteps: 4 })} | ||
</Heading> | ||
<div className="overflow-hidden bg-gray-200 w-full"> | ||
{/*todo: check how to set primary color */} | ||
<div | ||
style={{ width: `${percentage}%` }} | ||
className="h-1 bg-green-700" | ||
/> | ||
</div> | ||
</div> | ||
</div> | ||
) | ||
} | ||
return <Button onClick={() => resetState()}>{t('new_notification')}</Button> | ||
} | ||
|
||
export default FormProgress |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,8 +6,8 @@ import { fetchAdditionalQuestions } from '@/services/additional-questions' | |
import { useFormStore } from '@/store/form_store' | ||
import { IncidentFormFooter } from '@/app/[locale]/incident/components/IncidentFormFooter' | ||
import { useStepperStore } from '@/store/stepper_store' | ||
import { useRouter } from '@/routing/navigation' | ||
import { PublicQuestion } from '@/types/form' | ||
import { steps, useRouter } from '@/routing/navigation' | ||
import { FormStep, PublicQuestion } from '@/types/form' | ||
import { Paragraph } from '@/components/index' | ||
import { RenderSingleField } from '@/app/[locale]/incident/add/components/questions/RenderSingleField' | ||
import { LocationSelect } from '@/app/[locale]/incident/add/components/questions/LocationSelect' | ||
|
@@ -20,7 +20,15 @@ export const IncidentQuestionsLocationForm = () => { | |
const [additionalQuestions, setAdditionalQuestions] = useState< | ||
PublicQuestion[] | ||
>([]) | ||
const { addOneStep, setLastCompletedStep } = useStepperStore() | ||
const { | ||
addOneStep, | ||
addVisitedStep, | ||
navToSummary, | ||
setNavToSummary, | ||
goToStep, | ||
goBack, | ||
setGoBack, | ||
} = useStepperStore() | ||
const router = useRouter() | ||
const methods = useForm() | ||
const t = useTranslations('general.errors') | ||
|
@@ -105,12 +113,31 @@ export const IncidentQuestionsLocationForm = () => { | |
extra_properties: answers, | ||
}) | ||
|
||
setLastCompletedStep(2) | ||
addVisitedStep(FormStep.STEP_2_ADD) | ||
addOneStep() | ||
|
||
router.push('/incident/contact') | ||
router.push(steps[FormStep.STEP_3_CONTACT]) | ||
} | ||
|
||
useEffect(() => { | ||
if (navToSummary) { | ||
// todo: finish update form when component it fixed | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Wordt opgepakt wanneer we de locatie selectie afmaken |
||
// updateForm({}) | ||
|
||
goToStep(FormStep.STEP_4_SUMMARY) | ||
router.push(steps[FormStep.STEP_4_SUMMARY]) | ||
setNavToSummary(false) | ||
} | ||
}, [navToSummary]) | ||
|
||
useEffect(() => { | ||
if (goBack) { | ||
// updateForm | ||
goToStep(FormStep.STEP_1_DESCRIPTION) | ||
router.push(steps[FormStep.STEP_1_DESCRIPTION]) | ||
setGoBack(false) | ||
} | ||
}, [goBack]) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Wat denk ik een handigere manier is om al deze code duplicatie in straks elke form te voorkomen is deze goBack useEffect logica en summary logica in het FormProgress component te stoppen. Wat je in principe namelijk wil is dat bij een klik op vorige dan een functie gecalled wordt, die obv van dan je huidige formulier stap de nieuwe stap bedenkt: goToStep(betreffendeStap) daarmee zet, dan de router.push doet en onGoBack(false) zet, dan weer datzelfde riedeltje voor de summary. Form specifieke logica (om het formulier op te slaan) hou je dan gewoon in de form submission functie en die call je dan ook weer vanuit die functie in je form progress component. |
||
|
||
return ( | ||
<FormProvider {...methods}> | ||
<form | ||
|
This file was deleted.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ik zou zelf verwachten dat deze resetState() functie binnen de store bestaat en daar dus de resetVisitedSteps() logica en de goToStep() logica uitgevoerd wordt. Dan heb je hier enkel nog resetForm en de router.push()