Skip to content

Commit

Permalink
Add info into application form (#72)
Browse files Browse the repository at this point in the history
  • Loading branch information
Lucieo authored Feb 20, 2024
1 parent 16f4b58 commit 571dda0
Show file tree
Hide file tree
Showing 11 changed files with 57 additions and 19 deletions.
2 changes: 1 addition & 1 deletion web/@types/reference.d.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
export interface Reference {
title: string
year: number
coproducers?: string[]
coproducers?: string[] | string
other?: string
actors: number
}
41 changes: 34 additions & 7 deletions web/components/Campaign/Places/Application/ApplicationForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,44 +8,69 @@ 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')),
})
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)
Expand All @@ -63,6 +88,8 @@ const ApplicationForm = ({
: file,
),
)
} else if (key === 'references') {
total.data[key] = JSON.stringify(formValues[key])
} else {
total.data[key] = formValues[key]
}
Expand Down Expand Up @@ -96,7 +123,7 @@ const ApplicationForm = ({
<form onSubmit={handleSubmit(onSubmit)}>
<VStack flex={2} spacing={14}>
<ApplicationReferences />
<ApplicationGeneral />
<ApplicationGeneral place={place} />
<ApplicationCreation />
<ApplicationEligibility />

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand All @@ -17,7 +18,7 @@ const ApplicationGeneral = () => {

<BooleanField
label={t('campaignApplication.general.subtitle', {
place: currentCampaign?.title,
place: place?.name,
})}
name="already_supported"
/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ const CampaignApplicationPopin = ({ events, place, back }: Props) => {
back={back}
setConfirmed={setConfirmed}
events={events}
place={place}
/>

<ApplicationRecap place={place} events={events} back={back} />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ const BooleanField = ({ label, name }: { label: string; name: string }) => {
onChange={(value) => {
field.onChange(value)
}}
value={field.value}
>
<HStack>
<Box
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ const TextAreaField = ({

return (
<FormField label={label} errors={errors[name]} flex={1} helper={helper}>
<Textarea onChange={onChange} size="lg" {...props} />
<Textarea onChange={onChange} size="lg" value={field.value} {...props} />
</FormField>
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const ReferenceItem = ({
}) => {
const { currentCampaign } = useCampaignContext()
const { t } = useTranslation('place')

return (
<VStack
border="1px solid blue"
Expand Down Expand Up @@ -73,7 +74,10 @@ const ReferenceItem = ({
</Text>
<Text as="span" pl={1} color="gray.500">
{[
...(reference?.coproducers?.map(
...((Array.isArray(reference?.coproducers)
? reference.coproducers
: reference.coproducers.split(',')
).map(
(coproducer) =>
currentCampaign.users_permissions_users.find(
(user) => user?.id === coproducer,
Expand Down
7 changes: 4 additions & 3 deletions web/components/Campaign/Places/PlacesListCampaignHelper.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
Button,
StackProps,
VStack,
Stack,
} from '@chakra-ui/react'
import { useTranslation } from 'next-i18next'
import ApplicationCounter from '~components/Campaign/ApplicationCounter'
Expand Down Expand Up @@ -38,7 +39,7 @@ const PlacesListCampaignHelper = ({
{campaign && !isHome && (
<ApplicationCounter borderBottom="1px solid lightgray" />
)}
<HStack width="100%" spacing={6}>
<Stack width="100%" spacing={6} direction={{ base: 'column', sm: 'row' }}>
<VStack flex={4} justifyContent="flex-start" alignItems="flex-start">
<Text as="span" fontWeight="bold" marginRight={1}>
{campaign
Expand All @@ -49,7 +50,7 @@ const PlacesListCampaignHelper = ({
{campaign ? campaign.description : t('solidarity.helper')}
</Text>
</VStack>
<Flex flex={1} justifyContent="flex-end">
<Flex flex={1} justifyContent={{ base: 'flex-start', sm: 'flex-end' }}>
<Button
variant={campaign ? 'campaign' : 'blueFill'}
as={Link}
Expand All @@ -62,7 +63,7 @@ const PlacesListCampaignHelper = ({
{t('show')}
</Button>
</Flex>
</HStack>
</Stack>
</VStack>
)
}
Expand Down
5 changes: 3 additions & 2 deletions web/components/Home/HomePlaces.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -81,13 +81,14 @@ const HomePlaces = ({ campaign }: Props) => {
href={ROUTE_PLACES}
variant="outline"
colorScheme="campaign"
size="xl"
color={'campaign.dark'}
_hover={{ bg: 'campaign.light', textDecor: 'none' }}
whiteSpace="normal"
textAlign="center"
p={6}
lineHeight="inherit"
height="auto"
size="xl"
p={2}
>
{t('places.campaign.cta', {
title: campaign?.title,
Expand Down
4 changes: 3 additions & 1 deletion web/components/InputMultiSelect.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ const InputMultiSelect = ({
})

const onChange = (value) => {
field.onChange(value?.map((el) => el.value) || [])
field.onChange(
Array.isArray(value) ? value?.map((el) => el.value) : [value.value] || [],
)
}

return (
Expand Down
2 changes: 1 addition & 1 deletion web/hooks/useCurrentUser.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,6 @@ export const useCurrentUser = () => {
canApply:
userData?.data?.type === 'company' &&
currentCampaign?.applications_max &&
applications?.length < 2,
applications?.length < currentCampaign?.applications_max,
}
}

0 comments on commit 571dda0

Please sign in to comment.