From 571dda0680ebf2e27ae916c9e378d06e935099ae Mon Sep 17 00:00:00 2001 From: Lucieo Date: Tue, 20 Feb 2024 15:21:23 +0000 Subject: [PATCH] Add info into application form (#72) --- web/@types/reference.d.ts | 2 +- .../Places/Application/ApplicationForm.tsx | 41 +++++++++++++++---- .../Places/Application/ApplicationGeneral.tsx | 5 ++- .../Application/CampaignApplicationPopin.tsx | 1 + .../Application/Inputs/BooleanField.tsx | 1 + .../Application/Inputs/TextAreaField.tsx | 2 +- .../Application/References/ReferenceItem.tsx | 6 ++- .../Places/PlacesListCampaignHelper.tsx | 7 ++-- web/components/Home/HomePlaces.tsx | 5 ++- web/components/InputMultiSelect.tsx | 4 +- web/hooks/useCurrentUser.tsx | 2 +- 11 files changed, 57 insertions(+), 19 deletions(-) diff --git a/web/@types/reference.d.ts b/web/@types/reference.d.ts index fe5636e2..58f70292 100644 --- a/web/@types/reference.d.ts +++ b/web/@types/reference.d.ts @@ -1,7 +1,7 @@ export interface Reference { title: string year: number - coproducers?: string[] + coproducers?: string[] | string other?: string actors: number } diff --git a/web/components/Campaign/Places/Application/ApplicationForm.tsx b/web/components/Campaign/Places/Application/ApplicationForm.tsx index 7f991a72..b6985b69 100644 --- a/web/components/Campaign/Places/Application/ApplicationForm.tsx +++ b/web/components/Campaign/Places/Application/ApplicationForm.tsx @@ -8,31 +8,54 @@ import * as yup from 'yup' import useCampaignContext from '~components/Campaign/useCampaignContext' import { useTranslation } from 'next-i18next' import useToast from '~hooks/useToast' -import { useEffect, useState } from 'react' +import { useState } from 'react' import { client } from '~api/client-api' import { ScheduleEvent } from '~@types/schedule-event' import { useCurrentUser } from '~hooks/useCurrentUser' import ApplicationReferences from '~components/Campaign/Places/Application/References/ApplicationReferences' +import { Espace } from '~typings/api' +const getDefaultValues = (applications) => { + if (applications?.length > 0) { + const lastApplication = applications[applications?.length - 1] + + const data = { + ...lastApplication, + references: + lastApplication?.references.map((el) => ({ + ...el, + coproducers: Array.isArray(el.coproducers) + ? el.coproducers + : el.coproducers.split(',').map((el) => +el), + })) || [], + already_supported: lastApplication?.already_supported.toString(), + } + return data + } + return {} +} const ApplicationForm = ({ back, setConfirmed, events, + place, }: { back: () => void setConfirmed: (value: boolean) => void events: ScheduleEvent[] + place: Espace }) => { + const { applications } = useCurrentUser() const { t } = useTranslation('place') const schema = yup.object().shape({ already_supported: yup.boolean().required(t('global.required')), cv: yup.string().required(t('global.required')), creation_title: yup.string().required(t('global.required')), creation_dancers: yup.number().required(t('global.required')), - creation_file: yup - .array() - .min(1, t('global.required')) - .required(t('global.required')), + // creation_file: yup + // .array() + // .min(1, t('global.required')) + // .required(t('global.required')), creation_summary: yup.string().required(t('global.required')), creation_techical_requirements: yup.string().required(t('global.required')), eligibility: yup.boolean().required(t('global.required')), @@ -40,12 +63,14 @@ const ApplicationForm = ({ const { data: user } = useCurrentUser() const { currentCampaign } = useCampaignContext() + const form = useForm({ resolver: yupResolver(schema), + defaultValues: getDefaultValues(applications), }) const { errorToast } = useToast() const [isLoading, setLoading] = useState(false) - const { handleSubmit, errors, getValues } = form + const { handleSubmit, getValues } = form const onSubmit = async (formValues) => { setLoading(true) @@ -63,6 +88,8 @@ const ApplicationForm = ({ : file, ), ) + } else if (key === 'references') { + total.data[key] = JSON.stringify(formValues[key]) } else { total.data[key] = formValues[key] } @@ -96,7 +123,7 @@ const ApplicationForm = ({
- + diff --git a/web/components/Campaign/Places/Application/ApplicationGeneral.tsx b/web/components/Campaign/Places/Application/ApplicationGeneral.tsx index fe595a7a..199c3a6f 100644 --- a/web/components/Campaign/Places/Application/ApplicationGeneral.tsx +++ b/web/components/Campaign/Places/Application/ApplicationGeneral.tsx @@ -4,8 +4,9 @@ import ApplicationFormTitle from '~components/Campaign/Places/Application/Applic import BooleanField from '~components/Campaign/Places/Application/Inputs/BooleanField' import TextAreaField from '~components/Campaign/Places/Application/Inputs/TextAreaField' import useCampaignContext from '~components/Campaign/useCampaignContext' +import { Espace } from '~typings/api' -const ApplicationGeneral = () => { +const ApplicationGeneral = ({ place }: { place: Espace }) => { const { currentCampaign } = useCampaignContext() const { t } = useTranslation('place') return ( @@ -17,7 +18,7 @@ const ApplicationGeneral = () => { diff --git a/web/components/Campaign/Places/Application/CampaignApplicationPopin.tsx b/web/components/Campaign/Places/Application/CampaignApplicationPopin.tsx index df4b6832..d15a6915 100644 --- a/web/components/Campaign/Places/Application/CampaignApplicationPopin.tsx +++ b/web/components/Campaign/Places/Application/CampaignApplicationPopin.tsx @@ -54,6 +54,7 @@ const CampaignApplicationPopin = ({ events, place, back }: Props) => { back={back} setConfirmed={setConfirmed} events={events} + place={place} /> diff --git a/web/components/Campaign/Places/Application/Inputs/BooleanField.tsx b/web/components/Campaign/Places/Application/Inputs/BooleanField.tsx index aa0a040b..8491b234 100644 --- a/web/components/Campaign/Places/Application/Inputs/BooleanField.tsx +++ b/web/components/Campaign/Places/Application/Inputs/BooleanField.tsx @@ -20,6 +20,7 @@ const BooleanField = ({ label, name }: { label: string; name: string }) => { onChange={(value) => { field.onChange(value) }} + value={field.value} > -