Skip to content
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

Open
wants to merge 17 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 84 additions & 0 deletions src/app/[locale]/components/FormProgress.tsx
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 = () => {
Copy link
Contributor

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()

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
Expand Up @@ -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'
Expand All @@ -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')
Expand Down Expand Up @@ -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
Copy link
Contributor Author

Choose a reason for hiding this comment

The 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])
Copy link
Contributor

Choose a reason for hiding this comment

The 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
Expand Down
35 changes: 27 additions & 8 deletions src/app/[locale]/incident/components/IncidentDescriptionForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,21 @@ import * as z from 'zod'
import {
Form,
FormControl,
FormDescription,
FormField,
FormItem,
FormMessage,
FormDescription,
} from '@/components/ui/Form'
import { useForm } from 'react-hook-form'
import { zodResolver } from '@hookform/resolvers/zod'
import { useTranslations } from 'next-intl'
import { IncidentFormFooter } from '@/app/[locale]/incident/components/IncidentFormFooter'
import { useStepperStore } from '@/store/stepper_store'
import { useRouter } from '@/routing/navigation'
import { useEffect } from 'react'
import { steps, useRouter } from '@/routing/navigation'
import React, { useEffect } from 'react'
import { getCategoryForDescription } from '@/services/classification'
import { debounce } from 'lodash'
import { useFormStore } from '@/store/form_store'
import React from 'react'
import {
ACCEPTED_IMAGE_TYPES,
FileUpload,
Expand All @@ -30,11 +29,18 @@ import {

import { FormFieldTextarea } from '@utrecht/component-library-react/dist/css-module'
import { Label } from '@amsterdam/design-system-react'
import { FormStep } from '@/types/form'

export const IncidentDescriptionForm = () => {
const t = useTranslations('describe-report.form')
const { updateForm, formState } = useFormStore()
const { addOneStep, setLastCompletedStep } = useStepperStore()
const {
addOneStep,
navToSummary,
setNavToSummary,
goToStep,
addVisitedStep,
} = useStepperStore()
const router = useRouter()

useEffect(() => {
Expand Down Expand Up @@ -108,12 +114,25 @@ export const IncidentDescriptionForm = () => {
attachments: values.files,
})

setLastCompletedStep(1)
addVisitedStep(FormStep.STEP_1_DESCRIPTION)
addOneStep()

router.push('/incident/add')
router.push(steps[FormStep.STEP_2_ADD])
}

useEffect(() => {
if (navToSummary) {
updateForm({
...formState,
description: form.getValues('description'),
attachments: form.getValues('files'),
})

goToStep(FormStep.STEP_4_SUMMARY)
router.push(steps[FormStep.STEP_4_SUMMARY])
setNavToSummary(false)
}
}, [navToSummary])
iehkaatee marked this conversation as resolved.
Show resolved Hide resolved

const handleFileChange = (e: React.ChangeEvent<HTMLInputElement>) => {
const files = e.target.files
if (files && files.length > 0) {
Expand Down
132 changes: 0 additions & 132 deletions src/app/[locale]/incident/components/Stepper.tsx

This file was deleted.

Loading
Loading