lavoce #2
@@ -1,13 +1,97 @@
|
|||||||
import React, { memo, useEffect } from 'react';
|
|
||||||
|
import React, { memo, useState, useEffect } from 'react';
|
||||||
import { useNavigate } from 'react-router-dom';
|
import { useNavigate } from 'react-router-dom';
|
||||||
import { useBreadcrumb } from '../../../layout/LayoutBreadcrumb';
|
import { useBreadcrumb } from '../../../layout/LayoutBreadcrumb';
|
||||||
import { Typography } from 'antd';
|
import { Form, Modal, Typography } from 'antd';
|
||||||
|
import ListBrandDevice from './component/ListBrandDevice';
|
||||||
|
import DetailBrandDevice from './component/DetailBrandDevice';
|
||||||
|
|
||||||
|
import { NotifConfirmDialog, NotifAlert } from '../../../components/Global/ToastNotif';
|
||||||
|
|
||||||
const { Text } = Typography;
|
const { Text } = Typography;
|
||||||
|
|
||||||
|
// Mock Data
|
||||||
|
const initialData = [
|
||||||
|
{
|
||||||
|
key: '1',
|
||||||
|
brandCode: 'HTC-01',
|
||||||
|
brandName: 'Brand A',
|
||||||
|
brandType: 'Type X',
|
||||||
|
deviceName: 'Device 1',
|
||||||
|
description: 'Description for Brand A',
|
||||||
|
},
|
||||||
|
{
|
||||||
|
key: '2',
|
||||||
|
brandCode: 'HTC-02',
|
||||||
|
brandName: 'Brand B',
|
||||||
|
brandType: 'Type Y',
|
||||||
|
deviceName: 'Device 2',
|
||||||
|
description: 'Description for Brand B',
|
||||||
|
},
|
||||||
|
{
|
||||||
|
key: '3',
|
||||||
|
brandCode: 'HTC-03',
|
||||||
|
brandName: 'Brand C',
|
||||||
|
brandType: 'Type Z',
|
||||||
|
deviceName: 'Device 3',
|
||||||
|
description: 'Description for Brand C',
|
||||||
|
},
|
||||||
|
// Add more data for pagination testing
|
||||||
|
...Array.from({ length: 20 }, (_, i) => ({
|
||||||
|
key: `${i + 4}`,
|
||||||
|
brandCode: `HTC-${String(i + 4).padStart(2, '0')}`,
|
||||||
|
brandName: `Brand ${String.fromCharCode(68 + i)}`,
|
||||||
|
brandType: `Type ${String.fromCharCode(88 + i)}`,
|
||||||
|
deviceName: `Device ${i + 4}`,
|
||||||
|
description: `Description for Brand ${String.fromCharCode(68 + i)}`,
|
||||||
|
})),
|
||||||
|
];
|
||||||
|
|
||||||
const IndexBrandDevice = memo(function IndexBrandDevice() {
|
const IndexBrandDevice = memo(function IndexBrandDevice() {
|
||||||
const navigate = useNavigate();
|
const navigate = useNavigate();
|
||||||
const { setBreadcrumbItems } = useBreadcrumb();
|
const { setBreadcrumbItems } = useBreadcrumb();
|
||||||
|
const [form] = Form.useForm();
|
||||||
|
|
||||||
|
const [data, setData] = useState(initialData);
|
||||||
|
const [actionMode, setActionMode] = useState('list');
|
||||||
|
const [editingKey, setEditingKey] = useState('');
|
||||||
|
const [isModalVisible, setIsModalVisible] = useState(false);
|
||||||
|
const [readOnly, setReadOnly] = useState(false);
|
||||||
|
|
||||||
|
// Mock API function
|
||||||
|
const getAllBrandDevice = async (params) => {
|
||||||
|
const { page = 1, limit = 10, search = '' } = Object.fromEntries(params.entries());
|
||||||
|
|
||||||
|
let filteredData = data;
|
||||||
|
if (search) {
|
||||||
|
filteredData = data.filter(item =>
|
||||||
|
item.brandName.toLowerCase().includes(search.toLowerCase()) ||
|
||||||
|
item.brandType.toLowerCase().includes(search.toLowerCase()) ||
|
||||||
|
item.deviceName.toLowerCase().includes(search.toLowerCase())
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
const start = (page - 1) * limit;
|
||||||
|
const end = start + limit;
|
||||||
|
const paginatedData = filteredData.slice(start, end);
|
||||||
|
|
||||||
|
return new Promise(resolve => {
|
||||||
|
setTimeout(() => {
|
||||||
|
resolve({
|
||||||
|
status: 200,
|
||||||
|
data: {
|
||||||
|
data: paginatedData,
|
||||||
|
total: filteredData.length,
|
||||||
|
paging: {
|
||||||
|
page: parseInt(page),
|
||||||
|
limit: parseInt(limit),
|
||||||
|
total: filteredData.length,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
});
|
||||||
|
}, 500);
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const token = localStorage.getItem('token');
|
const token = localStorage.getItem('token');
|
||||||
@@ -19,12 +103,98 @@ const IndexBrandDevice = memo(function IndexBrandDevice() {
|
|||||||
} else {
|
} else {
|
||||||
navigate('/signin');
|
navigate('/signin');
|
||||||
}
|
}
|
||||||
}, []);
|
}, [navigate, setBreadcrumbItems]);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (actionMode === 'add' || actionMode === 'edit' || actionMode === 'preview') {
|
||||||
|
setIsModalVisible(true);
|
||||||
|
setReadOnly(actionMode === 'preview');
|
||||||
|
} else {
|
||||||
|
setIsModalVisible(false);
|
||||||
|
}
|
||||||
|
}, [actionMode]);
|
||||||
|
|
||||||
|
const handleCancel = () => {
|
||||||
|
setActionMode('list');
|
||||||
|
setEditingKey('');
|
||||||
|
form.resetFields();
|
||||||
|
};
|
||||||
|
|
||||||
|
const handleOk = () => {
|
||||||
|
if (readOnly) {
|
||||||
|
handleCancel();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
form.validateFields()
|
||||||
|
.then((values) => {
|
||||||
|
let newData = [...data];
|
||||||
|
if (editingKey) {
|
||||||
|
// Editing existing data
|
||||||
|
const index = newData.findIndex((item) => editingKey === item.key);
|
||||||
|
if (index > -1) {
|
||||||
|
const item = newData[index];
|
||||||
|
newData.splice(index, 1, { ...item, ...values });
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// Adding new data
|
||||||
|
const newKey = (Math.max(...data.map(item => parseInt(item.key))) + 1).toString();
|
||||||
|
newData = [{ key: newKey, ...values }, ...newData];
|
||||||
|
}
|
||||||
|
setData(newData);
|
||||||
|
handleCancel();
|
||||||
|
})
|
||||||
|
.catch((info) => {
|
||||||
|
console.log('Validate Failed:', info);
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
const handleEdit = (record) => {
|
||||||
|
form.setFieldsValue(record);
|
||||||
|
setEditingKey(record.key);
|
||||||
|
setActionMode('edit');
|
||||||
|
};
|
||||||
|
|
||||||
|
const handlePreview = (record) => {
|
||||||
|
form.setFieldsValue(record);
|
||||||
|
setEditingKey(record.key);
|
||||||
|
setActionMode('preview');
|
||||||
|
};
|
||||||
|
|
||||||
|
const handleDelete = (record) => {
|
||||||
|
NotifConfirmDialog({
|
||||||
|
icon: 'question',
|
||||||
|
title: 'Konfirmasi',
|
||||||
|
message: `Apakah anda yakin ingin menghapus brand "${record.brandName}"?`,
|
||||||
|
onConfirm: () => {
|
||||||
|
const newData = data.filter((item) => item.key !== record.key);
|
||||||
|
setData(newData);
|
||||||
|
NotifAlert({
|
||||||
|
icon: 'success',
|
||||||
|
title: 'Berhasil',
|
||||||
|
message: `Brand "${record.brandName}" berhasil dihapus.`,
|
||||||
|
});
|
||||||
|
},
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div>
|
<React.Fragment>
|
||||||
<h1>Brand Device Page</h1>
|
<ListBrandDevice
|
||||||
</div>
|
setActionMode={setActionMode}
|
||||||
|
handleEdit={handleEdit}
|
||||||
|
handleDelete={handleDelete}
|
||||||
|
handlePreview={handlePreview}
|
||||||
|
getAllBrandDevice={getAllBrandDevice}
|
||||||
|
/>
|
||||||
|
<DetailBrandDevice
|
||||||
|
visible={isModalVisible}
|
||||||
|
onCancel={handleCancel}
|
||||||
|
onOk={handleOk}
|
||||||
|
form={form}
|
||||||
|
editingKey={editingKey}
|
||||||
|
readOnly={readOnly}
|
||||||
|
/>
|
||||||
|
</React.Fragment>
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
117
src/pages/master/brandDevice/component/DetailBrandDevice.jsx
Normal file
117
src/pages/master/brandDevice/component/DetailBrandDevice.jsx
Normal file
@@ -0,0 +1,117 @@
|
|||||||
|
import React from 'react';
|
||||||
|
import { Modal, Form, Input, ConfigProvider, Button, Typography } from 'antd';
|
||||||
|
|
||||||
|
const { Text } = Typography;
|
||||||
|
|
||||||
|
const DetailBrandDevice = ({ visible, onCancel, onOk, form, editingKey, readOnly }) => {
|
||||||
|
const modalTitle = readOnly ? 'Preview Brand' : (editingKey ? 'Edit Brand' : 'Tambah Brand');
|
||||||
|
|
||||||
|
return (
|
||||||
|
<Modal
|
||||||
|
title={modalTitle}
|
||||||
|
visible={visible}
|
||||||
|
onCancel={onCancel}
|
||||||
|
footer={[
|
||||||
|
<React.Fragment key="modal-footer">
|
||||||
|
<ConfigProvider
|
||||||
|
theme={{
|
||||||
|
token: { colorBgContainer: '#E9F6EF' },
|
||||||
|
components: {
|
||||||
|
Button: {
|
||||||
|
defaultBg: 'white',
|
||||||
|
defaultColor: '#23A55A',
|
||||||
|
defaultBorderColor: '#23A55A',
|
||||||
|
defaultHoverColor: '#23A55A',
|
||||||
|
defaultHoverBorderColor: '#23A55A',
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
<Button onClick={onCancel}>Batal</Button>
|
||||||
|
</ConfigProvider>
|
||||||
|
<ConfigProvider
|
||||||
|
theme={{
|
||||||
|
token: {
|
||||||
|
colorBgContainer: '#209652',
|
||||||
|
},
|
||||||
|
components: {
|
||||||
|
Button: {
|
||||||
|
defaultBg: '#23a55a',
|
||||||
|
defaultColor: '#FFFFFF',
|
||||||
|
defaultBorderColor: '#23a55a',
|
||||||
|
defaultHoverColor: '#FFFFFF',
|
||||||
|
defaultHoverBorderColor: '#23a55a',
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
{!readOnly && (
|
||||||
|
<Button onClick={onOk}>
|
||||||
|
Simpan
|
||||||
|
</Button>
|
||||||
|
)}
|
||||||
|
</ConfigProvider>
|
||||||
|
</React.Fragment>,
|
||||||
|
]}
|
||||||
|
destroyOnClose
|
||||||
|
>
|
||||||
|
<Form form={form} layout="vertical" name="form_in_modal">
|
||||||
|
<div style={{ marginBottom: 12 }}>
|
||||||
|
<Text strong>Kode Brand</Text>
|
||||||
|
<Text style={{ color: 'red' }}> *</Text>
|
||||||
|
<Form.Item
|
||||||
|
name="brandCode"
|
||||||
|
rules={[{ required: true, message: 'Please input the brand code!' }]}
|
||||||
|
style={{ marginBottom: 0 }}
|
||||||
|
>
|
||||||
|
<Input readOnly={readOnly} placeholder="Enter Brand Code" />
|
||||||
|
</Form.Item>
|
||||||
|
</div>
|
||||||
|
<div style={{ marginBottom: 12 }}>
|
||||||
|
<Text strong>Brand Name</Text>
|
||||||
|
<Text style={{ color: 'red' }}> *</Text>
|
||||||
|
<Form.Item
|
||||||
|
name="brandName"
|
||||||
|
rules={[{ required: true, message: 'Please input the brand name!' }]}
|
||||||
|
style={{ marginBottom: 0 }}
|
||||||
|
>
|
||||||
|
<Input readOnly={readOnly} placeholder="Enter Brand Name" />
|
||||||
|
</Form.Item>
|
||||||
|
</div>
|
||||||
|
<div style={{ marginBottom: 12 }}>
|
||||||
|
<Text strong>Brand Type</Text>
|
||||||
|
<Text style={{ color: 'red' }}> *</Text>
|
||||||
|
<Form.Item
|
||||||
|
name="brandType"
|
||||||
|
rules={[{ required: true, message: 'Please input the brand type!' }]}
|
||||||
|
style={{ marginBottom: 0 }}
|
||||||
|
>
|
||||||
|
<Input readOnly={readOnly} placeholder="Enter Brand Type" />
|
||||||
|
</Form.Item>
|
||||||
|
</div>
|
||||||
|
<div style={{ marginBottom: 12 }}>
|
||||||
|
<Text strong>Device Name</Text>
|
||||||
|
<Text style={{ color: 'red' }}> *</Text>
|
||||||
|
<Form.Item
|
||||||
|
name="deviceName"
|
||||||
|
rules={[{ required: true, message: 'Please input the device name!' }]}
|
||||||
|
style={{ marginBottom: 0 }}
|
||||||
|
>
|
||||||
|
<Input readOnly={readOnly} placeholder="Enter Device Name" />
|
||||||
|
</Form.Item>
|
||||||
|
</div>
|
||||||
|
<div style={{ marginBottom: 12 }}>
|
||||||
|
<Text strong>Description</Text>
|
||||||
|
<Form.Item
|
||||||
|
name="description"
|
||||||
|
style={{ marginBottom: 0 }}
|
||||||
|
>
|
||||||
|
<Input.TextArea readOnly={readOnly} placeholder="Enter Description (Optional)" rows={4} />
|
||||||
|
</Form.Item>
|
||||||
|
</div>
|
||||||
|
</Form>
|
||||||
|
</Modal>
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
export default DetailBrandDevice;
|
||||||
137
src/pages/master/brandDevice/component/ListBrandDevice.jsx
Normal file
137
src/pages/master/brandDevice/component/ListBrandDevice.jsx
Normal file
@@ -0,0 +1,137 @@
|
|||||||
|
import React, { useState } from 'react';
|
||||||
|
import { Button, Col, Row, Space, Input, ConfigProvider, Card } from 'antd';
|
||||||
|
import { PlusOutlined, EditOutlined, DeleteOutlined, SearchOutlined, EyeOutlined } from '@ant-design/icons';
|
||||||
|
import TableList from '../../../../components/Global/TableList';
|
||||||
|
|
||||||
|
const ListBrandDevice = ({
|
||||||
|
setActionMode,
|
||||||
|
handleEdit,
|
||||||
|
handleDelete,
|
||||||
|
handlePreview,
|
||||||
|
getAllBrandDevice // Mock API function
|
||||||
|
}) => {
|
||||||
|
const [searchValue, setSearchValue] = useState('');
|
||||||
|
const [formDataFilter, setFormDataFilter] = useState({ search: '' });
|
||||||
|
const [trigerFilter, setTrigerFilter] = useState(false);
|
||||||
|
|
||||||
|
const columns = [
|
||||||
|
{
|
||||||
|
title: 'Kode Brand',
|
||||||
|
dataIndex: 'brandCode',
|
||||||
|
key: 'brandCode',
|
||||||
|
},
|
||||||
|
{
|
||||||
|
title: 'Brand Name',
|
||||||
|
dataIndex: 'brandName',
|
||||||
|
key: 'brandName',
|
||||||
|
},
|
||||||
|
{
|
||||||
|
title: 'Brand Type',
|
||||||
|
dataIndex: 'brandType',
|
||||||
|
key: 'brandType',
|
||||||
|
},
|
||||||
|
{
|
||||||
|
title: 'Device Name',
|
||||||
|
dataIndex: 'deviceName',
|
||||||
|
key: 'deviceName',
|
||||||
|
},
|
||||||
|
{
|
||||||
|
title: 'Description',
|
||||||
|
dataIndex: 'description',
|
||||||
|
key: 'description',
|
||||||
|
},
|
||||||
|
{
|
||||||
|
title: 'Aksi',
|
||||||
|
key: 'action',
|
||||||
|
render: (_, record) => (
|
||||||
|
<Space size="middle">
|
||||||
|
<Button type="text" icon={<EyeOutlined style={{ color: '#1890ff' }} />} onClick={() => handlePreview(record)} />
|
||||||
|
<Button type="text" icon={<EditOutlined style={{ color: '#faad14' }} />} onClick={() => handleEdit(record)} />
|
||||||
|
<Button type="text" danger icon={<DeleteOutlined />} onClick={() => handleDelete(record)} />
|
||||||
|
</Space>
|
||||||
|
),
|
||||||
|
},
|
||||||
|
];
|
||||||
|
|
||||||
|
const handleSearch = () => {
|
||||||
|
setFormDataFilter({ search: searchValue });
|
||||||
|
setTrigerFilter((prev) => !prev);
|
||||||
|
};
|
||||||
|
|
||||||
|
const handleSearchClear = () => {
|
||||||
|
setSearchValue('');
|
||||||
|
setFormDataFilter({ search: '' });
|
||||||
|
setTrigerFilter((prev) => !prev);
|
||||||
|
};
|
||||||
|
|
||||||
|
return (
|
||||||
|
<Card>
|
||||||
|
<Row justify="space-between" align="middle" gutter={[8, 8]}>
|
||||||
|
<Col xs={24} sm={24} md={12} lg={12}>
|
||||||
|
<Input.Search
|
||||||
|
placeholder="Search by brand name, type, or device name..."
|
||||||
|
value={searchValue}
|
||||||
|
onChange={(e) => {
|
||||||
|
const value = e.target.value;
|
||||||
|
setSearchValue(value);
|
||||||
|
if (value === '') {
|
||||||
|
handleSearchClear();
|
||||||
|
}
|
||||||
|
}}
|
||||||
|
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>
|
||||||
|
<ConfigProvider
|
||||||
|
theme={{
|
||||||
|
token: { colorBgContainer: '#E9F6EF' },
|
||||||
|
components: {
|
||||||
|
Button: {
|
||||||
|
defaultBg: 'white',
|
||||||
|
defaultColor: '#23A55A',
|
||||||
|
defaultBorderColor: '#23A55A',
|
||||||
|
defaultHoverColor: '#23A55A',
|
||||||
|
defaultHoverBorderColor: '#23A55A',
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
<Button
|
||||||
|
icon={<PlusOutlined />}
|
||||||
|
onClick={() => setActionMode('add')}
|
||||||
|
size="large"
|
||||||
|
>
|
||||||
|
Tambah Data
|
||||||
|
</Button>
|
||||||
|
</ConfigProvider>
|
||||||
|
</Col>
|
||||||
|
</Row>
|
||||||
|
<Col xs={24} sm={24} md={24} lg={24} xl={24} style={{ marginTop: '16px' }}>
|
||||||
|
<TableList
|
||||||
|
getData={getAllBrandDevice}
|
||||||
|
queryParams={formDataFilter}
|
||||||
|
columns={columns}
|
||||||
|
triger={trigerFilter}
|
||||||
|
/>
|
||||||
|
</Col>
|
||||||
|
</Card>
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
export default ListBrandDevice;
|
||||||
Reference in New Issue
Block a user