Skip to content

Commit

Permalink
fix: Fix defaultResult validation
Browse files Browse the repository at this point in the history
  • Loading branch information
SteveGT96 committed Oct 18, 2024
1 parent 510416a commit 32aabd2
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 26 deletions.
44 changes: 19 additions & 25 deletions src/components/accessories/admin/exams/examForm/ExamForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -93,26 +93,15 @@ const ExamForm: FC<IExamProps> = ({
code: string().required(t("common.required")),
description: string().required(t("common.required")),
examtype: string().required(t("common.required")),
rows: array()
.of(string().required(t("common.required")))
.test({
name: "min",
exclusive: true,
test: function (value) {
return parseInt(this.parent.procedure) === 3
? true
: (value as string[]).length <= 2;
},
message: t("exam.minLength"),
}),
rows: array().of(string().required(t("common.required"))),
defaultResult: string().test({
name: "procedure-1",
exclusive: true,
test: function (value) {
return (
this.parent.procedure === "1" &&
!isEmpty(value) &&
(this.parent.rows as string[]).includes(value)
this.parent.procedure !== 1 ||
(!isEmpty(value) &&
(this.parent.rows as string[]).some((item) => item == value))
);
},
message: t("exam.invalidDefaultResult"),
Expand Down Expand Up @@ -177,19 +166,24 @@ const ExamForm: FC<IExamProps> = ({
(e: React.FocusEvent<HTMLDivElement>, value: string) => {
handleBlur(e);
setFieldValue(fieldName, value);
if (fieldName === "procedure") {
const rows = formik.values.rows as string[];
if (parseInt(value) === 3) {
setFieldValue("rows", []);
} else if (rows.length < 2) {
setFieldValue("rows", rows.length === 1 ? [rows[0], ""] : ["", ""]);
}
console.log(formik.values.rows);
}
},
[handleBlur, setFieldValue]
);

const handleProcedureChange = useCallback(
(value: string) => {
setFieldValue("procedure", value);
const rows = formik.values.rows as string[];
if (value === "3") {
setFieldValue("rows", []);
} else if (rows.length < 2) {
setFieldValue("rows", rows.length === 1 ? [rows[0], ""] : ["", ""]);
}
setFieldValue("defaultResult", "");
},
[handleBlur, setFieldValue]
);

const handleDefaultResultChange = useCallback(
(e: ChangeEvent<HTMLInputElement>) => {
formik.setFieldValue("defaultResult", e.target.value);
Expand Down Expand Up @@ -266,7 +260,7 @@ const ExamForm: FC<IExamProps> = ({
options={procedureOptions}
errorText={getErrorText("procedure")}
isValid={isValid("procedure")}
onChange={(v) => formik.setFieldValue("procedure", v)}
onChange={handleProcedureChange}
onBlur={formik.handleBlur}
disabled={isLoading}
/>
Expand Down
4 changes: 3 additions & 1 deletion src/components/accessories/admin/exams/examForm/consts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ export const getInitialFields: (
rows: {
type: "text",
isArray: true,
value: JSON.stringify(rows ?? (exam?.procedure !== 3 ? ["", ""] : [])),
value: JSON.stringify(
rows ?? ((exam?.procedure ?? 3) !== 3 ? ["", ""] : [])
),
},
});

0 comments on commit 32aabd2

Please sign in to comment.