feat: enhance status management with validation and improved UI components
This commit is contained in:
@@ -2,6 +2,7 @@ import { useEffect, useState } from 'react';
|
||||
import { Modal, Input, Typography, Button, ConfigProvider, Switch } from 'antd';
|
||||
import { NotifAlert, NotifOk } from '../../../../components/Global/ToastNotif';
|
||||
import { createUnit, updateUnit, getAllUnit } from '../../../../api/master-unit';
|
||||
import { validateRun } from '../../../../Utils/validate';
|
||||
|
||||
const { Text } = Typography;
|
||||
|
||||
@@ -26,70 +27,47 @@ const DetailUnit = (props) => {
|
||||
const handleSave = async () => {
|
||||
setConfirmLoading(true);
|
||||
|
||||
// Validasi required fields
|
||||
if (!FormData.unit_name || FormData.unit_name.trim() === '') {
|
||||
NotifOk({
|
||||
icon: 'warning',
|
||||
title: 'Peringatan',
|
||||
message: 'Kolom Name Tidak Boleh Kosong',
|
||||
});
|
||||
setConfirmLoading(false);
|
||||
const validationRules = [
|
||||
{ field: 'unit_name', label: 'Name', required: true },
|
||||
];
|
||||
|
||||
if (
|
||||
validateRun(FormData, validationRules, (errorMessages) => {
|
||||
NotifOk({
|
||||
icon: 'warning',
|
||||
title: 'Peringatan',
|
||||
message: errorMessages,
|
||||
});
|
||||
setConfirmLoading(false);
|
||||
})
|
||||
)
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
if (FormData.unit_id) {
|
||||
// Update existing unit
|
||||
const payload = {
|
||||
name: FormData.unit_name,
|
||||
is_active: FormData.is_active,
|
||||
};
|
||||
const payload = {
|
||||
unit_name: FormData.unit_name,
|
||||
is_active: FormData.is_active,
|
||||
};
|
||||
|
||||
const response = await updateUnit(FormData.unit_id, payload);
|
||||
console.log('updateUnit response:', response);
|
||||
const response = FormData.unit_id
|
||||
? await updateUnit(FormData.unit_id, payload)
|
||||
: await createUnit(payload);
|
||||
|
||||
if (response.statusCode === 200) {
|
||||
// Get updated data to show unit_code in notification
|
||||
const unitCode = response.data?.unit_code || FormData.unit_code;
|
||||
NotifOk({
|
||||
icon: 'success',
|
||||
title: 'Berhasil',
|
||||
message: `Data Unit "${unitCode} - ${FormData.unit_name}" berhasil diubah.`,
|
||||
});
|
||||
props.setActionMode('list');
|
||||
} else {
|
||||
NotifAlert({
|
||||
icon: 'error',
|
||||
title: 'Gagal',
|
||||
message: response.message || 'Gagal mengubah data Unit.',
|
||||
});
|
||||
}
|
||||
if (response.statusCode === 200 || response.statusCode === 201) {
|
||||
const unitCode = response.data?.unit_code || FormData.unit_code || 'N/A';
|
||||
const action = FormData.unit_id ? 'diubah' : 'ditambahkan';
|
||||
NotifOk({
|
||||
icon: 'success',
|
||||
title: 'Berhasil',
|
||||
message: `Data Unit "${unitCode} - ${payload.unit_name}" berhasil ${action}.`,
|
||||
});
|
||||
props.setActionMode('list');
|
||||
} else {
|
||||
// Create new unit
|
||||
const payload = {
|
||||
name: FormData.unit_name,
|
||||
is_active: FormData.is_active,
|
||||
};
|
||||
|
||||
const response = await createUnit(payload);
|
||||
console.log('createUnit response:', response);
|
||||
|
||||
if (response.statusCode === 200 || response.statusCode === 201) {
|
||||
// Get unit_code from response
|
||||
const unitCode = response.data?.unit_code || 'N/A';
|
||||
NotifOk({
|
||||
icon: 'success',
|
||||
title: 'Berhasil',
|
||||
message: `Data Unit "${unitCode} - ${FormData.unit_name}" berhasil ditambahkan.`,
|
||||
});
|
||||
props.setActionMode('list');
|
||||
} else {
|
||||
NotifAlert({
|
||||
icon: 'error',
|
||||
title: 'Gagal',
|
||||
message: response.message || 'Gagal menambahkan data Unit.',
|
||||
});
|
||||
}
|
||||
NotifAlert({
|
||||
icon: 'error',
|
||||
title: 'Gagal',
|
||||
message: response.message || 'Gagal menyimpan data Unit.',
|
||||
});
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Save Unit Error:', error);
|
||||
@@ -98,9 +76,9 @@ const DetailUnit = (props) => {
|
||||
title: 'Error',
|
||||
message: error.message || 'Terjadi kesalahan saat menyimpan data.',
|
||||
});
|
||||
} finally {
|
||||
setConfirmLoading(false);
|
||||
}
|
||||
|
||||
setConfirmLoading(false);
|
||||
};
|
||||
|
||||
const handleInputChange = (e) => {
|
||||
@@ -298,4 +276,4 @@ const DetailUnit = (props) => {
|
||||
);
|
||||
};
|
||||
|
||||
export default DetailUnit;
|
||||
export default DetailUnit;
|
||||
Reference in New Issue
Block a user