fix form add and edit

- add type and code field
- fix field with response
This commit is contained in:
Wikzyy
2025-03-21 09:46:09 +07:00
6 changed files with 336 additions and 82 deletions

View File

@ -20,7 +20,7 @@ const API_URL = apiConfig.service_master_data;
const EditDialog = () => {
const { showEditDialog, handleEditDialog, selectedProfession } = useManageProfessionContext();
const { reload } = useDataGrid();
const { PutData } = useCallApi();
const { PutData, GetData } = useCallApi();
const parsedUser = getAuth()?.user;
const created_time = new Date();
const formattedTime = created_time.toISOString().slice(0, 19).replace('T', ' ');
@ -62,6 +62,19 @@ const EditDialog = () => {
[selectedProfession, formField]
);
const doFetchData = useCallback(async (id: string) => {
const response = await GetData(`${API_URL}/profession/getdata/${id}`, { id });
if (response?.status) {
setFormField((prev) => ({
...prev,
name: response.data.name
}));
} else {
setFormField(initialState);
}
}, []);
const handleUpdate = (e: React.FormEvent<HTMLFormElement>) => {
e.preventDefault();
@ -75,6 +88,12 @@ const EditDialog = () => {
setAlert({ show: false, message: '' });
};
useEffect(() => {
if (selectedProfession) {
doFetchData(selectedProfession);
}
}, [selectedProfession]);
useEffect(() => {
if (showEditDialog) {
setFormField({
@ -85,6 +104,12 @@ const EditDialog = () => {
}
}, [formattedTime]);
useEffect(() => {
if (showEditDialog === false) {
resetForm();
}
}, [showEditDialog]);
return (
<Dialog open={showEditDialog} onOpenChange={(open) => handleEditDialog(open, null)}>
<DialogContent className="container-fixed max-w-[768px] flex flex-col p-5 overflow-hidden">