Refactor: integration shift

This commit is contained in:
2025-10-24 10:27:56 +07:00
parent a3d24a9426
commit cf1ad6d511
2 changed files with 391 additions and 192 deletions

View File

@@ -9,19 +9,23 @@ import {
} from '@ant-design/icons';
import { NotifAlert, NotifConfirmDialog } from '../../../../components/Global/ToastNotif';
import { useNavigate } from 'react-router-dom';
import { deleteShift, getAllShift } from '../../../../api/master-shift';
import TableList from '../../../../components/Global/TableList';
// import { getAllShift, deleteShift } from '../../../../api/master-shift'; // <-- API needs to be created
import dayjs from 'dayjs';
import utc from 'dayjs/plugin/utc';
// Mock API calls for demonstration
const getAllShift = async () => ({
data: [
{ shift_id: 1, shift_name: 'Pagi', start_time: '08:00', end_time: '16:00', is_active: true },
{ shift_id: 2, shift_name: 'Sore', start_time: '16:00', end_time: '00:00', is_active: true },
{ shift_id: 3, shift_name: 'Malam', start_time: '00:00', end_time: '08:00', is_active: false },
],
statusCode: 200,
});
const deleteShift = async (id) => ({ statusCode: 200, message: 'Data berhasil dihapus' });
dayjs.extend(utc);
// Helper function untuk convert ISO time ke HH:mm
const formatTime = (timeValue) => {
if (!timeValue) return '-';
if (typeof timeValue === 'string' && timeValue.match(/^\d{2}:\d{2}$/)) {
return timeValue;
}
// UTC untuk menghindari timezone conversion
const time = dayjs.utc(timeValue);
return time.isValid() ? time.format('HH:mm') : '-';
};
const columns = (showPreviewModal, showEditModal, showDeleteDialog) => [
{
@@ -29,7 +33,6 @@ const columns = (showPreviewModal, showEditModal, showDeleteDialog) => [
dataIndex: 'shift_name',
key: 'shift_name',
width: '30%',
render: (text, record, index) => `${index + 1}. ${text}`,
},
{
title: 'Start Time',
@@ -37,6 +40,7 @@ const columns = (showPreviewModal, showEditModal, showDeleteDialog) => [
key: 'start_time',
width: '15%',
align: 'center',
render: (time) => formatTime(time),
},
{
title: 'End Time',
@@ -44,6 +48,7 @@ const columns = (showPreviewModal, showEditModal, showDeleteDialog) => [
key: 'end_time',
width: '15%',
align: 'center',
render: (time) => formatTime(time),
},
{
title: 'Status',
@@ -51,15 +56,9 @@ const columns = (showPreviewModal, showEditModal, showDeleteDialog) => [
key: 'is_active',
width: '15%',
align: 'center',
render: (_, { is_active }) => {
const color = is_active ? 'green' : 'red';
const text = is_active ? 'Active' : 'Inactive';
return (
<Tag color={color} key={'status'}>
{text}
</Tag>
);
},
render: (status) => (
<Tag color={status ? 'green' : 'red'}>{status ? 'Active' : 'Inactive'}</Tag>
),
},
{
title: 'Aksi',
@@ -68,9 +67,25 @@ const columns = (showPreviewModal, showEditModal, showDeleteDialog) => [
width: '25%',
render: (_, record) => (
<Space>
<Button ghost icon={<EyeOutlined />} onClick={() => showPreviewModal(record)} style={{ color: '#1890ff', borderColor: '#1890ff' }} />
<Button ghost icon={<EditOutlined />} onClick={() => showEditModal(record)} style={{ color: '#faad14', borderColor: '#faad14' }} />
<Button danger ghost icon={<DeleteOutlined />} onClick={() => showDeleteDialog(record)} />
<Button
type="text"
style={{ borderColor: '#1890ff' }}
icon={<EyeOutlined style={{ color: '#1890ff' }} />}
onClick={() => showPreviewModal(record)}
/>
<Button
type="text"
style={{ borderColor: '#faad14' }}
icon={<EditOutlined style={{ color: '#faad14' }} />}
onClick={() => showEditModal(record)}
/>
<Button
type="text"
danger
style={{ borderColor: 'red' }}
icon={<DeleteOutlined />}
onClick={() => showDeleteDialog(record)}
/>
</Space>
),
},
@@ -78,25 +93,36 @@ const columns = (showPreviewModal, showEditModal, showDeleteDialog) => [
const ListShift = memo(function ListShift(props) {
const [trigerFilter, setTrigerFilter] = useState(false);
const [formDataFilter, setFormDataFilter] = useState({ criteria: '' });
const defaultFilter = { criteria: '' };
const [formDataFilter, setFormDataFilter] = useState(defaultFilter);
const [searchValue, setSearchValue] = useState('');
const navigate = useNavigate();
useEffect(() => {
if (props.actionMode === 'list') {
doFilter();
const token = localStorage.getItem('token');
if (token) {
if (props.actionMode === 'list') {
setFormDataFilter(defaultFilter);
doFilter();
}
} else {
navigate('/signin');
}
}, [props.actionMode]);
const doFilter = () => setTrigerFilter((prev) => !prev);
const handleSearch = () => {
setFormDataFilter((prev) => ({ ...prev, criteria: searchValue }));
doFilter();
const doFilter = () => {
setTrigerFilter((prev) => !prev);
};
const handleSearch = () => {
setFormDataFilter({ criteria: searchValue });
setTrigerFilter((prev) => !prev);
};
const handleSearchClear = () => {
setSearchValue('');
setFormDataFilter((prev) => ({ ...prev, criteria: '' }));
doFilter();
setFormDataFilter({ criteria: '' });
setTrigerFilter((prev) => !prev);
};
const showPreviewModal = (param) => {
@@ -116,96 +142,111 @@ const ListShift = memo(function ListShift(props) {
const showDeleteDialog = (param) => {
NotifConfirmDialog({
icon: 'question',
title: 'Konfirmasi Hapus',
message: `Apakah Anda yakin ingin menghapus shift "${param.shift_name}"?`,
onConfirm: () => handleDelete(param),
message: 'Shift "' + param.shift_name + '" akan dihapus?',
onConfirm: () => handleDelete(param.shift_id),
onCancel: () => props.setSelectedData(null),
});
};
const handleDelete = async (param) => {
try {
const response = await deleteShift(param.shift_id);
if (response.statusCode === 200) {
NotifAlert({ icon: 'success', title: 'Berhasil', message: 'Data shift berhasil dihapus.' });
doFilter();
} else {
NotifAlert({ icon: 'error', title: 'Gagal', message: response.message || 'Gagal menghapus data.' });
}
} catch (error) {
NotifAlert({ icon: 'error', title: 'Error', message: error.message || 'Terjadi kesalahan server.' });
const handleDelete = async (shift_id) => {
const response = await deleteShift(shift_id);
if (response.statusCode === 200) {
NotifAlert({
icon: 'success',
title: 'Berhasil',
message: 'Data Shift berhasil dihapus.',
});
doFilter();
} else {
NotifAlert({
icon: 'error',
title: 'Gagal',
message: response?.message || 'Gagal Menghapus Data Shift',
});
}
};
return (
<Card>
<Row justify="space-between" align="middle" gutter={[8, 8]}>
<Col xs={24}>
<Row justify="space-between" align="middle" gutter={[8, 8]}>
<Col xs={24} sm={24} md={12} lg={12}>
<Input.Search
placeholder="Cari berdasarkan nama shift..."
value={searchValue}
onChange={(e) => {
const value = e.target.value;
setSearchValue(value);
// Auto search when clearing by backspace/delete
if (value === '') {
handleSearchClear();
}
}}
onSearch={handleSearch}
allowClear
onClear={handleSearchClear}
enterButton={
<Button
type="primary"
icon={<SearchOutlined />}
style={{ backgroundColor: '#23A55A', borderColor: '#23A55A' }}
>
Search
</Button>
}
size="large"
/>
</Col>
<Col>
<Space wrap size="small">
<ConfigProvider
theme={{
token: { colorBgContainer: '#E9F6EF' },
components: {
Button: {
defaultBg: 'white',
defaultColor: '#23A55A',
defaultBorderColor: '#23A55A',
defaultHoverColor: '#23A55A',
defaultHoverBorderColor: '#23A55A',
},
},
<React.Fragment>
<Card>
<Row>
<Col xs={24}>
<Row justify="space-between" align="middle" gutter={[8, 8]}>
<Col xs={24} sm={24} md={12} lg={12}>
<Input.Search
placeholder="Cari berdasarkan nama shift..."
value={searchValue}
onChange={(e) => {
const value = e.target.value;
setSearchValue(value);
if (value === '') {
handleSearchClear();
}
}}
>
<Button icon={<PlusOutlined />} onClick={() => showAddModal()} size="large">
Tambah Data
</Button>
</ConfigProvider>
</Space>
</Col>
</Row>
</Col>
<Col xs={24} sm={24} md={24} lg={24} xl={24} style={{ marginTop: '16px' }}>
<TableList
mobile
cardColor={'#42AAFF'}
header={'shift_name'} // Menggunakan shift_name langsung untuk judul kartu
getData={getAllShift}
queryParams={formDataFilter}
columns={columns(showPreviewModal, showEditModal, showDeleteDialog)}
triger={trigerFilter}
/>
</Col>
</Row>
</Card>
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">
<ConfigProvider
theme={{
components: {
Button: {
defaultBg: 'white',
defaultColor: '#23A55A',
defaultBorderColor: '#23A55A',
},
},
}}
>
<Button
icon={<PlusOutlined />}
onClick={() => showAddModal()}
size="large"
>
Tambah Data
</Button>
</ConfigProvider>
</Space>
</Col>
</Row>
</Col>
<Col xs={24} style={{ marginTop: '16px' }}>
<TableList
mobile
cardColor={'#42AAFF'}
header={'shift_name'}
showPreviewModal={showPreviewModal}
showEditModal={showEditModal}
showDeleteDialog={showDeleteDialog}
getData={getAllShift}
queryParams={formDataFilter}
columns={columns(showPreviewModal, showEditModal, showDeleteDialog)}
triger={trigerFilter}
/>
</Col>
</Row>
</Card>
</React.Fragment>
);
});
export default ListShift;
export default ListShift;