From af6c6de3014d274c03eca6633ec97b99e99ca666 Mon Sep 17 00:00:00 2001 From: vinix Date: Sun, 12 Oct 2025 23:18:47 +0700 Subject: [PATCH] fix field from be --- .../master/device/component/DetailDevice.jsx | 59 +++++++++++++------ .../master/device/component/ListDevice.jsx | 7 ++- 2 files changed, 46 insertions(+), 20 deletions(-) diff --git a/src/pages/master/device/component/DetailDevice.jsx b/src/pages/master/device/component/DetailDevice.jsx index fe299dc..c52c345 100644 --- a/src/pages/master/device/component/DetailDevice.jsx +++ b/src/pages/master/device/component/DetailDevice.jsx @@ -1,5 +1,15 @@ 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, + 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'; @@ -55,7 +65,8 @@ const DetailDevice = (props) => { }; 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]?)$/; + 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); }; @@ -63,15 +74,15 @@ const DetailDevice = (props) => { setConfirmLoading(true); // Validasi required fields - if (!FormData.device_code) { - NotifOk({ - icon: 'warning', - title: 'Peringatan', - message: 'Kolom Device Code Tidak Boleh Kosong', - }); - setConfirmLoading(false); - return; - } + // if (!FormData.device_code) { + // NotifOk({ + // icon: 'warning', + // title: 'Peringatan', + // message: 'Kolom Device Code Tidak Boleh Kosong', + // }); + // setConfirmLoading(false); + // return; + // } if (!FormData.device_name) { NotifOk({ @@ -125,15 +136,26 @@ const DetailDevice = (props) => { return; } + // Backend validation schema doesn't include device_code const payload = { - 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, }; + // 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) { @@ -146,10 +168,13 @@ const DetailDevice = (props) => { // 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; + NotifOk({ icon: 'success', title: 'Berhasil', - message: `Data Device "${response.data?.device_name || FormData.device_name}" berhasil ${ + message: `Data Device "${deviceName}" berhasil ${ FormData.device_id ? 'diubah' : 'ditambahkan' }.`, }); @@ -316,7 +341,7 @@ const DetailDevice = (props) => { disabled /> -
+ {/*
Device Code * { placeholder="Enter Device Code" readOnly={props.readOnly} /> -
+
*/}
Device Name * @@ -394,4 +419,4 @@ const DetailDevice = (props) => { ); }; -export default DetailDevice; \ No newline at end of file +export default DetailDevice; diff --git a/src/pages/master/device/component/ListDevice.jsx b/src/pages/master/device/component/ListDevice.jsx index 60bf582..9861c89 100644 --- a/src/pages/master/device/component/ListDevice.jsx +++ b/src/pages/master/device/component/ListDevice.jsx @@ -186,18 +186,19 @@ const ListDevice = memo(function ListDevice(props) { const handleDelete = async (device_id) => { const response = await deleteDevice(device_id); - if (response.statusCode == 200) { + // Backend returns: { statusCode: 200, message: "Device deleted successfully", rows: null, data: true } + if (response.statusCode == 200 && response.data === true) { NotifAlert({ icon: 'success', title: 'Berhasil', - message: 'Data Device "' + response.data.device_name + '" berhasil dihapus.', + message: response.message || 'Data Device berhasil dihapus.', }); doFilter(); } else { NotifOk({ icon: 'error', title: 'Gagal', - message: 'Gagal Menghapus Data Device', + message: response?.message || 'Gagal Menghapus Data Device', }); } };