update: enhance device API functions with pagination and response structure; modify device form validation and UI components
This commit is contained in:
@@ -1,9 +1,17 @@
|
||||
import React, { memo, useState, useEffect } from 'react';
|
||||
import {
|
||||
import {
|
||||
Space,
|
||||
Tag,
|
||||
ConfigProvider,
|
||||
Button, Row, Col, Card, Divider, Form, Input, Dropdown } from 'antd';
|
||||
Button,
|
||||
Row,
|
||||
Col,
|
||||
Card,
|
||||
Divider,
|
||||
Form,
|
||||
Input,
|
||||
Dropdown,
|
||||
} from 'antd';
|
||||
import {
|
||||
PlusOutlined,
|
||||
FilterOutlined,
|
||||
@@ -13,11 +21,12 @@ import {
|
||||
SearchOutlined,
|
||||
FilePdfOutlined,
|
||||
FileExcelOutlined,
|
||||
EllipsisOutlined
|
||||
EllipsisOutlined,
|
||||
} from '@ant-design/icons';
|
||||
import { NotifAlert, NotifOk, NotifConfirmDialog } from '../../../../components/Global/ToastNotif';
|
||||
import { useNavigate } from 'react-router-dom';
|
||||
import { deleteApd, getAllApd } from '../../../../api/master-apd';
|
||||
import { deleteDevice, getAllDevice } from '../../../../api/master-device';
|
||||
import TableList from '../../../../components/Global/TableList';
|
||||
import { getFilterData } from '../../../../components/Global/DataFilter';
|
||||
import ExcelJS from 'exceljs';
|
||||
@@ -27,32 +36,50 @@ import logoPiEnergi from '../../../../assets/images/logo/pi-energi.png';
|
||||
const columns = (items, handleClickMenu) => [
|
||||
{
|
||||
title: 'ID',
|
||||
dataIndex: 'id_apd',
|
||||
key: 'id_apd',
|
||||
dataIndex: 'device_id',
|
||||
key: 'device_id',
|
||||
width: '5%',
|
||||
hidden: 'true',
|
||||
},
|
||||
{
|
||||
title: 'Device Name',
|
||||
dataIndex: 'nama_apd',
|
||||
key: 'nama_apd',
|
||||
width: '55%',
|
||||
title: 'Device Code',
|
||||
dataIndex: 'device_code',
|
||||
key: 'device_code',
|
||||
width: '8%',
|
||||
},
|
||||
{
|
||||
title: 'Aktif',
|
||||
dataIndex: 'is_active',
|
||||
key: 'is_active',
|
||||
title: 'Device Name',
|
||||
dataIndex: 'device_name',
|
||||
key: 'device_name',
|
||||
width: '20%',
|
||||
},
|
||||
{
|
||||
title: 'Location',
|
||||
dataIndex: 'device_location',
|
||||
key: 'device_location',
|
||||
width: '12%',
|
||||
},
|
||||
{
|
||||
title: 'IP Address',
|
||||
dataIndex: 'ip_address',
|
||||
key: 'ip_address',
|
||||
width: '13%',
|
||||
},
|
||||
{
|
||||
title: 'Status',
|
||||
dataIndex: 'device_status',
|
||||
key: 'device_status',
|
||||
width: '10%',
|
||||
align: 'center',
|
||||
render: (_, { is_active }) => (
|
||||
render: (_, { device_status }) => (
|
||||
<>
|
||||
{is_active === true ? (
|
||||
<Tag color={'green'} key={'aaa'}>
|
||||
Aktif
|
||||
{device_status === true ? (
|
||||
<Tag color={'green'} key={'status'}>
|
||||
Running
|
||||
</Tag>
|
||||
) : (
|
||||
<Tag color={'red'} key={'aaa'}>
|
||||
Non-Aktif
|
||||
<Tag color={'red'} key={'status'}>
|
||||
Offline
|
||||
</Tag>
|
||||
)}
|
||||
</>
|
||||
@@ -67,15 +94,12 @@ const columns = (items, handleClickMenu) => [
|
||||
<Dropdown
|
||||
menu={{
|
||||
items,
|
||||
onClick: ({key})=>handleClickMenu(key, record)
|
||||
onClick: ({ key }) => handleClickMenu(key, record),
|
||||
}}
|
||||
trigger={['click']}
|
||||
placement="bottomRight"
|
||||
>
|
||||
<Button
|
||||
shape="default"
|
||||
icon={<EllipsisOutlined />}
|
||||
/>
|
||||
<Button shape="default" icon={<EllipsisOutlined />} />
|
||||
</Dropdown>
|
||||
),
|
||||
},
|
||||
@@ -85,8 +109,9 @@ const ListDevice = memo(function ListDevice(props) {
|
||||
const [showFilter, setShowFilter] = useState(false);
|
||||
const [trigerFilter, setTrigerFilter] = useState(false);
|
||||
|
||||
const defaultFilter = { nama_apd: '' };
|
||||
const defaultFilter = { search: '' };
|
||||
const [formDataFilter, setFormDataFilter] = useState(defaultFilter);
|
||||
const [searchValue, setSearchValue] = useState('');
|
||||
|
||||
const navigate = useNavigate();
|
||||
|
||||
@@ -111,12 +136,15 @@ const ListDevice = memo(function ListDevice(props) {
|
||||
setTrigerFilter((prev) => !prev);
|
||||
};
|
||||
|
||||
const handleInputChangeFilter = (e) => {
|
||||
const { name, value } = e.target;
|
||||
setFormDataFilter((prevData) => ({
|
||||
...prevData,
|
||||
[name]: value,
|
||||
}));
|
||||
const handleSearch = () => {
|
||||
setFormDataFilter({ search: searchValue });
|
||||
setTrigerFilter((prev) => !prev);
|
||||
};
|
||||
|
||||
const handleSearchClear = () => {
|
||||
setSearchValue('');
|
||||
setFormDataFilter({ search: '' });
|
||||
setTrigerFilter((prev) => !prev);
|
||||
};
|
||||
|
||||
const showPreviewModal = (param) => {
|
||||
@@ -138,27 +166,27 @@ const ListDevice = memo(function ListDevice(props) {
|
||||
NotifConfirmDialog({
|
||||
icon: 'question',
|
||||
title: 'Konfirmasi',
|
||||
message: 'Apakah anda yakin hapus data "' + param.nama_apd + '" ?',
|
||||
onConfirm: () => handleDelete(param.id_apd),
|
||||
message: 'Apakah anda yakin hapus data "' + param.device_name + '" ?',
|
||||
onConfirm: () => handleDelete(param.device_id),
|
||||
onCancel: () => props.setSelectedData(null),
|
||||
});
|
||||
};
|
||||
|
||||
const handleDelete = async (id_apd) => {
|
||||
const response = await deleteApd(id_apd);
|
||||
const handleDelete = async (device_id) => {
|
||||
const response = await deleteDevice(device_id);
|
||||
|
||||
if (response.statusCode == 200) {
|
||||
NotifAlert({
|
||||
icon: 'success',
|
||||
title: 'Berhasil',
|
||||
message: 'Data Data "' + response.data[0].nama_apd + '" berhasil dihapus.',
|
||||
message: 'Data Device "' + response.data.device_name + '" berhasil dihapus.',
|
||||
});
|
||||
doFilter();
|
||||
} else {
|
||||
NotifOk({
|
||||
icon: 'error',
|
||||
title: 'Gagal',
|
||||
message: 'Gagal Menghapus Data Data "' + response.data[0].nama_apd + '"',
|
||||
message: 'Gagal Menghapus Data Device',
|
||||
});
|
||||
}
|
||||
};
|
||||
@@ -167,7 +195,7 @@ const ListDevice = memo(function ListDevice(props) {
|
||||
props.setActionMode('generatepdf');
|
||||
};
|
||||
|
||||
const exportExcel = async()=>{
|
||||
const exportExcel = async () => {
|
||||
const data = getFilterData();
|
||||
|
||||
const workbook = new ExcelJS.Workbook();
|
||||
@@ -217,7 +245,7 @@ const ListDevice = memo(function ListDevice(props) {
|
||||
headerRow.eachCell((cell) => {
|
||||
cell.alignment = {
|
||||
horizontal: 'center', // rata tengah kiri-kanan
|
||||
vertical: 'middle', // rata tengah atas-bawah
|
||||
vertical: 'middle', // rata tengah atas-bawah
|
||||
};
|
||||
});
|
||||
|
||||
@@ -238,8 +266,8 @@ const ListDevice = memo(function ListDevice(props) {
|
||||
sheet.columns.forEach((col) => {
|
||||
let maxLength = 10;
|
||||
col.eachCell({ includeEmpty: true }, (cell) => {
|
||||
const len = cell.value?.toString().length || 0;
|
||||
if (len > maxLength) maxLength = len;
|
||||
const len = cell.value?.toString().length || 0;
|
||||
if (len > maxLength) maxLength = len;
|
||||
});
|
||||
col.width = maxLength + 2;
|
||||
});
|
||||
@@ -271,18 +299,18 @@ const ListDevice = memo(function ListDevice(props) {
|
||||
const menu = [
|
||||
{
|
||||
key: 'preview',
|
||||
label: <span style={{fontSize:'17px'}}>Preview</span>,
|
||||
icon: <EyeOutlined style={{fontSize:'17px', marginTop:'5px'}} />,
|
||||
label: <span style={{ fontSize: '17px' }}>Preview</span>,
|
||||
icon: <EyeOutlined style={{ fontSize: '17px', marginTop: '5px' }} />,
|
||||
},
|
||||
{
|
||||
key: 'edit',
|
||||
label: <span style={{fontSize:'17px'}}>Edit</span>,
|
||||
icon: <EditOutlined style={{fontSize:'17px'}} />,
|
||||
label: <span style={{ fontSize: '17px' }}>Edit</span>,
|
||||
icon: <EditOutlined style={{ fontSize: '17px' }} />,
|
||||
},
|
||||
{
|
||||
key: 'delete',
|
||||
label: <span style={{fontSize:'17px'}}>Delete</span>,
|
||||
icon: <DeleteOutlined style={{fontSize:'17px'}} />,
|
||||
label: <span style={{ fontSize: '17px' }}>Delete</span>,
|
||||
icon: <DeleteOutlined style={{ fontSize: '17px' }} />,
|
||||
danger: true,
|
||||
},
|
||||
];
|
||||
@@ -293,23 +321,37 @@ const ListDevice = memo(function ListDevice(props) {
|
||||
<Row>
|
||||
<Col xs={24}>
|
||||
<Row justify="space-between" align="middle" gutter={[8, 8]}>
|
||||
<Col>
|
||||
<ConfigProvider
|
||||
theme={{
|
||||
token: { colorBgContainer: '#E9F6EF' },
|
||||
components: {
|
||||
Button: {
|
||||
defaultBg: 'white',
|
||||
defaultColor: '#23A55A',
|
||||
defaultBorderColor: '#23A55A',
|
||||
},
|
||||
},
|
||||
<Col xs={24} sm={24} md={12} lg={12}>
|
||||
<Input.Search
|
||||
placeholder="Search device by name, code, or location..."
|
||||
value={searchValue}
|
||||
onChange={(e) => {
|
||||
const value = e.target.value;
|
||||
setSearchValue(value);
|
||||
// Auto search when clearing by backspace/delete
|
||||
if (value === '') {
|
||||
setFormDataFilter({ search: '' });
|
||||
setTrigerFilter((prev) => !prev);
|
||||
}
|
||||
}}
|
||||
>
|
||||
<Button onClick={toggleFilter} icon={<FilterOutlined />}>
|
||||
Filter
|
||||
</Button>
|
||||
</ConfigProvider>
|
||||
onSearch={handleSearch}
|
||||
allowClear={{
|
||||
clearIcon: <span onClick={handleSearchClear}>✕</span>,
|
||||
}}
|
||||
enterButton={
|
||||
<Button
|
||||
type="primary"
|
||||
icon={<SearchOutlined />}
|
||||
style={{
|
||||
backgroundColor: '#23A55A',
|
||||
borderColor: '#23A55A',
|
||||
}}
|
||||
>
|
||||
Search
|
||||
</Button>
|
||||
}
|
||||
size="large"
|
||||
/>
|
||||
</Col>
|
||||
<Col>
|
||||
<Space wrap size="small">
|
||||
@@ -330,6 +372,7 @@ const ListDevice = memo(function ListDevice(props) {
|
||||
<Button
|
||||
icon={<PlusOutlined />}
|
||||
onClick={() => showAddModal()}
|
||||
size="large"
|
||||
>
|
||||
Tambah Data
|
||||
</Button>
|
||||
@@ -338,62 +381,9 @@ const ListDevice = memo(function ListDevice(props) {
|
||||
</Col>
|
||||
</Row>
|
||||
</Col>
|
||||
|
||||
{/* filter */}
|
||||
<Col xs={24} sm={24} md={24} lg={24} xl={24} style={{ marginTop: '16px' }}>
|
||||
{showFilter && (
|
||||
<>
|
||||
<Divider />
|
||||
<Form layout="vertical">
|
||||
<Form.Item label="Nama APD">
|
||||
<div
|
||||
style={{
|
||||
display: 'flex',
|
||||
gap: '8px',
|
||||
alignItems: 'center',
|
||||
maxWidth: '500px',
|
||||
width: '100%',
|
||||
}}
|
||||
>
|
||||
<Input
|
||||
name="nama_apd"
|
||||
value={formDataFilter.nama_apd}
|
||||
onChange={handleInputChangeFilter}
|
||||
placeholder="Enter Nama APD"
|
||||
style={{ height: '40px' }}
|
||||
/>
|
||||
<ConfigProvider
|
||||
theme={{
|
||||
token: {
|
||||
colorText: 'white',
|
||||
colorBgContainer: 'purple',
|
||||
},
|
||||
components: {
|
||||
Button: {
|
||||
defaultBg: 'white',
|
||||
defaultColor: '#23A55A',
|
||||
defaultBorderColor: '#23A55A',
|
||||
},
|
||||
},
|
||||
}}
|
||||
>
|
||||
<Button
|
||||
icon={<SearchOutlined />}
|
||||
onClick={doFilter}
|
||||
style={{ height: '40px' }}
|
||||
>
|
||||
Cari
|
||||
</Button>
|
||||
</ConfigProvider>
|
||||
</div>
|
||||
</Form.Item>
|
||||
</Form>
|
||||
</>
|
||||
)}
|
||||
</Col>
|
||||
<Col xs={24} sm={24} md={24} lg={24} xl={24} style={{ marginTop: '16px' }}>
|
||||
<TableList
|
||||
getData={getAllApd}
|
||||
getData={getAllDevice}
|
||||
queryParams={formDataFilter}
|
||||
columns={columns(menu, handleClickMenu)}
|
||||
triger={trigerFilter}
|
||||
@@ -405,4 +395,4 @@ const ListDevice = memo(function ListDevice(props) {
|
||||
);
|
||||
});
|
||||
|
||||
export default ListDevice;
|
||||
export default ListDevice;
|
||||
|
||||
Reference in New Issue
Block a user