Skip to content

Commit

Permalink
fix(contributions): make button behavior right on answer form (#962)
Browse files Browse the repository at this point in the history
  • Loading branch information
carolineBda authored Jun 29, 2023
1 parent 1bb8628 commit 9bdf988
Showing 1 changed file with 23 additions and 16 deletions.
39 changes: 23 additions & 16 deletions targets/frontend/src/components/contributions/answers/Answer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ export const ContributionsAnswer = ({
setStatus(answer.status.status);
}
}, [answer]);
const { control, handleSubmit, watch } = useForm<AnswerForm>({
const { control, getValues, trigger, watch } = useForm<AnswerForm>({
defaultValues: {
content: answer?.content ?? "",
otherAnswer: answer?.otherAnswer ?? "ANSWER",
Expand All @@ -91,7 +91,20 @@ export const ContributionsAnswer = ({
}>({
open: false,
});
const onSubmit = async (data: AnswerForm) => {

const onSubmit = async (newStatus: Status) => {
const isValid = await trigger();
if (!isValid) {
return setSnack({
open: true,
severity: "error",
message: "Formulaire invalide",
});
}

setStatus(newStatus);
const data = getValues();

try {
if (!answer || !answer.id) {
throw new Error("Id non définit");
Expand All @@ -101,7 +114,7 @@ export const ContributionsAnswer = ({
content: data.content,
id: answer.id,
otherAnswer: data.otherAnswer,
status,
status: newStatus,
userId: user?.id,
kali_references: formatKaliReferences(answer, data),
legi_references: formatLegiReferences(answer, data),
Expand Down Expand Up @@ -140,7 +153,7 @@ export const ContributionsAnswer = ({
<h2>{answer?.agreement?.name}</h2>
<Box sx={{ display: "flex", flexDirection: "row" }}>
<Box sx={{ width: "70%" }}>
<form onSubmit={handleSubmit(onSubmit)}>
<form>
<Stack spacing={5}>
<FormControl>
<FormEditionField
Expand Down Expand Up @@ -206,31 +219,25 @@ export const ContributionsAnswer = ({
>
<Button
variant="outlined"
type="submit"
onClick={() => setStatus("REDACTING")}
type="button"
onClick={() => onSubmit("REDACTING")}
disabled={status === "TODO" || status === "REDACTING"}
>
Remettre en rédaction
</Button>
<Button
variant="text"
type="submit"
type="button"
disabled={isNotEditable(answer)}
onClick={() => {
if (status === "TODO") {
setStatus("REDACTING");
}
}}
onClick={() => onSubmit("REDACTING")}
>
Sauvegarder
</Button>
<Button
variant="contained"
type="button"
color="success"
type="submit"
onClick={() => {
setStatus(getNextStatus(status));
}}
onClick={() => onSubmit(getNextStatus(status))}
disabled={status === "PUBLISHED"}
>
{getPrimaryButtonLabel(status)}
Expand Down

0 comments on commit 9bdf988

Please sign in to comment.