Skip to content

Commit

Permalink
feat(contrib): désactivation du formulaire quand on soumet
Browse files Browse the repository at this point in the history
  • Loading branch information
carolineBda committed Oct 12, 2023
1 parent 59e35bb commit dc72c7b
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export const ContributionsAnswer = ({
throw new Error("Id non définit");
}

await updateAnswer({
const res = await updateAnswer({
content: data.content,
id: answer.id,
contentType: data.contentType,
Expand All @@ -63,6 +63,7 @@ export const ContributionsAnswer = ({
severity: "success",
message: "La réponse a été modifiée",
});
return res;
} catch (e: any) {
setSnack({ open: true, severity: "error", message: e.message });
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { zodResolver } from "@hookform/resolvers/zod";
import { z } from "zod";

import { FormEditionField, FormRadioGroup, FormTextField } from "../../forms";
import { Answer, Status, answerRelationSchema, documentSchema } from "../type";
import { Answer, answerRelationSchema, documentSchema, Status } from "../type";
import { AnswerWithStatus } from "./answer.query";
import {
CdtnReferenceInput,
Expand Down Expand Up @@ -67,14 +67,9 @@ export type AnswerFormValidation = z.infer<typeof answerFormSchema>;

export type ContributionsAnswerProps = {
answer: AnswerWithStatus;
onSubmit: (status: Status, data: Answer) => void;
onSubmit: (status: Status, data: Answer) => Promise<any>;
};

const isNotEditable = (answer: Answer | undefined) =>
answer?.status?.status !== "REDACTING" &&
answer?.status?.status !== "TODO" &&
answer?.status?.status !== "VALIDATING";

const isCodeDuTravail = (answer: Answer): boolean =>
answer?.agreement?.id === "0000";

Expand All @@ -83,6 +78,7 @@ export const AnswerForm = ({
onSubmit,
}: ContributionsAnswerProps): JSX.Element => {
const [status, setStatus] = useState<Status>("TODO");
const [submitting, setSubmitting] = useState(false);
useEffect(() => {
if (answer?.status) {
setStatus(answer.status.status);
Expand All @@ -103,10 +99,21 @@ export const AnswerForm = ({
},
});

const isNotEditable = (answer: Answer | undefined) => {
return (
submitting ||
(answer?.status?.status !== "REDACTING" &&
answer?.status?.status !== "TODO" &&
answer?.status?.status !== "VALIDATING")
);
};

const submit = async (newStatus: Status) => {
setSubmitting(true);
if (!isNotEditable(answer)) {
const isValid = await trigger();
if (!isValid) {
setSubmitting(false);
return;
}
}
Expand All @@ -115,7 +122,7 @@ export const AnswerForm = ({
onSubmit(newStatus, {
...answer,
...formData,
});
}).then(() => setSubmitting(false));
};

const agreementResponseOptions = [
Expand Down Expand Up @@ -223,33 +230,36 @@ export const AnswerForm = ({
: undefined
}
/>
<Stack direction="row" justifyContent="end" spacing={2} padding={2}>
<Button
variant="outlined"
type="button"
onClick={() => submit("REDACTING")}
disabled={status === "TODO" || status === "REDACTING"}
>
Remettre en rédaction
</Button>
<Button
variant="text"
type="button"
disabled={isNotEditable(answer)}
onClick={() => submit("REDACTING")}
>
Sauvegarder
</Button>
<Button
variant="contained"
type="button"
color="success"
onClick={() => submit(getNextStatus(status))}
disabled={status === "PUBLISHED"}
>
{getPrimaryButtonLabel(status)}
</Button>
</Stack>

{!submitting && (
<Stack direction="row" justifyContent="end" spacing={2} padding={2}>
<Button
variant="outlined"
type="button"
onClick={() => submit("REDACTING")}
disabled={status === "TODO" || status === "REDACTING"}
>
Remettre en rédaction
</Button>
<Button
variant="text"
type="button"
disabled={isNotEditable(answer)}
onClick={() => submit("REDACTING")}
>
Sauvegarder
</Button>
<Button
variant="contained"
type="button"
color="success"
onClick={() => submit(getNextStatus(status))}
disabled={submitting || status === "PUBLISHED"}
>
{getPrimaryButtonLabel(status)}
</Button>
</Stack>
)}
</Stack>
</form>
</>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export const initStatus = (answer: Answer) => {
};

export const getNextStatus = (status: Status): Status => {
console.log(getNextStatus, status);
switch (status) {
case "REDACTED":
return "VALIDATING";
Expand Down

0 comments on commit dc72c7b

Please sign in to comment.