update: enhance device API functions with pagination and response structure; modify device form validation and UI components
This commit is contained in:
@@ -1,21 +1,25 @@
|
||||
import React, { useEffect, useState } from 'react';
|
||||
import { Modal, Input, Divider, Typography, Switch, Button, ConfigProvider, Radio } from 'antd';
|
||||
import { Modal, Input, Divider, Typography, Switch, Button, ConfigProvider, Radio, Select } from 'antd';
|
||||
import { NotifAlert, NotifOk } from '../../../../components/Global/ToastNotif';
|
||||
import { createApd, getJenisPermit, updateApd } from '../../../../api/master-apd';
|
||||
import { createDevice, updateDevice } from '../../../../api/master-device';
|
||||
import { Checkbox } from 'antd';
|
||||
const CheckboxGroup = Checkbox.Group;
|
||||
|
||||
const { Text } = Typography;
|
||||
const { TextArea } = Input;
|
||||
|
||||
const DetailDevice = (props) => {
|
||||
const [confirmLoading, setConfirmLoading] = useState(false);
|
||||
|
||||
const defaultData = {
|
||||
id_apd: '',
|
||||
nama_apd: '',
|
||||
type_input: 1,
|
||||
is_active: true,
|
||||
jenis_permit_default: [],
|
||||
device_id: '',
|
||||
device_code: '',
|
||||
device_name: '',
|
||||
device_status: true,
|
||||
device_location: 'Building A',
|
||||
device_description: '',
|
||||
ip_address: '',
|
||||
};
|
||||
|
||||
const [FormData, setFormData] = useState(defaultData);
|
||||
@@ -50,16 +54,62 @@ const DetailDevice = (props) => {
|
||||
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);
|
||||
|
||||
if (!FormData.nama_apd) {
|
||||
// Validasi required fields
|
||||
if (!FormData.device_code) {
|
||||
NotifOk({
|
||||
icon: 'warning',
|
||||
title: 'Peringatan',
|
||||
message: 'Kolom Nama APD Tidak Boleh Kosong',
|
||||
message: 'Kolom Device Code Tidak Boleh Kosong',
|
||||
});
|
||||
setConfirmLoading(false);
|
||||
return;
|
||||
}
|
||||
|
||||
if (!FormData.device_name) {
|
||||
NotifOk({
|
||||
icon: 'warning',
|
||||
title: 'Peringatan',
|
||||
message: 'Kolom Device Name Tidak Boleh Kosong',
|
||||
});
|
||||
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;
|
||||
}
|
||||
@@ -76,52 +126,49 @@ const DetailDevice = (props) => {
|
||||
}
|
||||
|
||||
const payload = {
|
||||
nama_apd: FormData.nama_apd,
|
||||
is_active: FormData.is_active,
|
||||
type_input: FormData.type_input,
|
||||
jenis_permit_default: checkedList,
|
||||
device_code: FormData.device_code,
|
||||
device_name: FormData.device_name,
|
||||
device_status: FormData.device_status,
|
||||
device_location: FormData.device_location,
|
||||
device_description: FormData.device_description,
|
||||
ip_address: FormData.ip_address,
|
||||
};
|
||||
|
||||
if (props.permitDefault) {
|
||||
try {
|
||||
let response;
|
||||
if (!FormData.id_apd) {
|
||||
response = await createApd(payload);
|
||||
} else {
|
||||
response = await updateApd(FormData.id_apd, payload);
|
||||
}
|
||||
try {
|
||||
let response;
|
||||
if (!FormData.device_id) {
|
||||
response = await createDevice(payload);
|
||||
} else {
|
||||
response = await updateDevice(FormData.device_id, payload);
|
||||
}
|
||||
|
||||
if (response.statusCode === 200) {
|
||||
NotifOk({
|
||||
icon: 'success',
|
||||
title: 'Berhasil',
|
||||
message: `Data "${response.data.nama_apd}" berhasil ${
|
||||
FormData.id_apd ? 'diubah' : 'ditambahkan'
|
||||
}.`,
|
||||
});
|
||||
console.log('Save Device Response:', response);
|
||||
|
||||
props.setActionMode('list');
|
||||
} else {
|
||||
NotifAlert({
|
||||
icon: 'error',
|
||||
title: 'Gagal',
|
||||
message: response.message || 'Terjadi kesalahan saat menyimpan data.',
|
||||
});
|
||||
}
|
||||
} catch (error) {
|
||||
// Check if response is successful
|
||||
if (response && (response.statusCode === 200 || response.statusCode === 201)) {
|
||||
NotifOk({
|
||||
icon: 'success',
|
||||
title: 'Berhasil',
|
||||
message: `Data Device "${response.data?.device_name || FormData.device_name}" berhasil ${
|
||||
FormData.device_id ? 'diubah' : 'ditambahkan'
|
||||
}.`,
|
||||
});
|
||||
|
||||
props.setActionMode('list');
|
||||
} else {
|
||||
NotifAlert({
|
||||
icon: 'error',
|
||||
title: 'Error',
|
||||
message: 'Terjadi kesalahan pada server. Coba lagi nanti.',
|
||||
title: 'Gagal',
|
||||
message: response?.message || 'Terjadi kesalahan saat menyimpan data.',
|
||||
});
|
||||
}
|
||||
} else {
|
||||
props.setData((prevData) => [
|
||||
...prevData,
|
||||
{ nama_apd: payload.nama_apd, type_input: payload.type_input },
|
||||
]);
|
||||
|
||||
props.setActionMode('list');
|
||||
} catch (error) {
|
||||
console.error('Save Device Error:', error);
|
||||
NotifAlert({
|
||||
icon: 'error',
|
||||
title: 'Error',
|
||||
message: error.message || 'Terjadi kesalahan pada server. Coba lagi nanti.',
|
||||
});
|
||||
}
|
||||
|
||||
setConfirmLoading(false);
|
||||
@@ -139,22 +186,35 @@ const DetailDevice = (props) => {
|
||||
const isChecked = event;
|
||||
setFormData({
|
||||
...FormData,
|
||||
is_active: isChecked ? true : false,
|
||||
device_status: isChecked ? true : false,
|
||||
});
|
||||
};
|
||||
|
||||
const handleSelectChange = (value) => {
|
||||
setFormData({
|
||||
...FormData,
|
||||
device_location: value,
|
||||
});
|
||||
};
|
||||
|
||||
useEffect(() => {
|
||||
const token = localStorage.getItem('token');
|
||||
if (token) {
|
||||
getDataJenisPermit();
|
||||
// Only call getDataJenisPermit if permitDefault is enabled
|
||||
if (props.permitDefault) {
|
||||
getDataJenisPermit();
|
||||
}
|
||||
|
||||
if (props.selectedData != null) {
|
||||
setFormData(props.selectedData);
|
||||
setCheckedList(props.selectedData.jenis_permit_default_arr);
|
||||
if (props.permitDefault && props.selectedData.jenis_permit_default_arr) {
|
||||
setCheckedList(props.selectedData.jenis_permit_default_arr);
|
||||
}
|
||||
} else {
|
||||
setFormData(defaultData);
|
||||
}
|
||||
} else {
|
||||
navigate('/signin');
|
||||
// navigate('/signin'); // Uncomment if useNavigate is imported
|
||||
}
|
||||
}, [props.showModal]);
|
||||
|
||||
@@ -217,74 +277,103 @@ const DetailDevice = (props) => {
|
||||
>
|
||||
{FormData && (
|
||||
<div>
|
||||
{props.permitDefault && (
|
||||
<>
|
||||
<div>
|
||||
<div>
|
||||
<Text strong>Aktif</Text>
|
||||
</div>
|
||||
<div
|
||||
<div>
|
||||
<div>
|
||||
<Text strong>Device Status</Text>
|
||||
</div>
|
||||
<div
|
||||
style={{
|
||||
display: 'flex',
|
||||
alignItems: 'center',
|
||||
marginTop: '8px',
|
||||
}}
|
||||
>
|
||||
<div style={{ marginRight: '8px' }}>
|
||||
<Switch
|
||||
disabled={props.readOnly}
|
||||
style={{
|
||||
display: 'flex',
|
||||
alignItems: 'center',
|
||||
marginTop: '8px',
|
||||
backgroundColor:
|
||||
FormData.device_status === true ? '#23A55A' : '#bfbfbf',
|
||||
}}
|
||||
>
|
||||
<div style={{ marginRight: '8px' }}>
|
||||
<Switch
|
||||
disabled={props.readOnly}
|
||||
style={{
|
||||
backgroundColor:
|
||||
FormData.is_active == 1 ? '#23A55A' : '#bfbfbf',
|
||||
}}
|
||||
checked={FormData.is_active == 1 ? true : false}
|
||||
onChange={handleStatusToggle}
|
||||
/>
|
||||
</div>
|
||||
<div>
|
||||
<Text>
|
||||
{FormData.is_active == 1 ? 'Aktif' : 'Non Aktif'}
|
||||
</Text>
|
||||
</div>
|
||||
</div>
|
||||
checked={FormData.device_status === true}
|
||||
onChange={handleStatusToggle}
|
||||
/>
|
||||
</div>
|
||||
<Divider style={{ margin: '5px 0' }} />
|
||||
</>
|
||||
)}
|
||||
<div>
|
||||
<Text>
|
||||
{FormData.device_status === true ? 'Running' : 'Offline'}
|
||||
</Text>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<Divider style={{ margin: '12px 0' }} />
|
||||
<div hidden>
|
||||
<Text strong>Device ID</Text>
|
||||
<Input
|
||||
name="id_apd"
|
||||
value={FormData.id_apd}
|
||||
name="device_id"
|
||||
value={FormData.device_id}
|
||||
onChange={handleInputChange}
|
||||
disabled
|
||||
/>
|
||||
</div>
|
||||
<div>
|
||||
<Text strong>Device Name</Text>
|
||||
<div style={{ marginBottom: 12 }}>
|
||||
<Text strong>Device Code</Text>
|
||||
<Text style={{ color: 'red' }}> *</Text>
|
||||
<Input
|
||||
name="nama_apd"
|
||||
value={FormData.nama_apd}
|
||||
name="device_code"
|
||||
value={FormData.device_code}
|
||||
onChange={handleInputChange}
|
||||
placeholder="Enter Device Code"
|
||||
readOnly={props.readOnly}
|
||||
/>
|
||||
</div>
|
||||
<div style={{ marginTop: 4, marginBottom: 4 }}>
|
||||
<Text strong>Tipe Input</Text>
|
||||
<Text style={{ color: 'red', marginLeft: 4 }}>*</Text>
|
||||
<div>
|
||||
<Radio.Group
|
||||
value={FormData.type_input}
|
||||
options={[
|
||||
{ value: 1, label: 'Check' },
|
||||
{ value: 2, label: 'Text' },
|
||||
{ value: 3, label: 'Number' },
|
||||
]}
|
||||
onChange={onChangeRadio}
|
||||
disabled={props.readOnly}
|
||||
/>
|
||||
</div>
|
||||
<div style={{ marginBottom: 12 }}>
|
||||
<Text strong>Device Name</Text>
|
||||
<Text style={{ color: 'red' }}> *</Text>
|
||||
<Input
|
||||
name="device_name"
|
||||
value={FormData.device_name}
|
||||
onChange={handleInputChange}
|
||||
placeholder="Enter Device Name"
|
||||
readOnly={props.readOnly}
|
||||
/>
|
||||
</div>
|
||||
<div style={{ marginBottom: 12 }}>
|
||||
<Text strong>Device Location</Text>
|
||||
<Text style={{ color: 'red' }}> *</Text>
|
||||
<Select
|
||||
value={FormData.device_location}
|
||||
onChange={handleSelectChange}
|
||||
disabled={props.readOnly}
|
||||
style={{ width: '100%' }}
|
||||
placeholder="Select Location"
|
||||
options={[
|
||||
{ value: 'Building A', label: 'Building A' },
|
||||
{ value: 'Building B', label: 'Building B' },
|
||||
]}
|
||||
/>
|
||||
</div>
|
||||
<div style={{ marginBottom: 12 }}>
|
||||
<Text strong>IP Address</Text>
|
||||
<Text style={{ color: 'red' }}> *</Text>
|
||||
<Input
|
||||
name="ip_address"
|
||||
value={FormData.ip_address}
|
||||
onChange={handleInputChange}
|
||||
placeholder="e.g. 192.168.1.1"
|
||||
readOnly={props.readOnly}
|
||||
/>
|
||||
</div>
|
||||
<div style={{ marginBottom: 12 }}>
|
||||
<Text strong>Device Description</Text>
|
||||
<TextArea
|
||||
name="device_description"
|
||||
value={FormData.device_description}
|
||||
onChange={handleInputChange}
|
||||
placeholder="Enter Device Description (Optional)"
|
||||
readOnly={props.readOnly}
|
||||
rows={4}
|
||||
/>
|
||||
</div>
|
||||
{props.permitDefault && (
|
||||
<div>
|
||||
|
||||
Reference in New Issue
Block a user