83 lines
2.3 KiB
TypeScript
83 lines
2.3 KiB
TypeScript
import React, { useEffect } from "react";
|
|
import { Modal, Spin } from "antd";
|
|
import { useModalState } from "../../../../../../zustand/Modal";
|
|
import FormikForm from "../../../../../../Layout/Dashboard/FormikFormModel";
|
|
import ModelBody from "./AddUnit";
|
|
import { getInitialValues, getValidationSchema } from "./formUtil";
|
|
import { ModalEnum } from "../../../../../../enums/Model";
|
|
import { useUpdateUnit } from "../../../../../../api/unit";
|
|
import { useObjectToEdit } from "../../../../../../zustand/ObjectToEditState";
|
|
import { useParams } from "react-router-dom";
|
|
import { useTranslation } from "react-i18next";
|
|
|
|
const ModalForm: React.FC = () => {
|
|
const { isOpen, setIsOpen } = useModalState((state) => state);
|
|
|
|
const { mutate, isSuccess, isLoading } = useUpdateUnit();
|
|
const { object_to_edit, set_object_to_edit } = useObjectToEdit();
|
|
|
|
// console.log(object_to_edit,"object_to_edit");
|
|
const { subject_id } = useParams();
|
|
|
|
useEffect(() => {
|
|
if (isSuccess) {
|
|
setIsOpen("");
|
|
}
|
|
}, [setIsOpen, isSuccess]);
|
|
|
|
const handleSubmit = (values: any) => {
|
|
// console.log(values,"values");
|
|
mutate({
|
|
...values,
|
|
subject_id: subject_id,
|
|
});
|
|
};
|
|
|
|
const handleCancel = () => {
|
|
setIsOpen("");
|
|
};
|
|
|
|
const [t] = useTranslation();
|
|
return (
|
|
<>
|
|
<Modal
|
|
className="ModalForm"
|
|
centered
|
|
width={"40vw"}
|
|
footer={null}
|
|
open={isOpen === ModalEnum?.UNIT_EDIT}
|
|
onCancel={handleCancel}
|
|
>
|
|
<FormikForm
|
|
handleSubmit={handleSubmit}
|
|
initialValues={getInitialValues(object_to_edit)}
|
|
validationSchema={getValidationSchema}
|
|
>
|
|
<header>
|
|
{" "}
|
|
{t("practical.edit")}
|
|
{t("models.unit")}{" "}
|
|
</header>
|
|
<main className="main_modal w-100">
|
|
<ModelBody />
|
|
<div className="buttons">
|
|
<div onClick={handleCancel}>{t("practical.cancel")}</div>
|
|
<button disabled={isLoading} type="submit">
|
|
{t("practical.edit")}
|
|
|
|
{isLoading && (
|
|
<span className="Spinier_Div">
|
|
<Spin />
|
|
</span>
|
|
)}
|
|
</button>
|
|
</div>
|
|
</main>
|
|
</FormikForm>
|
|
</Modal>
|
|
</>
|
|
);
|
|
};
|
|
|
|
export default ModalForm;
|