lavoce #2
@@ -6,8 +6,6 @@ const getAllTag = async (queryParams) => {
|
||||
method: 'get',
|
||||
prefix: `tags?${queryParams.toString()}`,
|
||||
});
|
||||
console.log('getAllTag response:', response);
|
||||
console.log('Query params:', queryParams.toString());
|
||||
|
||||
// Check if response has error
|
||||
if (response.error) {
|
||||
@@ -106,8 +104,6 @@ const createTag = async (queryParams) => {
|
||||
prefix: `tags`,
|
||||
params: queryParams,
|
||||
});
|
||||
console.log('createTag full response:', response);
|
||||
console.log('createTag payload sent:', queryParams);
|
||||
|
||||
// Check if response has error flag
|
||||
if (response.error) {
|
||||
@@ -134,8 +130,6 @@ const updateTag = async (tag_id, queryParams) => {
|
||||
prefix: `tags/${tag_id}`,
|
||||
params: queryParams,
|
||||
});
|
||||
console.log('updateTag full response:', response);
|
||||
console.log('updateTag payload sent:', queryParams);
|
||||
|
||||
// Check if response has error flag
|
||||
if (response.error) {
|
||||
@@ -161,7 +155,6 @@ const deleteTag = async (queryParams) => {
|
||||
method: 'delete',
|
||||
prefix: `tags/${queryParams}`,
|
||||
});
|
||||
console.log('deleteTag full response:', response);
|
||||
|
||||
// Check if response has error flag
|
||||
if (response.error) {
|
||||
|
||||
@@ -1,20 +1,27 @@
|
||||
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 { createTag, updateTag } from '../../../../api/master-tag';
|
||||
import { getAllDevice } from '../../../../api/master-device';
|
||||
|
||||
const { Text } = Typography;
|
||||
const { TextArea } = Input;
|
||||
|
||||
const DetailTag = (props) => {
|
||||
const [confirmLoading, setConfirmLoading] = useState(false);
|
||||
const [deviceList, setDeviceList] = useState([]);
|
||||
const [loadingDevices, setLoadingDevices] = useState(false);
|
||||
|
||||
const defaultData = {
|
||||
tag_id: '',
|
||||
tag_code: '',
|
||||
tag_name: '',
|
||||
tag_value: 'Off',
|
||||
tag_type: 'alarm',
|
||||
tag_description: '',
|
||||
tag_number: '',
|
||||
data_type: '',
|
||||
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);
|
||||
@@ -27,18 +34,8 @@ const DetailTag = (props) => {
|
||||
const handleSave = async () => {
|
||||
setConfirmLoading(true);
|
||||
|
||||
// Validasi required fields
|
||||
if (!FormData.tag_code) {
|
||||
NotifOk({
|
||||
icon: 'warning',
|
||||
title: 'Peringatan',
|
||||
message: 'Kolom Tag Code Tidak Boleh Kosong',
|
||||
});
|
||||
setConfirmLoading(false);
|
||||
return;
|
||||
}
|
||||
|
||||
if (!FormData.tag_name) {
|
||||
// Validasi required fields untuk CREATE
|
||||
if (!FormData.tag_name || FormData.tag_name.trim() === '') {
|
||||
NotifOk({
|
||||
icon: 'warning',
|
||||
title: 'Peringatan',
|
||||
@@ -48,55 +45,114 @@ const DetailTag = (props) => {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!FormData.tag_value) {
|
||||
if (!FormData.tag_number || FormData.tag_number === '') {
|
||||
NotifOk({
|
||||
icon: 'warning',
|
||||
title: 'Peringatan',
|
||||
message: 'Kolom Tag Value Tidak Boleh Kosong',
|
||||
message: 'Kolom Tag Number Tidak Boleh Kosong',
|
||||
});
|
||||
setConfirmLoading(false);
|
||||
return;
|
||||
}
|
||||
|
||||
if (!FormData.tag_type) {
|
||||
// Validasi format number untuk tag_number
|
||||
const tagNumberInt = parseInt(FormData.tag_number);
|
||||
if (isNaN(tagNumberInt)) {
|
||||
NotifOk({
|
||||
icon: 'warning',
|
||||
title: 'Peringatan',
|
||||
message: 'Kolom Tag Type Tidak Boleh Kosong',
|
||||
message: 'Tag Number harus berupa angka yang valid',
|
||||
});
|
||||
setConfirmLoading(false);
|
||||
return;
|
||||
}
|
||||
|
||||
const payload = {
|
||||
tag_code: FormData.tag_code,
|
||||
tag_name: FormData.tag_name,
|
||||
tag_value: FormData.tag_value,
|
||||
tag_type: FormData.tag_type,
|
||||
tag_description: FormData.tag_description,
|
||||
};
|
||||
if (!FormData.data_type || FormData.data_type.trim() === '') {
|
||||
NotifOk({
|
||||
icon: 'warning',
|
||||
title: 'Peringatan',
|
||||
message: 'Kolom Data Type Tidak Boleh Kosong',
|
||||
});
|
||||
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 {
|
||||
// Simulate API call
|
||||
await new Promise((resolve) => setTimeout(resolve, 500));
|
||||
let response;
|
||||
|
||||
const response = {
|
||||
statusCode: FormData.tag_id ? 200 : 201,
|
||||
data: {
|
||||
tag_name: FormData.tag_name,
|
||||
},
|
||||
};
|
||||
|
||||
console.log('Save Tag Response:', response);
|
||||
if (FormData.tag_id) {
|
||||
// Update existing tag
|
||||
response = await updateTag(FormData.tag_id, payload);
|
||||
} else {
|
||||
// Create new tag
|
||||
response = await createTag(payload);
|
||||
}
|
||||
|
||||
// Check if response is successful
|
||||
if (response && (response.statusCode === 200 || response.statusCode === 201)) {
|
||||
NotifOk({
|
||||
icon: 'success',
|
||||
title: 'Berhasil',
|
||||
message: `Data Tag "${response.data?.tag_name || FormData.tag_name}" berhasil ${
|
||||
FormData.tag_id ? 'diubah' : 'ditambahkan'
|
||||
}.`,
|
||||
message:
|
||||
response.message ||
|
||||
`Data Tag "${response.data?.tag_name || FormData.tag_name}" berhasil ${
|
||||
FormData.tag_id ? 'diubah' : 'ditambahkan'
|
||||
}.`,
|
||||
});
|
||||
|
||||
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(() => {
|
||||
const token = localStorage.getItem('token');
|
||||
if (token) {
|
||||
if (props.showModal) {
|
||||
// Load devices when modal opens
|
||||
loadDevices();
|
||||
}
|
||||
|
||||
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 {
|
||||
setFormData(defaultData);
|
||||
}
|
||||
@@ -169,8 +274,6 @@ const DetailTag = (props) => {
|
||||
defaultColor: '#23A55A',
|
||||
defaultBorderColor: '#23A55A',
|
||||
defaultHoverColor: '#23A55A',
|
||||
defaultHoverBorderColor: '#23A55A',
|
||||
defaultHoverColor: '#23A55A',
|
||||
},
|
||||
},
|
||||
}}
|
||||
@@ -213,17 +316,19 @@ const DetailTag = (props) => {
|
||||
disabled
|
||||
/>
|
||||
</div>
|
||||
<div style={{ marginBottom: 12 }}>
|
||||
<Text strong>Tag Code</Text>
|
||||
<Text style={{ color: 'red' }}> *</Text>
|
||||
<Input
|
||||
name="tag_code"
|
||||
value={FormData.tag_code}
|
||||
onChange={handleInputChange}
|
||||
placeholder="Enter Tag Code"
|
||||
readOnly={props.readOnly}
|
||||
/>
|
||||
</div>
|
||||
{/* Tag Code hanya ditampilkan saat EDIT atau PREVIEW */}
|
||||
{(props.actionMode === 'edit' || props.actionMode === 'preview') && (
|
||||
<div style={{ marginBottom: 12 }}>
|
||||
<Text strong>Tag Code</Text>
|
||||
<Input
|
||||
name="tag_code"
|
||||
value={FormData.tag_code}
|
||||
onChange={handleInputChange}
|
||||
placeholder="Auto Generate"
|
||||
disabled
|
||||
/>
|
||||
</div>
|
||||
)}
|
||||
<div style={{ marginBottom: 12 }}>
|
||||
<Text strong>Tag Name</Text>
|
||||
<Text style={{ color: 'red' }}> *</Text>
|
||||
@@ -236,47 +341,97 @@ const DetailTag = (props) => {
|
||||
/>
|
||||
</div>
|
||||
<div style={{ marginBottom: 12 }}>
|
||||
<Text strong>Tag Value</Text>
|
||||
<Text strong>Tag Number</Text>
|
||||
<Text style={{ color: 'red' }}> *</Text>
|
||||
<Select
|
||||
value={FormData.tag_value}
|
||||
onChange={(value) => handleSelectChange('tag_value', value)}
|
||||
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}
|
||||
<Input
|
||||
name="tag_number"
|
||||
value={FormData.tag_number}
|
||||
onChange={handleInputChange}
|
||||
placeholder="Enter Tag Description (Optional)"
|
||||
placeholder="Enter Tag Number"
|
||||
readOnly={props.readOnly}
|
||||
rows={4}
|
||||
/>
|
||||
</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>
|
||||
)}
|
||||
</Modal>
|
||||
|
||||
@@ -10,170 +10,7 @@ import {
|
||||
import { NotifAlert, NotifConfirmDialog } from '../../../../components/Global/ToastNotif';
|
||||
import { useNavigate } from 'react-router-dom';
|
||||
import TableList from '../../../../components/Global/TableList';
|
||||
|
||||
// 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',
|
||||
},
|
||||
];
|
||||
import { getAllTag, deleteTag } from '../../../../api/master-tag';
|
||||
|
||||
const columns = (showPreviewModal, showEditModal, showDeleteDialog) => [
|
||||
{
|
||||
@@ -181,7 +18,7 @@ const columns = (showPreviewModal, showEditModal, showDeleteDialog) => [
|
||||
dataIndex: 'tag_id',
|
||||
key: 'tag_id',
|
||||
width: '5%',
|
||||
hidden: 'true',
|
||||
hidden: true,
|
||||
},
|
||||
{
|
||||
title: 'Tag Code',
|
||||
@@ -193,38 +30,49 @@ const columns = (showPreviewModal, showEditModal, showDeleteDialog) => [
|
||||
title: 'Tag Name',
|
||||
dataIndex: 'tag_name',
|
||||
key: 'tag_name',
|
||||
width: '20%',
|
||||
width: '15%',
|
||||
},
|
||||
{
|
||||
title: 'Value',
|
||||
dataIndex: 'tag_value',
|
||||
key: 'tag_value',
|
||||
title: 'Tag Number',
|
||||
dataIndex: 'tag_number',
|
||||
key: 'tag_number',
|
||||
width: '10%',
|
||||
align: 'center',
|
||||
render: (_, { tag_value }) => {
|
||||
let color = 'default';
|
||||
if (tag_value.toLowerCase() === 'on') color = 'green';
|
||||
else if (tag_value.toLowerCase() === 'off') color = 'gray';
|
||||
else if (tag_value.toLowerCase() === 'critical') color = 'red';
|
||||
},
|
||||
{
|
||||
title: 'Data Type',
|
||||
dataIndex: 'data_type',
|
||||
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 (
|
||||
<Tag color={color} key={'value'}>
|
||||
{tag_value}
|
||||
<Tag color={color} key={'status'}>
|
||||
{text}
|
||||
</Tag>
|
||||
);
|
||||
},
|
||||
},
|
||||
{
|
||||
title: 'Type',
|
||||
dataIndex: 'tag_type',
|
||||
key: 'tag_type',
|
||||
width: '10%',
|
||||
},
|
||||
{
|
||||
title: 'Description',
|
||||
dataIndex: 'tag_description',
|
||||
key: 'tag_description',
|
||||
width: '25%',
|
||||
},
|
||||
{
|
||||
title: 'Aksi',
|
||||
key: 'aksi',
|
||||
@@ -265,9 +113,7 @@ const columns = (showPreviewModal, showEditModal, showDeleteDialog) => [
|
||||
];
|
||||
|
||||
const ListTag = memo(function ListTag(props) {
|
||||
const [showFilter, setShowFilter] = useState(false);
|
||||
const [trigerFilter, setTrigerFilter] = useState(false);
|
||||
const [tagsData, setTagsData] = useState(initialTagsData);
|
||||
|
||||
const defaultFilter = { search: '' };
|
||||
const [formDataFilter, setFormDataFilter] = useState(defaultFilter);
|
||||
@@ -275,94 +121,30 @@ const ListTag = memo(function ListTag(props) {
|
||||
|
||||
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(() => {
|
||||
const token = localStorage.getItem('token');
|
||||
if (token) {
|
||||
if (props.actionMode == 'list') {
|
||||
setFormDataFilter(defaultFilter);
|
||||
doFilter();
|
||||
}
|
||||
} else {
|
||||
navigate('/signin');
|
||||
}
|
||||
}, [props.actionMode, tagsData]);
|
||||
}, [props.actionMode]);
|
||||
|
||||
const toggleFilter = () => {
|
||||
setFormDataFilter(defaultFilter);
|
||||
setShowFilter((prev) => !prev);
|
||||
};
|
||||
useEffect(() => {
|
||||
// Memicu filter setiap kali formDataFilter berubah
|
||||
doFilter();
|
||||
}, [formDataFilter]);
|
||||
|
||||
const doFilter = () => {
|
||||
setTrigerFilter((prev) => !prev);
|
||||
};
|
||||
|
||||
const handleSearch = () => {
|
||||
setFormDataFilter({ search: searchValue });
|
||||
setTrigerFilter((prev) => !prev);
|
||||
const handleSearch = (value) => {
|
||||
setSearchValue(value);
|
||||
setFormDataFilter({ search: value });
|
||||
};
|
||||
|
||||
const handleSearchClear = () => {
|
||||
setSearchValue('');
|
||||
setFormDataFilter({ search: '' });
|
||||
setTrigerFilter((prev) => !prev);
|
||||
};
|
||||
|
||||
const showPreviewModal = (param) => {
|
||||
props.setSelectedData(param);
|
||||
props.setActionMode('preview');
|
||||
@@ -389,21 +171,32 @@ const ListTag = memo(function ListTag(props) {
|
||||
};
|
||||
|
||||
const handleDelete = async (tag_id) => {
|
||||
// Find tag name before deleting
|
||||
const tagToDelete = tagsData.find((tag) => tag.tag_id === tag_id);
|
||||
try {
|
||||
const response = await deleteTag(tag_id);
|
||||
|
||||
// Simulate delete API call
|
||||
await new Promise((resolve) => setTimeout(resolve, 300));
|
||||
|
||||
// Remove from state
|
||||
const updatedTags = tagsData.filter((tag) => tag.tag_id !== tag_id);
|
||||
setTagsData(updatedTags);
|
||||
|
||||
NotifAlert({
|
||||
icon: 'success',
|
||||
title: 'Berhasil',
|
||||
message: `Data Tag "${tagToDelete?.tag_name || ''}" berhasil dihapus.`,
|
||||
});
|
||||
if (response && response.statusCode === 200) {
|
||||
NotifAlert({
|
||||
icon: 'success',
|
||||
title: 'Berhasil',
|
||||
message: response.message || 'Data Tag berhasil dihapus.',
|
||||
});
|
||||
// Refresh table
|
||||
doFilter();
|
||||
} else {
|
||||
NotifAlert({
|
||||
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 (
|
||||
@@ -417,18 +210,11 @@ const ListTag = memo(function ListTag(props) {
|
||||
placeholder="Search tag by code, name, or type..."
|
||||
value={searchValue}
|
||||
onChange={(e) => {
|
||||
const value = e.target.value;
|
||||
const { value } = e.target;
|
||||
setSearchValue(value);
|
||||
// Auto search when clearing by backspace/delete
|
||||
if (value === '') {
|
||||
setFormDataFilter({ search: '' });
|
||||
setTrigerFilter((prev) => !prev);
|
||||
}
|
||||
}}
|
||||
onSearch={handleSearch}
|
||||
allowClear={{
|
||||
clearIcon: <span onClick={handleSearchClear}>✕</span>,
|
||||
}}
|
||||
allowClear
|
||||
enterButton={
|
||||
<Button
|
||||
type="primary"
|
||||
@@ -486,4 +272,4 @@ const ListTag = memo(function ListTag(props) {
|
||||
);
|
||||
});
|
||||
|
||||
export default ListTag;
|
||||
export default ListTag;
|
||||
|
||||
Reference in New Issue
Block a user