add loading when post/put data on master data module

This commit is contained in:
Wikzyy
2025-04-23 13:35:32 +07:00
parent e248c463c9
commit 94ffc332e9
26 changed files with 838 additions and 454 deletions

View File

@ -17,6 +17,7 @@ import { Button } from '@/components/ui/button';
import { getAuth, useAuthContext } from '@/auth';
import { useCallApi } from '@/hooks';
import { doSaveLogActivity } from '@/actions/GlobalActions';
import { RefreshCw } from 'lucide-react';
const API_URL = apiConfig.service_master_data;
@ -27,6 +28,7 @@ const EditDialog = () => {
const { PutData, GetData } = useCallApi();
const parsedUser = getAuth()?.user;
const [isLoading, setIsLoading] = useState(false);
const [isSubmitting, setIsSubmitting] = useState(false);
const [alert, setAlert] = useState({
show: false,
message: ''
@ -50,26 +52,34 @@ const EditDialog = () => {
const doUpdateMunicipios = useCallback(
async (e: React.FormEvent<HTMLFormElement>) => {
e.preventDefault();
const response = await PutData(
`${API_URL}/municipios/update/${selectedMunicipios}`,
formField
);
setIsSubmitting(true);
if (response?.status) {
handleEditDialog(false, null);
resetForm();
toast.success('Success update municipio');
reload();
const createActivity = {
module: 'Manage Municipios',
description: `Edit Municipio => ${selectedMunicipios}`,
action: 'U'
};
try {
const response = await PutData(
`${API_URL}/municipios/update/${selectedMunicipios}`,
formField
);
doSaveLogActivity(createActivity);
} else {
toast.error('Failed update user');
setAlert({ show: true, message: 'Failed to update municipio. Please try again.' });
if (response?.status) {
handleEditDialog(false, null);
resetForm();
toast.success('Success update municipio');
reload();
const createActivity = {
module: 'Manage Municipios',
description: `Edit Municipio => ${selectedMunicipios}`,
action: 'U'
};
doSaveLogActivity(createActivity);
} else {
toast.error('Failed update user');
setAlert({ show: true, message: response?.message });
}
} catch (error) {
toast.error('Something went wrong, please try again.');
} finally {
setIsSubmitting(false);
}
},
[selectedMunicipios, formField]
@ -177,7 +187,13 @@ const EditDialog = () => {
</div>
<div className="flex justify-end pt-2.5">
<Button className="btn btn-primary">Save Changes</Button>
<Button variant="default" type="submit" disabled={isSubmitting}>
{isSubmitting ? (
<RefreshCw className="animate-spin h-8 w-8 text-white mx-7" />
) : (
'Save Changes'
)}
</Button>
</div>
</div>
</form>