feat: add shift management functionality with CRUD operations and UI components

This commit is contained in:
2025-10-13 02:25:16 +07:00
parent af6c6de301
commit 54290baaac
10 changed files with 864 additions and 1 deletions

View File

@@ -0,0 +1,28 @@
import { useState } from 'react';
import ListJadwalShift from './component/ListJadwalShift';
import DetailJadwalShift from './component/DetailJadwalShift';
const IndexJadwalShift = () => {
const [actionMode, setActionMode] = useState('list');
const [selectedData, setSelectedData] = useState(null);
return (
<>
{actionMode === 'list' && (
<ListJadwalShift
setActionMode={setActionMode}
setSelectedData={setSelectedData}
/>
)}
{(actionMode === 'add' || actionMode === 'edit') && (
<DetailJadwalShift
actionMode={actionMode}
selectedData={selectedData}
setActionMode={setActionMode}
/>
)}
</>
);
};
export default IndexJadwalShift;

View File

@@ -0,0 +1,210 @@
import { useEffect, useState } from 'react';
import { Modal, Select, Divider, Typography, Button, ConfigProvider } from 'antd';
import { NotifAlert, NotifOk } from '../../../components/Global/ToastNotif';
import { createJadwalShift, updateJadwalShift } from '../../../api/jadwal-shift';
import { getAllShift } from '../../../api/master-shift';
import { getAllUser } from '../../../api/user';
const { Text } = Typography;
const { Option } = Select;
const DetailJadwalShift = (props) => {
const [confirmLoading, setConfirmLoading] = useState(false);
const [shifts, setShifts] = useState([]);
const [users, setUsers] = useState([]);
const defaultData = {
id: '',
id_shift: null,
id_user: null,
};
const [FormData, setFormData] = useState(defaultData);
const fetchShifts = async () => {
const response = await getAllShift(new URLSearchParams());
setShifts(response.data.data);
};
const fetchUsers = async () => {
const response = await getAllUser(new URLSearchParams("limit=1000")); // Fetch all users
setUsers(response.data.data);
};
useEffect(() => {
fetchShifts();
fetchUsers();
}, []);
const handleCancel = () => {
props.setSelectedData(null);
props.setActionMode('list');
};
const handleSave = async () => {
setConfirmLoading(true);
if (!FormData.id_shift) {
NotifOk({
icon: 'warning',
title: 'Peringatan',
message: 'Kolom Shift Tidak Boleh Kosong',
});
setConfirmLoading(false);
return;
}
if (!FormData.id_user) {
NotifOk({
icon: 'warning',
title: 'Peringatan',
message: 'Kolom User Tidak Boleh Kosong',
});
setConfirmLoading(false);
return;
}
const payload = {
id_shift: FormData.id_shift,
id_user: FormData.id_user,
};
try {
const response = FormData.id
? await updateJadwalShift(FormData.id, payload)
: await createJadwalShift(payload);
if (response && (response.statusCode === 200 || response.statusCode === 201)) {
NotifOk({
icon: 'success',
title: 'Berhasil',
message: `Data Jadwal Shift berhasil ${FormData.id ? 'diubah' : 'ditambahkan'}.`,
});
props.setActionMode('list');
} else {
NotifAlert({
icon: 'error',
title: 'Gagal',
message: response?.message || 'Terjadi kesalahan saat menyimpan data.',
});
}
} catch (error) {
console.error('Save Jadwal Shift Error:', error);
NotifAlert({
icon: 'error',
title: 'Error',
message: error.message || 'Terjadi kesalahan pada server. Coba lagi nanti.',
});
}
setConfirmLoading(false);
};
const handleSelectChange = (name, value) => {
setFormData({
...FormData,
[name]: value,
});
};
useEffect(() => {
if (props.selectedData) {
setFormData(props.selectedData);
} else {
setFormData(defaultData);
}
}, [props.actionMode, props.selectedData]);
return (
<Modal
title={`${
props.actionMode === 'add'
? 'Tambah'
: 'Edit'
} Jadwal Shift`}
open={props.actionMode !== 'list'}
onCancel={handleCancel}
footer={[
<>
<ConfigProvider
theme={{
token: { colorBgContainer: '#E9F6EF' },
components: {
Button: {
defaultBg: 'white',
defaultColor: '#23A55A',
defaultBorderColor: '#23A55A',
defaultHoverColor: '#23A55A',
defaultHoverBorderColor: '#23A55A',
},
},
}}
>
<Button onClick={handleCancel}>Batal</Button>
</ConfigProvider>
<ConfigProvider
theme={{
token: {
colorBgContainer: '#209652',
},
components: {
Button: {
defaultBg: '#23a55a',
defaultColor: '#FFFFFF',
defaultBorderColor: '#23a55a',
defaultHoverColor: '#FFFFFF',
defaultHoverBorderColor: '#23a55a',
},
},
}}
>
<Button loading={confirmLoading} onClick={handleSave}>
Simpan
</Button>
</ConfigProvider>
</>,
]}
>
{FormData && (
<div>
<div style={{ marginBottom: 12 }}>
<Text strong>Shift</Text>
<Text style={{ color: 'red' }}> *</Text>
<Select
style={{ width: '100%' }}
placeholder="Pilih Shift"
onChange={(value) => handleSelectChange('id_shift', value)}
value={FormData.id_shift}
>
{shifts.map(shift => (
<Option key={shift.id} value={shift.id}>{shift.nama_shift} ({shift.jam_shift})</Option>
))}
</Select>
</div>
<div style={{ marginBottom: 12 }}>
<Text strong>User</Text>
<Text style={{ color: 'red' }}> *</Text>
<Select
style={{ width: '100%' }}
placeholder="Pilih User"
onChange={(value) => handleSelectChange('id_user', value)}
value={FormData.id_user}
showSearch
optionFilterProp="children"
filterOption={(input, option) =>
option.children.toLowerCase().indexOf(input.toLowerCase()) >= 0
}
>
{users.map(user => (
<Option key={user.id} value={user.id}>{user.username} ({user.nama_employee})</Option>
))}
</Select>
</div>
</div>
)}
</Modal>
);
};
export default DetailJadwalShift;

