fixing add dialog manage user & manage position and edit dialog manage user

This commit is contained in:
Raja Oktafrianto
2025-03-25 14:53:54 +07:00
parent e9b74f27af
commit f1a7a839e8
3 changed files with 50 additions and 16 deletions

View File

@ -1,4 +1,4 @@
import { useCallback, useRef, useState } from 'react'; import { useCallback, useEffect, useRef, useState } from 'react';
import { import {
Dialog, Dialog,
@ -59,6 +59,10 @@ const MenuItemComponent: React.FC<{
); );
}; };
const initialState = {
name: ''
};
const AddDialog = () => { const AddDialog = () => {
const parentRef = useRef<any | null>(null); const parentRef = useRef<any | null>(null);
const { showAddDialog, handleAddDialog, menus } = useManagePositionContext(); const { showAddDialog, handleAddDialog, menus } = useManagePositionContext();
@ -69,9 +73,7 @@ const AddDialog = () => {
message: '' message: ''
}); });
const [selectMenus, setSelectMenus] = useState<string[]>([]); const [selectMenus, setSelectMenus] = useState<string[]>([]);
const [formField, setFormField] = useState({ const [formField, setFormField] = useState(initialState);
name: ''
});
/* actions */ /* actions */
const handleCheckboxChange = useCallback((key: string) => { const handleCheckboxChange = useCallback((key: string) => {
@ -80,6 +82,12 @@ const AddDialog = () => {
); );
}, []); }, []);
const resetForm = () => {
setFormField(() => ({ name: '' }));
setAlert({ show: false, message: '' });
setSelectMenus([]);
};
const doCreatePosition = useCallback( const doCreatePosition = useCallback(
async (e: React.FormEvent<HTMLFormElement>) => { async (e: React.FormEvent<HTMLFormElement>) => {
e.preventDefault(); e.preventDefault();
@ -96,7 +104,7 @@ const AddDialog = () => {
}); });
if (response?.status) { if (response?.status) {
setAlert((prev) => ({ ...prev, show: false, message: '' })); resetForm();
handleAddDialog(false); handleAddDialog(false);
toast.success('Success Create Position'); toast.success('Success Create Position');
reload(); reload();
@ -114,6 +122,12 @@ const AddDialog = () => {
[formField, selectMenus] [formField, selectMenus]
); );
useEffect(() => {
if (showAddDialog === false) {
resetForm();
}
}, [showAddDialog]);
return ( return (
<Dialog open={showAddDialog} onOpenChange={(open) => handleAddDialog(open)}> <Dialog open={showAddDialog} onOpenChange={(open) => handleAddDialog(open)}>
<DialogContent className="container-fixed max-w-screen-lg flex flex-col p-10 overflow-hidden [&>button]:hidden"> <DialogContent className="container-fixed max-w-screen-lg flex flex-col p-10 overflow-hidden [&>button]:hidden">

View File

@ -59,6 +59,7 @@ const AddDialog = () => {
const [formField, setFormField] = useState(initialState); const [formField, setFormField] = useState(initialState);
const resetForm = () => { const resetForm = () => {
setFormField(initialState); setFormField(initialState);
setAlert({ show: false, message: '' });
}; };
const [showPassword, setShowPassword] = useState({ const [showPassword, setShowPassword] = useState({
password: false, password: false,
@ -104,6 +105,12 @@ const AddDialog = () => {
const isButtonDisabled = !messagePassword || isSubmitting || passwordErrors.length > 0; const isButtonDisabled = !messagePassword || isSubmitting || passwordErrors.length > 0;
useEffect(() => {
if (showAddDialog === false) {
resetForm();
}
}, [showAddDialog]);
/* actions */ /* actions */
const doCreateUser = useCallback( const doCreateUser = useCallback(
async (e: React.FormEvent<HTMLFormElement>) => { async (e: React.FormEvent<HTMLFormElement>) => {
@ -118,7 +125,7 @@ const AddDialog = () => {
`${API_URL}/user/add_role/${response?.message?.id}/${formField.id_role}`, `${API_URL}/user/add_role/${response?.message?.id}/${formField.id_role}`,
{} {}
); );
setAlert((prev) => ({ ...prev, show: false, message: '' })); resetForm();
handleAddDialog(false); handleAddDialog(false);
toast.success('Success Create User'); toast.success('Success Create User');
reload(); reload();
@ -337,7 +344,6 @@ const AddDialog = () => {
</div> </div>
</div> </div>
<div className="flex justify-end pt-2.5"> <div className="flex justify-end pt-2.5">
<Button className="btn btn-primary" type="submit" disabled={isButtonDisabled}> <Button className="btn btn-primary" type="submit" disabled={isButtonDisabled}>
Save Changes Save Changes

View File

@ -26,6 +26,15 @@ import { doSaveLogActivity } from '@/actions/GlobalActions';
const API_URL = apiConfig.service_dashboard; const API_URL = apiConfig.service_dashboard;
const initialState = {
name: '',
username: '',
email: '',
id_role: '',
id_role_old: '',
status: ''
};
const EditDialog = () => { const EditDialog = () => {
const parentRef = useRef<any | null>(null); const parentRef = useRef<any | null>(null);
const { showEditDialog, selectedUser, handleEditDialog, roles } = useUserContext(); const { showEditDialog, selectedUser, handleEditDialog, roles } = useUserContext();
@ -35,16 +44,14 @@ const EditDialog = () => {
show: false, show: false,
message: '' message: ''
}); });
const initialState = {
name: '',
username: '',
email: '',
id_role: '',
id_role_old: '',
status: ''
};
const [formField, setFormField] = useState(initialState); const [formField, setFormField] = useState(initialState);
const resetForm = () => {
setFormField(initialState);
setAlert({ show: false, message: '' });
};
/* actions */ /* actions */
const doResetForm = () => { const doResetForm = () => {
setAlert({ show: false, message: '' }); setAlert({ show: false, message: '' });
@ -58,7 +65,14 @@ const EditDialog = () => {
...formField, ...formField,
id_role_old: undefined id_role_old: undefined
}); });
if (formField.name.trim() === '') {
setAlert({ show: true, message: 'Please fill all required field' });
return;
}
if (response?.status) { if (response?.status) {
resetForm();
handleEditDialog(false, null); handleEditDialog(false, null);
toast.success('Success Update User'); toast.success('Success Update User');
reload(); reload();
@ -111,7 +125,7 @@ const EditDialog = () => {
useEffect(() => { useEffect(() => {
if (showEditDialog === false) { if (showEditDialog === false) {
doResetForm(); resetForm();
} }
}, [showEditDialog]); }, [showEditDialog]);