clean code master plant sub section and master device
This commit is contained in:
@@ -1,20 +1,8 @@
|
||||
import React, { useEffect, useState } from 'react';
|
||||
import {
|
||||
Modal,
|
||||
Input,
|
||||
Divider,
|
||||
Typography,
|
||||
Switch,
|
||||
Button,
|
||||
ConfigProvider,
|
||||
Radio,
|
||||
Select,
|
||||
} from 'antd';
|
||||
import { Modal, Input, Divider, Typography, Switch, Button, ConfigProvider } from 'antd';
|
||||
import { NotifAlert, NotifOk } from '../../../../components/Global/ToastNotif';
|
||||
import { createApd, getJenisPermit, updateApd } from '../../../../api/master-apd';
|
||||
import { createDevice, updateDevice, getAllDevice } from '../../../../api/master-device';
|
||||
import { Checkbox } from 'antd';
|
||||
const CheckboxGroup = Checkbox.Group;
|
||||
import { createDevice, updateDevice } from '../../../../api/master-device';
|
||||
import { validateRun } from '../../../../Utils/validate';
|
||||
|
||||
const { Text } = Typography;
|
||||
const { TextArea } = Input;
|
||||
@@ -27,156 +15,60 @@ const DetailDevice = (props) => {
|
||||
device_code: '',
|
||||
device_name: '',
|
||||
is_active: true,
|
||||
device_location: 'Building A',
|
||||
device_location: '',
|
||||
device_description: '',
|
||||
ip_address: '',
|
||||
};
|
||||
|
||||
const [FormData, setFormData] = useState(defaultData);
|
||||
const [nextDeviceCode, setNextDeviceCode] = useState('Auto-fill');
|
||||
|
||||
const [jenisPermit, setJenisPermit] = useState([]);
|
||||
const [checkedList, setCheckedList] = useState([]);
|
||||
|
||||
const onChange = (list) => {
|
||||
setCheckedList(list);
|
||||
};
|
||||
|
||||
const onChangeRadio = (e) => {
|
||||
setFormData({
|
||||
...FormData,
|
||||
type_input: e.target.value,
|
||||
});
|
||||
};
|
||||
|
||||
const getDataJenisPermit = async () => {
|
||||
setCheckedList([]);
|
||||
const result = await getJenisPermit();
|
||||
const data = result.data ?? [];
|
||||
const names = data.map((item) => ({
|
||||
value: item.id_jenis_permit,
|
||||
label: item.nama_jenis_permit,
|
||||
}));
|
||||
setJenisPermit(names);
|
||||
};
|
||||
const [formData, setFormData] = useState(defaultData);
|
||||
|
||||
const handleCancel = () => {
|
||||
props.setSelectedData(null);
|
||||
props.setActionMode('list');
|
||||
};
|
||||
|
||||
const validateIPAddress = (ip) => {
|
||||
const ipRegex =
|
||||
/^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/;
|
||||
return ipRegex.test(ip);
|
||||
};
|
||||
|
||||
const handleSave = async () => {
|
||||
setConfirmLoading(true);
|
||||
|
||||
// Validasi required fields
|
||||
// if (!FormData.device_code) {
|
||||
// NotifOk({
|
||||
// icon: 'warning',
|
||||
// title: 'Peringatan',
|
||||
// message: 'Kolom Device Code Tidak Boleh Kosong',
|
||||
// });
|
||||
// setConfirmLoading(false);
|
||||
// return;
|
||||
// }
|
||||
// Daftar aturan validasi
|
||||
const validationRules = [
|
||||
{ field: 'device_name', label: 'Device Name', required: true },
|
||||
{ field: 'ip_address', label: 'Ip Address', required: true, ip: true },
|
||||
];
|
||||
|
||||
if (!FormData.device_name) {
|
||||
NotifOk({
|
||||
icon: 'warning',
|
||||
title: 'Peringatan',
|
||||
message: 'Kolom Device Name Tidak Boleh Kosong',
|
||||
});
|
||||
setConfirmLoading(false);
|
||||
if (
|
||||
validateRun(formData, validationRules, (errorMessages) => {
|
||||
NotifOk({
|
||||
icon: 'warning',
|
||||
title: 'Peringatan',
|
||||
message: errorMessages,
|
||||
});
|
||||
setConfirmLoading(false);
|
||||
})
|
||||
)
|
||||
return;
|
||||
}
|
||||
|
||||
if (!FormData.device_location) {
|
||||
NotifOk({
|
||||
icon: 'warning',
|
||||
title: 'Peringatan',
|
||||
message: 'Kolom Device Location Tidak Boleh Kosong',
|
||||
});
|
||||
setConfirmLoading(false);
|
||||
return;
|
||||
}
|
||||
|
||||
if (!FormData.ip_address) {
|
||||
NotifOk({
|
||||
icon: 'warning',
|
||||
title: 'Peringatan',
|
||||
message: 'Kolom IP Address Tidak Boleh Kosong',
|
||||
});
|
||||
setConfirmLoading(false);
|
||||
return;
|
||||
}
|
||||
|
||||
// Validasi format IP
|
||||
if (!validateIPAddress(FormData.ip_address)) {
|
||||
NotifOk({
|
||||
icon: 'warning',
|
||||
title: 'Peringatan',
|
||||
message: 'Format IP Address Tidak Valid',
|
||||
});
|
||||
setConfirmLoading(false);
|
||||
return;
|
||||
}
|
||||
|
||||
if (props.permitDefault && checkedList.length === 0) {
|
||||
NotifOk({
|
||||
icon: 'warning',
|
||||
title: 'Peringatan',
|
||||
message: 'Kolom Jenis Permit Tidak Boleh Kosong',
|
||||
});
|
||||
|
||||
setConfirmLoading(false);
|
||||
return;
|
||||
}
|
||||
|
||||
// Backend validation schema doesn't include device_code
|
||||
const payload = {
|
||||
device_name: FormData.device_name,
|
||||
is_active: FormData.is_active,
|
||||
device_location: FormData.device_location,
|
||||
ip_address: FormData.ip_address,
|
||||
};
|
||||
|
||||
// For CREATE: device_description is required (cannot be empty)
|
||||
// For UPDATE: device_description is optional
|
||||
if (!FormData.device_id) {
|
||||
// Creating - ensure description is not empty
|
||||
payload.device_description = FormData.device_description || '-';
|
||||
} else {
|
||||
// Updating - include description as-is
|
||||
payload.device_description = FormData.device_description;
|
||||
}
|
||||
|
||||
console.log('Payload to send:', payload);
|
||||
|
||||
try {
|
||||
let response;
|
||||
if (!FormData.device_id) {
|
||||
response = await createDevice(payload);
|
||||
} else {
|
||||
response = await updateDevice(FormData.device_id, payload);
|
||||
}
|
||||
const payload = {
|
||||
device_name: formData.device_name,
|
||||
is_active: formData.is_active,
|
||||
device_location: formData.device_location,
|
||||
ip_address: formData.ip_address,
|
||||
};
|
||||
|
||||
console.log('Save Device Response:', response);
|
||||
const response = !formData.device_id
|
||||
? await updateDevice(formData.device_id, payload)
|
||||
: await createDevice(payload);
|
||||
|
||||
// Check if response is successful
|
||||
if (response && (response.statusCode === 200 || response.statusCode === 201)) {
|
||||
// Response.data is now a single object (already extracted from array)
|
||||
const deviceName = response.data?.device_name || FormData.device_name;
|
||||
const deviceName = response.data?.device_name || formData.device_name;
|
||||
|
||||
NotifOk({
|
||||
icon: 'success',
|
||||
title: 'Berhasil',
|
||||
message: `Data Device "${deviceName}" berhasil ${
|
||||
FormData.device_id ? 'diubah' : 'ditambahkan'
|
||||
formData.device_id ? 'diubah' : 'ditambahkan'
|
||||
}.`,
|
||||
});
|
||||
|
||||
@@ -203,7 +95,7 @@ const DetailDevice = (props) => {
|
||||
const handleInputChange = (e) => {
|
||||
const { name, value } = e.target;
|
||||
setFormData({
|
||||
...FormData,
|
||||
...formData,
|
||||
[name]: value,
|
||||
});
|
||||
};
|
||||
@@ -211,78 +103,22 @@ const DetailDevice = (props) => {
|
||||
const handleStatusToggle = (event) => {
|
||||
const isChecked = event;
|
||||
setFormData({
|
||||
...FormData,
|
||||
...formData,
|
||||
is_active: isChecked ? true : false,
|
||||
});
|
||||
};
|
||||
|
||||
const generateNextDeviceCode = async () => {
|
||||
try {
|
||||
const params = new URLSearchParams({ limit: 10000 });
|
||||
const response = await getAllDevice(params);
|
||||
|
||||
if (response && response.data && response.data.data) {
|
||||
const devices = response.data.data;
|
||||
|
||||
if (devices.length === 0) {
|
||||
setNextDeviceCode('DVC001');
|
||||
return;
|
||||
}
|
||||
|
||||
// Extract numeric part from device codes and find the maximum
|
||||
const deviceNumbers = devices
|
||||
.map((device) => {
|
||||
const match = device.device_code?.match(/dvc(\d+)/i);
|
||||
return match ? parseInt(match[1], 10) : 0;
|
||||
})
|
||||
.filter((num) => !isNaN(num));
|
||||
|
||||
const maxNumber = deviceNumbers.length > 0 ? Math.max(...deviceNumbers) : 0;
|
||||
const nextNumber = maxNumber + 1;
|
||||
|
||||
// Format with leading zeros (DVC001, DVC002, etc.)
|
||||
const nextCode = `DVC${String(nextNumber).padStart(3, '0')}`;
|
||||
setNextDeviceCode(nextCode);
|
||||
} else {
|
||||
setNextDeviceCode('DVC001');
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Error generating next device code:', error);
|
||||
setNextDeviceCode('Auto-fill');
|
||||
}
|
||||
};
|
||||
|
||||
useEffect(() => {
|
||||
const token = localStorage.getItem('token');
|
||||
if (token) {
|
||||
if (props.showModal) {
|
||||
// Only call getDataJenisPermit if permitDefault is enabled
|
||||
if (props.permitDefault) {
|
||||
getDataJenisPermit();
|
||||
}
|
||||
|
||||
// Generate next device code only for add mode
|
||||
if (props.actionMode === 'add' && !props.selectedData) {
|
||||
generateNextDeviceCode();
|
||||
}
|
||||
}
|
||||
|
||||
if (props.selectedData != null) {
|
||||
setFormData(props.selectedData);
|
||||
if (props.permitDefault && props.selectedData.jenis_permit_default_arr) {
|
||||
setCheckedList(props.selectedData.jenis_permit_default_arr);
|
||||
}
|
||||
} else {
|
||||
setFormData(defaultData);
|
||||
}
|
||||
if (props.selectedData) {
|
||||
setFormData(props.selectedData);
|
||||
} else {
|
||||
// navigate('/signin'); // Uncomment if useNavigate is imported
|
||||
setFormData(defaultData);
|
||||
}
|
||||
}, [props.showModal, props.actionMode]);
|
||||
}, [props.showModal, props.selectedData, props.actionMode]);
|
||||
|
||||
return (
|
||||
<Modal
|
||||
// title={`${FormData.id_apd === '' ? 'Tambah' : 'Edit'} APD`}
|
||||
// title={`${formData.id_apd === '' ? 'Tambah' : 'Edit'} APD`}
|
||||
title={`${
|
||||
props.actionMode === 'add'
|
||||
? 'Tambah'
|
||||
@@ -337,7 +173,7 @@ const DetailDevice = (props) => {
|
||||
</React.Fragment>,
|
||||
]}
|
||||
>
|
||||
{FormData && (
|
||||
{formData && (
|
||||
<div>
|
||||
<div>
|
||||
<div>
|
||||
@@ -355,14 +191,14 @@ const DetailDevice = (props) => {
|
||||
disabled={props.readOnly}
|
||||
style={{
|
||||
backgroundColor:
|
||||
FormData.is_active === true ? '#23A55A' : '#bfbfbf',
|
||||
formData.is_active === true ? '#23A55A' : '#bfbfbf',
|
||||
}}
|
||||
checked={FormData.is_active === true}
|
||||
checked={formData.is_active === true}
|
||||
onChange={handleStatusToggle}
|
||||
/>
|
||||
</div>
|
||||
<div>
|
||||
<Text>{FormData.is_active === true ? 'Running' : 'Offline'}</Text>
|
||||
<Text>{formData.is_active === true ? 'Running' : 'Offline'}</Text>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
@@ -371,7 +207,7 @@ const DetailDevice = (props) => {
|
||||
<Text strong>Device ID</Text>
|
||||
<Input
|
||||
name="device_id"
|
||||
value={FormData.device_id}
|
||||
value={formData.device_id}
|
||||
onChange={handleInputChange}
|
||||
disabled
|
||||
/>
|
||||
@@ -381,13 +217,13 @@ const DetailDevice = (props) => {
|
||||
<Text strong>Device Code</Text>
|
||||
<Input
|
||||
name="device_code"
|
||||
value={FormData.device_code || nextDeviceCode}
|
||||
placeholder={nextDeviceCode}
|
||||
value={formData.device_code}
|
||||
placeholder={'Device Code Auto Fill'}
|
||||
disabled
|
||||
style={{
|
||||
backgroundColor: '#f5f5f5',
|
||||
cursor: 'not-allowed',
|
||||
color: FormData.device_code ? '#000000' : '#bfbfbf'
|
||||
color: formData.device_code ? '#000000' : '#bfbfbf',
|
||||
}}
|
||||
/>
|
||||
</div>
|
||||
@@ -396,7 +232,7 @@ const DetailDevice = (props) => {
|
||||
<Text style={{ color: 'red' }}> *</Text>
|
||||
<Input
|
||||
name="device_name"
|
||||
value={FormData.device_name}
|
||||
value={formData.device_name}
|
||||
onChange={handleInputChange}
|
||||
placeholder="Enter Device Name"
|
||||
readOnly={props.readOnly}
|
||||
@@ -404,11 +240,10 @@ const DetailDevice = (props) => {
|
||||
</div>
|
||||
<div style={{ marginBottom: 12 }}>
|
||||
<Text strong>Device Location</Text>
|
||||
<Text style={{ color: 'red' }}> *</Text>
|
||||
<Input
|
||||
type="text"
|
||||
name="device_location"
|
||||
value={FormData.device_location}
|
||||
value={formData.device_location}
|
||||
onChange={handleInputChange}
|
||||
placeholder="Enter Device Location"
|
||||
readOnly={props.readOnly}
|
||||
@@ -419,7 +254,7 @@ const DetailDevice = (props) => {
|
||||
<Text style={{ color: 'red' }}> *</Text>
|
||||
<Input
|
||||
name="ip_address"
|
||||
value={FormData.ip_address}
|
||||
value={formData.ip_address}
|
||||
onChange={handleInputChange}
|
||||
placeholder="e.g. 192.168.1.1"
|
||||
readOnly={props.readOnly}
|
||||
@@ -429,26 +264,13 @@ const DetailDevice = (props) => {
|
||||
<Text strong>Device Description</Text>
|
||||
<TextArea
|
||||
name="device_description"
|
||||
value={FormData.device_description}
|
||||
value={formData.device_description}
|
||||
onChange={handleInputChange}
|
||||
placeholder="Enter Device Description (Optional)"
|
||||
readOnly={props.readOnly}
|
||||
rows={4}
|
||||
/>
|
||||
</div>
|
||||
{props.permitDefault && (
|
||||
<div>
|
||||
<Text strong>Jenis Permit</Text>
|
||||
<Text style={{ color: 'red' }}> *</Text>
|
||||
|
||||
<CheckboxGroup
|
||||
options={jenisPermit}
|
||||
value={checkedList}
|
||||
onChange={onChange}
|
||||
disabled={props.readOnly}
|
||||
/>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
)}
|
||||
</Modal>
|
||||
|
||||
Reference in New Issue
Block a user