View File

@@ -0,0 +1,118 @@
import { useEffect, useState } from 'react';
import { Button, Table, Space, Popconfirm } from 'antd';
import { getAllJadwalShift, deleteJadwalShift } from '../../../api/jadwal-shift';
import { NotifAlert, NotifOk } from '../../../components/Global/ToastNotif';
const ListJadwalShift = (props) => {
const [data, setData] = useState([]);
const [loading, setLoading] = useState(false);
const fetchData = async () => {
setLoading(true);
try {
const response = await getAllJadwalShift(new URLSearchParams());
if (response.data && response.data.data) {
setData(response.data.data);
} else {
setData([]);
}
} catch (error) {
console.error("Failed to fetch shift schedule data:", error);
setData([]); // Set data to empty array on error
} finally {
setLoading(false);
}
};
const handleAdd = () => {
props.setSelectedData(null);
props.setActionMode('add');
};
const handleEdit = (record) => {
props.setSelectedData(record);
props.setActionMode('edit');
};
const handleDelete = async (id) => {
try {
const response = await deleteJadwalShift(id);
if (response.statusCode === 200) {
NotifOk({ icon: 'success', title: 'Berhasil', message: 'Data jadwal shift berhasil dihapus' });
fetchData();
} else {
NotifAlert({ icon: 'error', title: 'Gagal', message: response.message });
}
} catch (error) {
NotifAlert({ icon: 'error', title: 'Error', message: error.message });
}
};
useEffect(() => {
if(props.actionMode === 'list') {
fetchData();
}
}, [props.actionMode]);
const columns = [
{
title: 'Nama Shift',
dataIndex: 'nama_shift',
key: 'nama_shift',
},
{
title: 'Jam Shift',
dataIndex: 'jam_shift',
key: 'jam_shift',
},
{
title: 'Username',
dataIndex: 'username',
key: 'username',
},
{
title: 'Nama Employee',
dataIndex: 'nama_employee',
key: 'nama_employee',
},
{
title: 'WhatsApp',
dataIndex: 'whatsapp',
key: 'whatsapp',
},
{
title: 'Aksi',
key: 'action',
render: (text, record) => (
<Space size="middle">
<Button type="primary" onClick={() => handleEdit(record)}>Edit</Button>
<Popconfirm
title="Hapus data jadwal shift?"
description="Apakah anda yakin untuk menghapus data jadwal shift ini?"
onConfirm={() => handleDelete(record.id)}
okText="Yes"
cancelText="No"
>
<Button danger>Delete</Button>
</Popconfirm>
</Space>
),
},
];
return (
<div>
<Button type="primary" onClick={handleAdd} style={{ marginBottom: 16 }}>
Tambah Jadwal Shift
</Button>
<Table
columns={columns}
dataSource={data}
loading={loading}
rowKey="id"
/>
</div>
);
};
export default ListJadwalShift;