refactor: update tag management with improved validation and device integration
This commit is contained in:
@@ -6,8 +6,6 @@ const getAllTag = async (queryParams) => {
|
|||||||
method: 'get',
|
method: 'get',
|
||||||
prefix: `tags?${queryParams.toString()}`,
|
prefix: `tags?${queryParams.toString()}`,
|
||||||
});
|
});
|
||||||
console.log('getAllTag response:', response);
|
|
||||||
console.log('Query params:', queryParams.toString());
|
|
||||||
|
|
||||||
// Check if response has error
|
// Check if response has error
|
||||||
if (response.error) {
|
if (response.error) {
|
||||||
@@ -106,8 +104,6 @@ const createTag = async (queryParams) => {
|
|||||||
prefix: `tags`,
|
prefix: `tags`,
|
||||||
params: queryParams,
|
params: queryParams,
|
||||||
});
|
});
|
||||||
console.log('createTag full response:', response);
|
|
||||||
console.log('createTag payload sent:', queryParams);
|
|
||||||
|
|
||||||
// Check if response has error flag
|
// Check if response has error flag
|
||||||
if (response.error) {
|
if (response.error) {
|
||||||
@@ -134,8 +130,6 @@ const updateTag = async (tag_id, queryParams) => {
|
|||||||
prefix: `tags/${tag_id}`,
|
prefix: `tags/${tag_id}`,
|
||||||
params: queryParams,
|
params: queryParams,
|
||||||
});
|
});
|
||||||
console.log('updateTag full response:', response);
|
|
||||||
console.log('updateTag payload sent:', queryParams);
|
|
||||||
|
|
||||||
// Check if response has error flag
|
// Check if response has error flag
|
||||||
if (response.error) {
|
if (response.error) {
|
||||||
@@ -161,7 +155,6 @@ const deleteTag = async (queryParams) => {
|
|||||||
method: 'delete',
|
method: 'delete',
|
||||||
prefix: `tags/${queryParams}`,
|
prefix: `tags/${queryParams}`,
|
||||||
});
|
});
|
||||||
console.log('deleteTag full response:', response);
|
|
||||||
|
|
||||||
// Check if response has error flag
|
// Check if response has error flag
|
||||||
if (response.error) {
|
if (response.error) {
|
||||||
|
|||||||
@@ -1,20 +1,27 @@
|
|||||||
import { useEffect, useState } from 'react';
|
import { useEffect, useState } from 'react';
|
||||||
import { Modal, Input, Divider, Typography, Button, ConfigProvider, Select } from 'antd';
|
import { Modal, Input, Typography, Button, ConfigProvider, Switch, Select } from 'antd';
|
||||||
import { NotifAlert, NotifOk } from '../../../../components/Global/ToastNotif';
|
import { NotifAlert, NotifOk } from '../../../../components/Global/ToastNotif';
|
||||||
|
import { createTag, updateTag } from '../../../../api/master-tag';
|
||||||
|
import { getAllDevice } from '../../../../api/master-device';
|
||||||
|
|
||||||
const { Text } = Typography;
|
const { Text } = Typography;
|
||||||
const { TextArea } = Input;
|
|
||||||
|
|
||||||
const DetailTag = (props) => {
|
const DetailTag = (props) => {
|
||||||
const [confirmLoading, setConfirmLoading] = useState(false);
|
const [confirmLoading, setConfirmLoading] = useState(false);
|
||||||
|
const [deviceList, setDeviceList] = useState([]);
|
||||||
|
const [loadingDevices, setLoadingDevices] = useState(false);
|
||||||
|
|
||||||
const defaultData = {
|
const defaultData = {
|
||||||
tag_id: '',
|
tag_id: '',
|
||||||
tag_code: '',
|
tag_code: '',
|
||||||
tag_name: '',
|
tag_name: '',
|
||||||
tag_value: 'Off',
|
tag_number: '',
|
||||||
tag_type: 'alarm',
|
data_type: '',
|
||||||
tag_description: '',
|
unit: '',
|
||||||
|
device_name: '', // Read-only, auto-display dari device yang dipilih
|
||||||
|
is_active: true,
|
||||||
|
device_id: null, // akan set ketika user select device dari dropdown
|
||||||
|
sub_section_id: null,
|
||||||
};
|
};
|
||||||
|
|
||||||
const [FormData, setFormData] = useState(defaultData);
|
const [FormData, setFormData] = useState(defaultData);
|
||||||
@@ -27,18 +34,8 @@ const DetailTag = (props) => {
|
|||||||
const handleSave = async () => {
|
const handleSave = async () => {
|
||||||
setConfirmLoading(true);
|
setConfirmLoading(true);
|
||||||
|
|
||||||
// Validasi required fields
|
// Validasi required fields untuk CREATE
|
||||||
if (!FormData.tag_code) {
|
if (!FormData.tag_name || FormData.tag_name.trim() === '') {
|
||||||
NotifOk({
|
|
||||||
icon: 'warning',
|
|
||||||
title: 'Peringatan',
|
|
||||||
message: 'Kolom Tag Code Tidak Boleh Kosong',
|
|
||||||
});
|
|
||||||
setConfirmLoading(false);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!FormData.tag_name) {
|
|
||||||
NotifOk({
|
NotifOk({
|
||||||
icon: 'warning',
|
icon: 'warning',
|
||||||
title: 'Peringatan',
|
title: 'Peringatan',
|
||||||
@@ -48,55 +45,114 @@ const DetailTag = (props) => {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!FormData.tag_value) {
|
if (!FormData.tag_number || FormData.tag_number === '') {
|
||||||
NotifOk({
|
NotifOk({
|
||||||
icon: 'warning',
|
icon: 'warning',
|
||||||
title: 'Peringatan',
|
title: 'Peringatan',
|
||||||
message: 'Kolom Tag Value Tidak Boleh Kosong',
|
message: 'Kolom Tag Number Tidak Boleh Kosong',
|
||||||
});
|
});
|
||||||
setConfirmLoading(false);
|
setConfirmLoading(false);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!FormData.tag_type) {
|
// Validasi format number untuk tag_number
|
||||||
|
const tagNumberInt = parseInt(FormData.tag_number);
|
||||||
|
if (isNaN(tagNumberInt)) {
|
||||||
NotifOk({
|
NotifOk({
|
||||||
icon: 'warning',
|
icon: 'warning',
|
||||||
title: 'Peringatan',
|
title: 'Peringatan',
|
||||||
message: 'Kolom Tag Type Tidak Boleh Kosong',
|
message: 'Tag Number harus berupa angka yang valid',
|
||||||
});
|
});
|
||||||
setConfirmLoading(false);
|
setConfirmLoading(false);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const payload = {
|
if (!FormData.data_type || FormData.data_type.trim() === '') {
|
||||||
tag_code: FormData.tag_code,
|
NotifOk({
|
||||||
tag_name: FormData.tag_name,
|
icon: 'warning',
|
||||||
tag_value: FormData.tag_value,
|
title: 'Peringatan',
|
||||||
tag_type: FormData.tag_type,
|
message: 'Kolom Data Type Tidak Boleh Kosong',
|
||||||
tag_description: FormData.tag_description,
|
});
|
||||||
};
|
setConfirmLoading(false);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!FormData.unit || FormData.unit.trim() === '') {
|
||||||
|
NotifOk({
|
||||||
|
icon: 'warning',
|
||||||
|
title: 'Peringatan',
|
||||||
|
message: 'Kolom Unit Tidak Boleh Kosong',
|
||||||
|
});
|
||||||
|
setConfirmLoading(false);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Device validation
|
||||||
|
if (!FormData.device_id) {
|
||||||
|
NotifOk({
|
||||||
|
icon: 'warning',
|
||||||
|
title: 'Peringatan',
|
||||||
|
message: 'Device harus dipilih',
|
||||||
|
});
|
||||||
|
setConfirmLoading(false);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Prepare payload berdasarkan backend validation schema
|
||||||
|
let payload;
|
||||||
|
|
||||||
|
if (FormData.tag_id) {
|
||||||
|
payload = {};
|
||||||
|
|
||||||
|
if (FormData.tag_name && FormData.tag_name.trim() !== '') {
|
||||||
|
payload.tag_name = FormData.tag_name;
|
||||||
|
}
|
||||||
|
if (FormData.tag_number && FormData.tag_number !== '') {
|
||||||
|
payload.tag_number = parseInt(FormData.tag_number);
|
||||||
|
}
|
||||||
|
if (FormData.data_type && FormData.data_type.trim() !== '') {
|
||||||
|
payload.data_type = FormData.data_type;
|
||||||
|
}
|
||||||
|
if (FormData.unit && FormData.unit.trim() !== '') {
|
||||||
|
payload.unit = FormData.unit;
|
||||||
|
}
|
||||||
|
if (FormData.device_id) {
|
||||||
|
payload.device_id = parseInt(FormData.device_id);
|
||||||
|
}
|
||||||
|
payload.is_active = FormData.is_active;
|
||||||
|
} else {
|
||||||
|
// CREATE: device_id hardcoded
|
||||||
|
payload = {
|
||||||
|
tag_name: FormData.tag_name,
|
||||||
|
tag_number: parseInt(FormData.tag_number),
|
||||||
|
data_type: FormData.data_type,
|
||||||
|
unit: FormData.unit,
|
||||||
|
is_active: FormData.is_active,
|
||||||
|
device_id: parseInt(FormData.device_id), // Hardcoded dari defaultData (9)
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// Simulate API call
|
let response;
|
||||||
await new Promise((resolve) => setTimeout(resolve, 500));
|
|
||||||
|
|
||||||
const response = {
|
if (FormData.tag_id) {
|
||||||
statusCode: FormData.tag_id ? 200 : 201,
|
// Update existing tag
|
||||||
data: {
|
response = await updateTag(FormData.tag_id, payload);
|
||||||
tag_name: FormData.tag_name,
|
} else {
|
||||||
},
|
// Create new tag
|
||||||
};
|
response = await createTag(payload);
|
||||||
|
}
|
||||||
console.log('Save Tag Response:', response);
|
|
||||||
|
|
||||||
// Check if response is successful
|
// Check if response is successful
|
||||||
if (response && (response.statusCode === 200 || response.statusCode === 201)) {
|
if (response && (response.statusCode === 200 || response.statusCode === 201)) {
|
||||||
NotifOk({
|
NotifOk({
|
||||||
icon: 'success',
|
icon: 'success',
|
||||||
title: 'Berhasil',
|
title: 'Berhasil',
|
||||||
message: `Data Tag "${response.data?.tag_name || FormData.tag_name}" berhasil ${
|
message:
|
||||||
FormData.tag_id ? 'diubah' : 'ditambahkan'
|
response.message ||
|
||||||
}.`,
|
`Data Tag "${response.data?.tag_name || FormData.tag_name}" berhasil ${
|
||||||
|
FormData.tag_id ? 'diubah' : 'ditambahkan'
|
||||||
|
}.`,
|
||||||
});
|
});
|
||||||
|
|
||||||
props.setActionMode('list');
|
props.setActionMode('list');
|
||||||
@@ -134,11 +190,60 @@ const DetailTag = (props) => {
|
|||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const handleDeviceChange = (deviceId) => {
|
||||||
|
const selectedDevice = deviceList.find((device) => device.device_id === deviceId);
|
||||||
|
setFormData({
|
||||||
|
...FormData,
|
||||||
|
device_id: deviceId,
|
||||||
|
device_name: selectedDevice ? selectedDevice.device_name : '',
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
const handleStatusToggle = (isChecked) => {
|
||||||
|
setFormData({
|
||||||
|
...FormData,
|
||||||
|
is_active: isChecked,
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
const loadDevices = async () => {
|
||||||
|
setLoadingDevices(true);
|
||||||
|
try {
|
||||||
|
const params = new URLSearchParams({ limit: 1000 });
|
||||||
|
const response = await getAllDevice(params);
|
||||||
|
if (response && response.data && response.data.data) {
|
||||||
|
setDeviceList(response.data.data);
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
console.error('Error loading devices:', error);
|
||||||
|
} finally {
|
||||||
|
setLoadingDevices(false);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const token = localStorage.getItem('token');
|
const token = localStorage.getItem('token');
|
||||||
if (token) {
|
if (token) {
|
||||||
|
if (props.showModal) {
|
||||||
|
// Load devices when modal opens
|
||||||
|
loadDevices();
|
||||||
|
}
|
||||||
|
|
||||||
if (props.selectedData != null) {
|
if (props.selectedData != null) {
|
||||||
setFormData(props.selectedData);
|
// Only set fields that are in defaultData to avoid sending extra fields
|
||||||
|
const filteredData = {
|
||||||
|
tag_id: props.selectedData.tag_id || '',
|
||||||
|
tag_code: props.selectedData.tag_code || '',
|
||||||
|
tag_name: props.selectedData.tag_name || '',
|
||||||
|
tag_number: props.selectedData.tag_number || '',
|
||||||
|
data_type: props.selectedData.data_type || '',
|
||||||
|
unit: props.selectedData.unit || '',
|
||||||
|
device_name: props.selectedData.device_name || '',
|
||||||
|
is_active: props.selectedData.is_active ?? true,
|
||||||
|
device_id: props.selectedData.device_id || null,
|
||||||
|
sub_section_id: props.selectedData.sub_section_id || null,
|
||||||
|
};
|
||||||
|
setFormData(filteredData);
|
||||||
} else {
|
} else {
|
||||||
setFormData(defaultData);
|
setFormData(defaultData);
|
||||||
}
|
}
|
||||||
@@ -169,8 +274,6 @@ const DetailTag = (props) => {
|
|||||||
defaultColor: '#23A55A',
|
defaultColor: '#23A55A',
|
||||||
defaultBorderColor: '#23A55A',
|
defaultBorderColor: '#23A55A',
|
||||||
defaultHoverColor: '#23A55A',
|
defaultHoverColor: '#23A55A',
|
||||||
defaultHoverBorderColor: '#23A55A',
|
|
||||||
defaultHoverColor: '#23A55A',
|
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
}}
|
}}
|
||||||
@@ -213,17 +316,19 @@ const DetailTag = (props) => {
|
|||||||
disabled
|
disabled
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
<div style={{ marginBottom: 12 }}>
|
{/* Tag Code hanya ditampilkan saat EDIT atau PREVIEW */}
|
||||||
<Text strong>Tag Code</Text>
|
{(props.actionMode === 'edit' || props.actionMode === 'preview') && (
|
||||||
<Text style={{ color: 'red' }}> *</Text>
|
<div style={{ marginBottom: 12 }}>
|
||||||
<Input
|
<Text strong>Tag Code</Text>
|
||||||
name="tag_code"
|
<Input
|
||||||
value={FormData.tag_code}
|
name="tag_code"
|
||||||
onChange={handleInputChange}
|
value={FormData.tag_code}
|
||||||
placeholder="Enter Tag Code"
|
onChange={handleInputChange}
|
||||||
readOnly={props.readOnly}
|
placeholder="Auto Generate"
|
||||||
/>
|
disabled
|
||||||
</div>
|
/>
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
<div style={{ marginBottom: 12 }}>
|
<div style={{ marginBottom: 12 }}>
|
||||||
<Text strong>Tag Name</Text>
|
<Text strong>Tag Name</Text>
|
||||||
<Text style={{ color: 'red' }}> *</Text>
|
<Text style={{ color: 'red' }}> *</Text>
|
||||||
@@ -236,47 +341,97 @@ const DetailTag = (props) => {
|
|||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
<div style={{ marginBottom: 12 }}>
|
<div style={{ marginBottom: 12 }}>
|
||||||
<Text strong>Tag Value</Text>
|
<Text strong>Tag Number</Text>
|
||||||
<Text style={{ color: 'red' }}> *</Text>
|
<Text style={{ color: 'red' }}> *</Text>
|
||||||
<Select
|
<Input
|
||||||
value={FormData.tag_value}
|
name="tag_number"
|
||||||
onChange={(value) => handleSelectChange('tag_value', value)}
|
value={FormData.tag_number}
|
||||||
disabled={props.readOnly}
|
|
||||||
style={{ width: '100%' }}
|
|
||||||
placeholder="Select Value"
|
|
||||||
options={[
|
|
||||||
{ value: 'On', label: 'On' },
|
|
||||||
{ value: 'Off', label: 'Off' },
|
|
||||||
{ value: 'Critical', label: 'Critical' },
|
|
||||||
]}
|
|
||||||
/>
|
|
||||||
</div>
|
|
||||||
<div style={{ marginBottom: 12 }}>
|
|
||||||
<Text strong>Tag Type</Text>
|
|
||||||
<Text style={{ color: 'red' }}> *</Text>
|
|
||||||
<Select
|
|
||||||
value={FormData.tag_type}
|
|
||||||
onChange={(value) => handleSelectChange('tag_type', value)}
|
|
||||||
disabled={props.readOnly}
|
|
||||||
style={{ width: '100%' }}
|
|
||||||
placeholder="Select Type"
|
|
||||||
options={[
|
|
||||||
{ value: 'alarm', label: 'Alarm' },
|
|
||||||
{ value: 'measurement', label: 'Measurement' },
|
|
||||||
]}
|
|
||||||
/>
|
|
||||||
</div>
|
|
||||||
<div style={{ marginBottom: 12 }}>
|
|
||||||
<Text strong>Tag Description</Text>
|
|
||||||
<TextArea
|
|
||||||
name="tag_description"
|
|
||||||
value={FormData.tag_description}
|
|
||||||
onChange={handleInputChange}
|
onChange={handleInputChange}
|
||||||
placeholder="Enter Tag Description (Optional)"
|
placeholder="Enter Tag Number"
|
||||||
readOnly={props.readOnly}
|
readOnly={props.readOnly}
|
||||||
rows={4}
|
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
|
<div style={{ marginBottom: 12 }}>
|
||||||
|
<Text strong>Data Type</Text>
|
||||||
|
<Text style={{ color: 'red' }}> *</Text>
|
||||||
|
<Input
|
||||||
|
name="data_type"
|
||||||
|
value={FormData.data_type}
|
||||||
|
onChange={handleInputChange}
|
||||||
|
placeholder="Enter Data Type"
|
||||||
|
readOnly={props.readOnly}
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
<div style={{ marginBottom: 12 }}>
|
||||||
|
<Text strong>Unit</Text>
|
||||||
|
<Text style={{ color: 'red' }}> *</Text>
|
||||||
|
<Input
|
||||||
|
name="unit"
|
||||||
|
value={FormData.unit}
|
||||||
|
onChange={handleInputChange}
|
||||||
|
placeholder="Enter Unit"
|
||||||
|
readOnly={props.readOnly}
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
<div style={{ marginBottom: 12 }}>
|
||||||
|
<Text strong>Device Code</Text>
|
||||||
|
<Text style={{ color: 'red' }}> *</Text>
|
||||||
|
<Select
|
||||||
|
style={{ width: '100%' }}
|
||||||
|
placeholder="Select Device Code"
|
||||||
|
value={FormData.device_id || undefined}
|
||||||
|
onChange={handleDeviceChange}
|
||||||
|
disabled={props.readOnly}
|
||||||
|
loading={loadingDevices}
|
||||||
|
showSearch
|
||||||
|
filterOption={(input, option) =>
|
||||||
|
(option?.label ?? '').toLowerCase().includes(input.toLowerCase())
|
||||||
|
}
|
||||||
|
options={deviceList.map((device) => ({
|
||||||
|
value: device.device_id,
|
||||||
|
label: device.device_code || '',
|
||||||
|
}))}
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
<div style={{ marginBottom: 12 }}>
|
||||||
|
<Text strong>Device Name</Text>
|
||||||
|
<Input
|
||||||
|
name="device_name"
|
||||||
|
value={FormData.device_name}
|
||||||
|
placeholder="Auto-filled from selected device code"
|
||||||
|
disabled
|
||||||
|
style={{ backgroundColor: '#f5f5f5' }}
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
{/* Device ID hidden - value dari dropdown */}
|
||||||
|
<input type="hidden" name="device_id" value={FormData.device_id || ''} />
|
||||||
|
<div style={{ marginBottom: 12 }}>
|
||||||
|
<div>
|
||||||
|
<Text strong>Status</Text>
|
||||||
|
</div>
|
||||||
|
<div
|
||||||
|
style={{
|
||||||
|
display: 'flex',
|
||||||
|
alignItems: 'center',
|
||||||
|
marginTop: '8px',
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
<div style={{ marginRight: '8px' }}>
|
||||||
|
<Switch
|
||||||
|
disabled={props.readOnly}
|
||||||
|
style={{
|
||||||
|
backgroundColor:
|
||||||
|
FormData.is_active === true ? '#23A55A' : '#bfbfbf',
|
||||||
|
}}
|
||||||
|
checked={FormData.is_active === true}
|
||||||
|
onChange={handleStatusToggle}
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
<div>
|
||||||
|
<Text>{FormData.is_active === true ? 'Active' : 'Inactive'}</Text>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
</div>
|
</div>
|
||||||
)}
|
)}
|
||||||
</Modal>
|
</Modal>
|
||||||
|
|||||||
@@ -10,170 +10,7 @@ import {
|
|||||||
import { NotifAlert, NotifConfirmDialog } from '../../../../components/Global/ToastNotif';
|
import { NotifAlert, NotifConfirmDialog } from '../../../../components/Global/ToastNotif';
|
||||||
import { useNavigate } from 'react-router-dom';
|
import { useNavigate } from 'react-router-dom';
|
||||||
import TableList from '../../../../components/Global/TableList';
|
import TableList from '../../../../components/Global/TableList';
|
||||||
|
import { getAllTag, deleteTag } from '../../../../api/master-tag';
|
||||||
// Dummy data
|
|
||||||
const initialTagsData = [
|
|
||||||
{
|
|
||||||
tag_id: 1,
|
|
||||||
tag_code: 'J06-OPEN',
|
|
||||||
tag_name: 'tag 1',
|
|
||||||
tag_value: 'On',
|
|
||||||
tag_type: 'alarm',
|
|
||||||
tag_description: 'test1',
|
|
||||||
},
|
|
||||||
{
|
|
||||||
tag_id: 2,
|
|
||||||
tag_code: 'J06-CLS',
|
|
||||||
tag_name: 'tag 2',
|
|
||||||
tag_value: 'Off',
|
|
||||||
tag_type: 'alarm',
|
|
||||||
tag_description: 'tes2',
|
|
||||||
},
|
|
||||||
{
|
|
||||||
tag_id: 3,
|
|
||||||
tag_code: 'J06-VRS',
|
|
||||||
tag_name: 'tag 3',
|
|
||||||
tag_value: 'Critical',
|
|
||||||
tag_type: 'measurement',
|
|
||||||
tag_description: 'test3',
|
|
||||||
},
|
|
||||||
{
|
|
||||||
tag_id: 4,
|
|
||||||
tag_code: 'J07-IR',
|
|
||||||
tag_name: 'tag 4 R',
|
|
||||||
tag_value: 'On',
|
|
||||||
tag_type: 'measurement',
|
|
||||||
tag_description: 'test 4',
|
|
||||||
},
|
|
||||||
{
|
|
||||||
tag_id: 5,
|
|
||||||
tag_code: 'K01-CLS',
|
|
||||||
tag_name: 'tag 5',
|
|
||||||
tag_value: 'Off',
|
|
||||||
tag_type: 'alarm',
|
|
||||||
tag_description: 'tes5',
|
|
||||||
},
|
|
||||||
{
|
|
||||||
tag_id: 6,
|
|
||||||
tag_code: 'J07-IS',
|
|
||||||
tag_name: 'tag6',
|
|
||||||
tag_value: 'On',
|
|
||||||
tag_type: 'measurement',
|
|
||||||
tag_description: 'tes6',
|
|
||||||
},
|
|
||||||
{
|
|
||||||
tag_id: 7,
|
|
||||||
tag_code: 'J07-IT',
|
|
||||||
tag_name: 'tag7',
|
|
||||||
tag_value: 'Critical',
|
|
||||||
tag_type: 'measurement',
|
|
||||||
tag_description: 'tes7',
|
|
||||||
},
|
|
||||||
{
|
|
||||||
tag_id: 8,
|
|
||||||
tag_code: 'J06-VST',
|
|
||||||
tag_name: 'tag8',
|
|
||||||
tag_value: 'On',
|
|
||||||
tag_type: 'measurement',
|
|
||||||
tag_description: 'tes8',
|
|
||||||
},
|
|
||||||
{
|
|
||||||
tag_id: 9,
|
|
||||||
tag_code: 'J06-VTR',
|
|
||||||
tag_name: 'tag9',
|
|
||||||
tag_value: 'On',
|
|
||||||
tag_type: 'measurement',
|
|
||||||
tag_description: 'tes9',
|
|
||||||
},
|
|
||||||
{
|
|
||||||
tag_id: 10,
|
|
||||||
tag_code: 'K02-OPEN',
|
|
||||||
tag_name: 'tag10',
|
|
||||||
tag_value: 'On',
|
|
||||||
tag_type: 'alarm',
|
|
||||||
tag_description: 'tes10',
|
|
||||||
},
|
|
||||||
{
|
|
||||||
tag_id: 11,
|
|
||||||
tag_code: 'K02-CLS',
|
|
||||||
tag_name: 'Kontaktor K02 Close',
|
|
||||||
tag_value: 'Off',
|
|
||||||
tag_type: 'alarm',
|
|
||||||
tag_description: 'Kontaktor K02 dalam kondisi tertutup',
|
|
||||||
},
|
|
||||||
{
|
|
||||||
tag_id: 12,
|
|
||||||
tag_code: 'J08-FREQ',
|
|
||||||
tag_name: 'Frequency',
|
|
||||||
tag_value: 'On',
|
|
||||||
tag_type: 'measurement',
|
|
||||||
tag_description: 'Frekuensi sistem normal (50Hz)',
|
|
||||||
},
|
|
||||||
{
|
|
||||||
tag_id: 13,
|
|
||||||
tag_code: 'J08-PF',
|
|
||||||
tag_name: 'Power Factor',
|
|
||||||
tag_value: 'Critical',
|
|
||||||
tag_type: 'measurement',
|
|
||||||
tag_description: 'Power factor rendah, perlu perbaikan',
|
|
||||||
},
|
|
||||||
{
|
|
||||||
tag_id: 14,
|
|
||||||
tag_code: 'TR01-TEMP',
|
|
||||||
tag_name: 'Transformer Temperature',
|
|
||||||
tag_value: 'Critical',
|
|
||||||
tag_type: 'alarm',
|
|
||||||
tag_description: 'Suhu trafo melebihi batas aman',
|
|
||||||
},
|
|
||||||
{
|
|
||||||
tag_id: 15,
|
|
||||||
tag_code: 'TR01-OIL',
|
|
||||||
tag_name: 'Transformer Oil Level',
|
|
||||||
tag_value: 'On',
|
|
||||||
tag_type: 'alarm',
|
|
||||||
tag_description: 'Level oli trafo normal',
|
|
||||||
},
|
|
||||||
{
|
|
||||||
tag_id: 16,
|
|
||||||
tag_code: 'GEN01-RUN',
|
|
||||||
tag_name: 'Generator Running',
|
|
||||||
tag_value: 'Off',
|
|
||||||
tag_type: 'alarm',
|
|
||||||
tag_description: 'Generator dalam kondisi mati',
|
|
||||||
},
|
|
||||||
{
|
|
||||||
tag_id: 17,
|
|
||||||
tag_code: 'GEN01-FUEL',
|
|
||||||
tag_name: 'Generator Fuel Level',
|
|
||||||
tag_value: 'On',
|
|
||||||
tag_type: 'measurement',
|
|
||||||
tag_description: 'Level bahan bakar generator mencukupi',
|
|
||||||
},
|
|
||||||
{
|
|
||||||
tag_id: 18,
|
|
||||||
tag_code: 'UPS01-BAT',
|
|
||||||
tag_name: 'UPS Battery Status',
|
|
||||||
tag_value: 'On',
|
|
||||||
tag_type: 'alarm',
|
|
||||||
tag_description: 'Status battery UPS normal',
|
|
||||||
},
|
|
||||||
{
|
|
||||||
tag_id: 19,
|
|
||||||
tag_code: 'UPS01-LOAD',
|
|
||||||
tag_name: 'UPS Load',
|
|
||||||
tag_value: 'Critical',
|
|
||||||
tag_type: 'measurement',
|
|
||||||
tag_description: 'Beban UPS mendekati kapasitas maksimal',
|
|
||||||
},
|
|
||||||
{
|
|
||||||
tag_id: 20,
|
|
||||||
tag_code: 'PANEL-DOOR',
|
|
||||||
tag_name: 'Panel Door Status',
|
|
||||||
tag_value: 'Off',
|
|
||||||
tag_type: 'alarm',
|
|
||||||
tag_description: 'Pintu panel tertutup',
|
|
||||||
},
|
|
||||||
];
|
|
||||||
|
|
||||||
const columns = (showPreviewModal, showEditModal, showDeleteDialog) => [
|
const columns = (showPreviewModal, showEditModal, showDeleteDialog) => [
|
||||||
{
|
{
|
||||||
@@ -181,7 +18,7 @@ const columns = (showPreviewModal, showEditModal, showDeleteDialog) => [
|
|||||||
dataIndex: 'tag_id',
|
dataIndex: 'tag_id',
|
||||||
key: 'tag_id',
|
key: 'tag_id',
|
||||||
width: '5%',
|
width: '5%',
|
||||||
hidden: 'true',
|
hidden: true,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
title: 'Tag Code',
|
title: 'Tag Code',
|
||||||
@@ -193,38 +30,49 @@ const columns = (showPreviewModal, showEditModal, showDeleteDialog) => [
|
|||||||
title: 'Tag Name',
|
title: 'Tag Name',
|
||||||
dataIndex: 'tag_name',
|
dataIndex: 'tag_name',
|
||||||
key: 'tag_name',
|
key: 'tag_name',
|
||||||
width: '20%',
|
width: '15%',
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
title: 'Value',
|
title: 'Tag Number',
|
||||||
dataIndex: 'tag_value',
|
dataIndex: 'tag_number',
|
||||||
key: 'tag_value',
|
key: 'tag_number',
|
||||||
width: '10%',
|
width: '10%',
|
||||||
align: 'center',
|
align: 'center',
|
||||||
render: (_, { tag_value }) => {
|
},
|
||||||
let color = 'default';
|
{
|
||||||
if (tag_value.toLowerCase() === 'on') color = 'green';
|
title: 'Data Type',
|
||||||
else if (tag_value.toLowerCase() === 'off') color = 'gray';
|
dataIndex: 'data_type',
|
||||||
else if (tag_value.toLowerCase() === 'critical') color = 'red';
|
key: 'data_type',
|
||||||
|
width: '10%',
|
||||||
|
},
|
||||||
|
{
|
||||||
|
title: 'Unit',
|
||||||
|
dataIndex: 'unit',
|
||||||
|
key: 'unit',
|
||||||
|
width: '8%',
|
||||||
|
},
|
||||||
|
{
|
||||||
|
title: 'Device',
|
||||||
|
dataIndex: 'device_name',
|
||||||
|
key: 'device_name',
|
||||||
|
width: '12%',
|
||||||
|
},
|
||||||
|
{
|
||||||
|
title: 'Status',
|
||||||
|
dataIndex: 'is_active',
|
||||||
|
key: 'is_active',
|
||||||
|
width: '8%',
|
||||||
|
align: 'center',
|
||||||
|
render: (_, { is_active }) => {
|
||||||
|
const color = is_active ? 'green' : 'red';
|
||||||
|
const text = is_active ? 'Active' : 'Inactive';
|
||||||
return (
|
return (
|
||||||
<Tag color={color} key={'value'}>
|
<Tag color={color} key={'status'}>
|
||||||
{tag_value}
|
{text}
|
||||||
</Tag>
|
</Tag>
|
||||||
);
|
);
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
|
||||||
title: 'Type',
|
|
||||||
dataIndex: 'tag_type',
|
|
||||||
key: 'tag_type',
|
|
||||||
width: '10%',
|
|
||||||
},
|
|
||||||
{
|
|
||||||
title: 'Description',
|
|
||||||
dataIndex: 'tag_description',
|
|
||||||
key: 'tag_description',
|
|
||||||
width: '25%',
|
|
||||||
},
|
|
||||||
{
|
{
|
||||||
title: 'Aksi',
|
title: 'Aksi',
|
||||||
key: 'aksi',
|
key: 'aksi',
|
||||||
@@ -265,9 +113,7 @@ const columns = (showPreviewModal, showEditModal, showDeleteDialog) => [
|
|||||||
];
|
];
|
||||||
|
|
||||||
const ListTag = memo(function ListTag(props) {
|
const ListTag = memo(function ListTag(props) {
|
||||||
const [showFilter, setShowFilter] = useState(false);
|
|
||||||
const [trigerFilter, setTrigerFilter] = useState(false);
|
const [trigerFilter, setTrigerFilter] = useState(false);
|
||||||
const [tagsData, setTagsData] = useState(initialTagsData);
|
|
||||||
|
|
||||||
const defaultFilter = { search: '' };
|
const defaultFilter = { search: '' };
|
||||||
const [formDataFilter, setFormDataFilter] = useState(defaultFilter);
|
const [formDataFilter, setFormDataFilter] = useState(defaultFilter);
|
||||||
@@ -275,94 +121,30 @@ const ListTag = memo(function ListTag(props) {
|
|||||||
|
|
||||||
const navigate = useNavigate();
|
const navigate = useNavigate();
|
||||||
|
|
||||||
// Dummy data function to simulate API call - now uses state
|
|
||||||
const getAllTag = async (params) => {
|
|
||||||
// Simulate API delay
|
|
||||||
await new Promise((resolve) => setTimeout(resolve, 300));
|
|
||||||
|
|
||||||
// Extract URLSearchParams - TableList sends URLSearchParams object
|
|
||||||
const searchParam = params.get('search') || '';
|
|
||||||
const page = parseInt(params.get('page')) || 1;
|
|
||||||
const limit = parseInt(params.get('limit')) || 10;
|
|
||||||
|
|
||||||
console.log('getAllTag called with:', { searchParam, page, limit });
|
|
||||||
|
|
||||||
// Filter by search
|
|
||||||
let filteredTags = tagsData;
|
|
||||||
if (searchParam) {
|
|
||||||
const searchLower = searchParam.toLowerCase();
|
|
||||||
filteredTags = tagsData.filter(
|
|
||||||
(tag) =>
|
|
||||||
tag.tag_code.toLowerCase().includes(searchLower) ||
|
|
||||||
tag.tag_name.toLowerCase().includes(searchLower) ||
|
|
||||||
tag.tag_type.toLowerCase().includes(searchLower) ||
|
|
||||||
tag.tag_description.toLowerCase().includes(searchLower)
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Pagination logic
|
|
||||||
const totalData = filteredTags.length;
|
|
||||||
const totalPages = Math.ceil(totalData / limit);
|
|
||||||
const startIndex = (page - 1) * limit;
|
|
||||||
const endIndex = startIndex + limit;
|
|
||||||
const paginatedData = filteredTags.slice(startIndex, endIndex);
|
|
||||||
|
|
||||||
// Return structure that matches TableList expectation
|
|
||||||
// Based on TableList code:
|
|
||||||
// - resData.data.data = array of items
|
|
||||||
// - resData.data.total = total count
|
|
||||||
// - resData.data.paging.page = current page
|
|
||||||
// - resData.data.paging.limit = items per page
|
|
||||||
// - resData.data.paging.total = total items
|
|
||||||
// - resData.data.paging.page_total = total pages
|
|
||||||
return {
|
|
||||||
status: 200,
|
|
||||||
statusCode: 200,
|
|
||||||
data: {
|
|
||||||
data: paginatedData,
|
|
||||||
total: totalData,
|
|
||||||
paging: {
|
|
||||||
page: page,
|
|
||||||
limit: limit,
|
|
||||||
total: totalData,
|
|
||||||
page_total: totalPages,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
};
|
|
||||||
};
|
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const token = localStorage.getItem('token');
|
const token = localStorage.getItem('token');
|
||||||
if (token) {
|
if (token) {
|
||||||
if (props.actionMode == 'list') {
|
if (props.actionMode == 'list') {
|
||||||
setFormDataFilter(defaultFilter);
|
|
||||||
doFilter();
|
doFilter();
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
navigate('/signin');
|
navigate('/signin');
|
||||||
}
|
}
|
||||||
}, [props.actionMode, tagsData]);
|
}, [props.actionMode]);
|
||||||
|
|
||||||
const toggleFilter = () => {
|
useEffect(() => {
|
||||||
setFormDataFilter(defaultFilter);
|
// Memicu filter setiap kali formDataFilter berubah
|
||||||
setShowFilter((prev) => !prev);
|
doFilter();
|
||||||
};
|
}, [formDataFilter]);
|
||||||
|
|
||||||
const doFilter = () => {
|
const doFilter = () => {
|
||||||
setTrigerFilter((prev) => !prev);
|
setTrigerFilter((prev) => !prev);
|
||||||
};
|
};
|
||||||
|
|
||||||
const handleSearch = () => {
|
const handleSearch = (value) => {
|
||||||
setFormDataFilter({ search: searchValue });
|
setSearchValue(value);
|
||||||
setTrigerFilter((prev) => !prev);
|
setFormDataFilter({ search: value });
|
||||||
};
|
};
|
||||||
|
|
||||||
const handleSearchClear = () => {
|
|
||||||
setSearchValue('');
|
|
||||||
setFormDataFilter({ search: '' });
|
|
||||||
setTrigerFilter((prev) => !prev);
|
|
||||||
};
|
|
||||||
|
|
||||||
const showPreviewModal = (param) => {
|
const showPreviewModal = (param) => {
|
||||||
props.setSelectedData(param);
|
props.setSelectedData(param);
|
||||||
props.setActionMode('preview');
|
props.setActionMode('preview');
|
||||||
@@ -389,21 +171,32 @@ const ListTag = memo(function ListTag(props) {
|
|||||||
};
|
};
|
||||||
|
|
||||||
const handleDelete = async (tag_id) => {
|
const handleDelete = async (tag_id) => {
|
||||||
// Find tag name before deleting
|
try {
|
||||||
const tagToDelete = tagsData.find((tag) => tag.tag_id === tag_id);
|
const response = await deleteTag(tag_id);
|
||||||
|
|
||||||
// Simulate delete API call
|
if (response && response.statusCode === 200) {
|
||||||
await new Promise((resolve) => setTimeout(resolve, 300));
|
NotifAlert({
|
||||||
|
icon: 'success',
|
||||||
// Remove from state
|
title: 'Berhasil',
|
||||||
const updatedTags = tagsData.filter((tag) => tag.tag_id !== tag_id);
|
message: response.message || 'Data Tag berhasil dihapus.',
|
||||||
setTagsData(updatedTags);
|
});
|
||||||
|
// Refresh table
|
||||||
NotifAlert({
|
doFilter();
|
||||||
icon: 'success',
|
} else {
|
||||||
title: 'Berhasil',
|
NotifAlert({
|
||||||
message: `Data Tag "${tagToDelete?.tag_name || ''}" berhasil dihapus.`,
|
icon: 'error',
|
||||||
});
|
title: 'Gagal',
|
||||||
|
message: response?.message || 'Terjadi kesalahan saat menghapus data.',
|
||||||
|
});
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
console.error('Delete Tag Error:', error);
|
||||||
|
NotifAlert({
|
||||||
|
icon: 'error',
|
||||||
|
title: 'Error',
|
||||||
|
message: error.message || 'Terjadi kesalahan pada server. Coba lagi nanti.',
|
||||||
|
});
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
return (
|
return (
|
||||||
@@ -417,18 +210,11 @@ const ListTag = memo(function ListTag(props) {
|
|||||||
placeholder="Search tag by code, name, or type..."
|
placeholder="Search tag by code, name, or type..."
|
||||||
value={searchValue}
|
value={searchValue}
|
||||||
onChange={(e) => {
|
onChange={(e) => {
|
||||||
const value = e.target.value;
|
const { value } = e.target;
|
||||||
setSearchValue(value);
|
setSearchValue(value);
|
||||||
// Auto search when clearing by backspace/delete
|
|
||||||
if (value === '') {
|
|
||||||
setFormDataFilter({ search: '' });
|
|
||||||
setTrigerFilter((prev) => !prev);
|
|
||||||
}
|
|
||||||
}}
|
}}
|
||||||
onSearch={handleSearch}
|
onSearch={handleSearch}
|
||||||
allowClear={{
|
allowClear
|
||||||
clearIcon: <span onClick={handleSearchClear}>✕</span>,
|
|
||||||
}}
|
|
||||||
enterButton={
|
enterButton={
|
||||||
<Button
|
<Button
|
||||||
type="primary"
|
type="primary"
|
||||||
|
|||||||
Reference in New Issue
Block a user