|
1 | 1 | "use client";
|
2 | 2 |
|
| 3 | +import { useCandidacy } from "@/components/candidacy/candidacy.context"; |
3 | 4 | import { useFeatureFlipping } from "@/components/feature-flipping/featureFlipping";
|
| 5 | +import { FormButtons } from "@/components/form/form-footer/FormButtons"; |
| 6 | +import { FormOptionalFieldsDisclaimer } from "@/components/legacy/atoms/FormOptionalFieldsDisclaimer/FormOptionalFieldsDisclaimer"; |
| 7 | +import { graphqlErrorToast, successToast } from "@/components/toast/toast"; |
| 8 | +import Button from "@codegouvfr/react-dsfr/Button"; |
| 9 | +import Checkbox from "@codegouvfr/react-dsfr/Checkbox"; |
| 10 | +import Input from "@codegouvfr/react-dsfr/Input"; |
| 11 | +import { zodResolver } from "@hookform/resolvers/zod"; |
| 12 | +import { format, isBefore } from "date-fns"; |
| 13 | +import Link from "next/link"; |
| 14 | +import { useCallback, useEffect, useMemo, useState } from "react"; |
| 15 | +import { useForm } from "react-hook-form"; |
| 16 | +import { z } from "zod"; |
| 17 | +import { useActualisation } from "./actualisation.hooks"; |
| 18 | + |
| 19 | +const schema = z |
| 20 | + .object({ |
| 21 | + candidateConfirmation: z.boolean(), |
| 22 | + readyForJuryEstimatedAt: z.string().nullable(), |
| 23 | + }) |
| 24 | + .superRefine(({ candidateConfirmation, readyForJuryEstimatedAt }, ctx) => { |
| 25 | + if (!candidateConfirmation) { |
| 26 | + ctx.addIssue({ |
| 27 | + code: z.ZodIssueCode.custom, |
| 28 | + message: "Vous devez confirmer être toujours en cours de parcours VAE.", |
| 29 | + path: ["candidateConfirmation"], |
| 30 | + }); |
| 31 | + } |
| 32 | + |
| 33 | + if (!readyForJuryEstimatedAt) { |
| 34 | + ctx.addIssue({ |
| 35 | + code: z.ZodIssueCode.custom, |
| 36 | + message: |
| 37 | + "Veuillez sélectionner une date prévisionnelle de dépôt du dossier de validation", |
| 38 | + path: ["readyForJuryEstimatedAt"], |
| 39 | + }); |
| 40 | + } else if (isBefore(new Date(readyForJuryEstimatedAt), new Date())) { |
| 41 | + ctx.addIssue({ |
| 42 | + code: z.ZodIssueCode.custom, |
| 43 | + message: "Merci d'indiquer une date postérieure à la date du jour", |
| 44 | + path: ["readyForJuryEstimatedAt"], |
| 45 | + }); |
| 46 | + } |
| 47 | + }); |
| 48 | + |
| 49 | +type ActualisationForm = z.infer<typeof schema>; |
| 50 | + |
| 51 | +const HasBeenUpdatedComponent = ({ |
| 52 | + readyForJuryEstimatedAt, |
| 53 | +}: { |
| 54 | + readyForJuryEstimatedAt: string; |
| 55 | +}) => { |
| 56 | + return ( |
| 57 | + <div className="flex flex-col"> |
| 58 | + <h1>Votre actualisation est enregistrée</h1> |
| 59 | + <p className="text-xl"> |
| 60 | + Vous pouvez désormais continuer votre parcours ! Rendez-vous dans votre |
| 61 | + espace pour connaître les prochaines étapes. |
| 62 | + </p> |
| 63 | + <p className="text-xl"> |
| 64 | + Pour information, votre date prévisionnelle de dépot du dossier de |
| 65 | + validation est le{" "} |
| 66 | + {format(new Date(readyForJuryEstimatedAt), "dd/MM/yyyy")}. |
| 67 | + </p> |
| 68 | + <div> |
| 69 | + <Link href="/"> |
| 70 | + <Button data-test="actualisation-continue-button"> |
| 71 | + Continuer mon parcours |
| 72 | + </Button> |
| 73 | + </Link> |
| 74 | + </div> |
| 75 | + </div> |
| 76 | + ); |
| 77 | +}; |
4 | 78 |
|
5 | 79 | export default function ActualisationPage() {
|
| 80 | + const [hasBeenUpdated, setHasBeenUpdated] = useState(false); |
6 | 81 | const { isFeatureActive } = useFeatureFlipping();
|
7 | 82 | const candidacyActualisationFeatureIsActive = isFeatureActive(
|
8 | 83 | "candidacy_actualisation",
|
9 | 84 | );
|
| 85 | + const { candidacy } = useCandidacy(); |
| 86 | + const { updateLastActivityDate } = useActualisation(); |
| 87 | + |
| 88 | + const defaultValues = useMemo( |
| 89 | + () => ({ |
| 90 | + candidateConfirmation: false, |
| 91 | + readyForJuryEstimatedAt: candidacy?.readyForJuryEstimatedAt |
| 92 | + ? format(new Date(candidacy.readyForJuryEstimatedAt), "yyyy-MM-dd") |
| 93 | + : null, |
| 94 | + }), |
| 95 | + [candidacy?.readyForJuryEstimatedAt], |
| 96 | + ); |
| 97 | + |
| 98 | + const { |
| 99 | + register, |
| 100 | + handleSubmit, |
| 101 | + reset, |
| 102 | + formState: { isDirty, isSubmitting, errors }, |
| 103 | + watch, |
| 104 | + } = useForm<ActualisationForm>({ |
| 105 | + resolver: zodResolver(schema), |
| 106 | + defaultValues, |
| 107 | + }); |
| 108 | + |
| 109 | + const handleFormSubmit = async ({ |
| 110 | + readyForJuryEstimatedAt, |
| 111 | + }: ActualisationForm) => { |
| 112 | + if (!candidacy?.id || !readyForJuryEstimatedAt) { |
| 113 | + return; |
| 114 | + } |
| 115 | + try { |
| 116 | + await updateLastActivityDate({ |
| 117 | + candidacyId: candidacy?.id, |
| 118 | + readyForJuryEstimatedAt: new Date(readyForJuryEstimatedAt).getTime(), |
| 119 | + }); |
| 120 | + successToast("Votre actualisation est enregistrée"); |
| 121 | + setHasBeenUpdated(true); |
| 122 | + } catch (error) { |
| 123 | + graphqlErrorToast(error); |
| 124 | + } |
| 125 | + }; |
| 126 | + |
| 127 | + const resetForm = useCallback( |
| 128 | + () => reset(defaultValues), |
| 129 | + [reset, defaultValues], |
| 130 | + ); |
| 131 | + |
| 132 | + useEffect(resetForm, [resetForm]); |
10 | 133 |
|
11 | 134 | if (!candidacyActualisationFeatureIsActive) {
|
12 | 135 | return null;
|
13 | 136 | }
|
14 | 137 |
|
15 |
| - return <div>Actualisation</div>; |
| 138 | + return hasBeenUpdated ? ( |
| 139 | + <HasBeenUpdatedComponent |
| 140 | + readyForJuryEstimatedAt={watch("readyForJuryEstimatedAt") as string} |
| 141 | + /> |
| 142 | + ) : ( |
| 143 | + <div className="flex flex-col"> |
| 144 | + <h1 className="mb-0">Recevabilité du candidat</h1> |
| 145 | + <FormOptionalFieldsDisclaimer /> |
| 146 | + <p className="text-xl mb-12"> |
| 147 | + Afin de continuer votre parcours VAE, nous vous demandons de vous |
| 148 | + actualiser tous les 6 mois. Cette action est nécessaire pour que votre |
| 149 | + recevabilité soit toujours valable. |
| 150 | + </p> |
| 151 | + |
| 152 | + <form |
| 153 | + onSubmit={handleSubmit(handleFormSubmit)} |
| 154 | + onReset={(e) => { |
| 155 | + e.preventDefault(); |
| 156 | + resetForm(); |
| 157 | + }} |
| 158 | + > |
| 159 | + <Checkbox |
| 160 | + state={errors.candidateConfirmation ? "error" : "default"} |
| 161 | + stateRelatedMessage={errors.candidateConfirmation?.message} |
| 162 | + options={[ |
| 163 | + { |
| 164 | + label: "Je confirme être toujours en cours de parcours VAE.", |
| 165 | + nativeInputProps: register("candidateConfirmation"), |
| 166 | + }, |
| 167 | + ]} |
| 168 | + className="mb-10" |
| 169 | + data-test="actualisation-candidate-confirmation-checkbox" |
| 170 | + /> |
| 171 | + <h2 className="mb-2"> |
| 172 | + Date prévisionnelle de dépôt du dossier de validation |
| 173 | + </h2> |
| 174 | + <p className="text-lg"> |
| 175 | + La date prévisionnelle de dépôt du dossier de validation est une |
| 176 | + simple estimation, elle ne vous engage pas. Elle permet au |
| 177 | + certificateur d'avoir une idée du moment où vous aurez fini votre |
| 178 | + dossier et d'anticiper l'organisation de votre jury. |
| 179 | + </p> |
| 180 | + <Input |
| 181 | + className="max-w-xs mt-4" |
| 182 | + label="Date prévisionnelle" |
| 183 | + hintText="Si un accompagnateur a renseigné une date, vous la retrouverez ci-dessous." |
| 184 | + nativeInputProps={{ |
| 185 | + type: "date", |
| 186 | + ...register("readyForJuryEstimatedAt"), |
| 187 | + }} |
| 188 | + state={errors.readyForJuryEstimatedAt ? "error" : "default"} |
| 189 | + stateRelatedMessage={errors.readyForJuryEstimatedAt?.message} |
| 190 | + data-test="actualisation-date-input" |
| 191 | + /> |
| 192 | + <FormButtons |
| 193 | + backUrl="/" |
| 194 | + formState={{ |
| 195 | + isDirty, |
| 196 | + isSubmitting, |
| 197 | + }} |
| 198 | + /> |
| 199 | + </form> |
| 200 | + </div> |
| 201 | + ); |
16 | 202 | }
|
0 commit comments