Skip to content

Commit

Permalink
Merge branch 'master' into feat/contribution-zod
Browse files Browse the repository at this point in the history
  • Loading branch information
Victor Zeinstra committed Oct 6, 2023
2 parents 13db745 + 2a0ace5 commit d471a06
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,7 @@ export const AnswerForm = ({
setStatus(answer.status.status);
}
}, [answer]);
const {
control,
getValues,
trigger,
formState: { errors },
} = useForm<Answer>({
const { control, getValues, trigger } = useForm<Answer>({
resolver: zodResolver(answerSchema),
shouldFocusError: true,
defaultValues: {
Expand Down Expand Up @@ -75,6 +70,15 @@ export const AnswerForm = ({
label: "La convention collective ne prévoit rien",
value: "NOTHING",
},
{
label: "La convention collective renvoie au Code du Travail",
value: "CDT",
},
{
label:
"La convention collective intégralement moins favorable que le CDT",
value: "UNFAVOURABLE",
},
{
label: "Nous n'avons pas la réponse",
value: "UNKNOWN",
Expand Down
17 changes: 16 additions & 1 deletion targets/frontend/src/components/contributions/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,14 @@ export const cdtnReferenceSchema = z.object({
});
export type CdtnReference = z.infer<typeof cdtnReferenceSchema>;

export const contentTypeSchema = z.enum(["ANSWER", "NOTHING", "UNKNOWN", "SP"]);
export const contentTypeSchema = z.enum([
"ANSWER",
"NOTHING",
"CDT",
"UNFAVOURABLE",
"UNKNOWN",
"SP",
]);
export type ContentType = z.infer<typeof contentTypeSchema>;

const answerBaseSchema = z.object({
Expand Down Expand Up @@ -168,6 +175,12 @@ const answerWithAnswerSchema = answerRelationSchema.extend({
const answerWithNothingSchema = answerRelationSchema.extend({
contentType: z.literal("NOTHING"),
});
const answerWithCdtSchema = answerRelationSchema.extend({
contentType: z.literal("CDT"),
});
const answerWithUnfavourableSchema = answerRelationSchema.extend({
contentType: z.literal("UNFAVOURABLE"),
});
const answerWithUnknownSchema = answerRelationSchema.extend({
contentType: z.literal("UNKNOWN"),
});
Expand All @@ -179,6 +192,8 @@ const answerWithSPSchema = answerRelationSchema.extend({
export const answerSchema = z.discriminatedUnion("contentType", [
answerWithAnswerSchema,
answerWithNothingSchema,
answerWithCdtSchema,
answerWithUnfavourableSchema,
answerWithUnknownSchema,
answerWithSPSchema,
]);
Expand Down
59 changes: 30 additions & 29 deletions targets/frontend/src/components/forms/RadioGroup/index.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import {
FormControl,
FormControlLabel,
FormLabel,
Radio,
RadioGroup,
FormHelperText,
Expand All @@ -10,6 +9,7 @@ import {
import React, { PropsWithChildren } from "react";
import { Controller } from "react-hook-form";
import { CommonFormProps } from "../type";
import { TitleBox } from "../TitleBox";

type OptionProps = {
label: string;
Expand All @@ -36,34 +36,35 @@ export const FormRadioGroup = ({
name={name}
control={control}
rules={rules}
render={({ field: { onChange, value }, fieldState: { error } }) => {
return (
<FormControl fullWidth={fullWidth} error={!!error}>
<FormLabel>{label}</FormLabel>
<RadioGroup
value={value}
onChange={(event: React.ChangeEvent<HTMLInputElement>) =>
onChange(event.target.value)
}
>
{options.map(({ label, value }) => (
<FormControlLabel
key={value}
value={value}
control={<Radio />}
label={label}
disabled={disabled}
/>
))}
</RadioGroup>
{error && error.message === "Required" && (
<StyledFormHelperText>
Un élément doit être sélectionner
</StyledFormHelperText>
)}
</FormControl>
);
}}
render={({ field: { onChange, value }, fieldState: { error } }) => (
<FormControl fullWidth={fullWidth} error={!!error}>
<TitleBox title={label} disabled={disabled}>
<>
<RadioGroup
value={value}
onChange={(event: React.ChangeEvent<HTMLInputElement>) =>
onChange(event.target.value)
}
>
{options.map(({ label, value }) => (
<FormControlLabel
key={value}
value={value}
control={<Radio />}
label={label}
disabled={disabled}
/>
))}
</RadioGroup>
{error && error.message === "Required" && (
<StyledFormHelperText>
Un élément doit être sélectionner
</StyledFormHelperText>
)}
</>
</TitleBox>
</FormControl>
)}
/>
);
};
Expand Down

0 comments on commit d471a06

Please sign in to comment.