Enhance DetailJadwalShift and ListJadwalShift components with improved employee fetching, form handling, and UI updates
This commit is contained in:
@@ -1,172 +1,173 @@
|
||||
import React, { useEffect, useState } from 'react';
|
||||
import {
|
||||
Modal,
|
||||
Input,
|
||||
Typography,
|
||||
Button,
|
||||
ConfigProvider,
|
||||
Row,
|
||||
Col
|
||||
Form,
|
||||
Select,
|
||||
Spin,
|
||||
Input
|
||||
} from 'antd';
|
||||
import { NotifOk } from '../../../components/Global/ToastNotif';
|
||||
import { NotifOk, NotifAlert } from '../../../components/Global/ToastNotif';
|
||||
import { updateJadwalShift, createJadwalShift } from '../../../api/jadwal-shift.jsx';
|
||||
|
||||
const { Text } = Typography;
|
||||
const { Option } = Select;
|
||||
|
||||
const DetailJadwalShift = (props) => {
|
||||
const [form] = Form.useForm();
|
||||
const [confirmLoading, setConfirmLoading] = useState(false);
|
||||
const [employees, setEmployees] = useState([]);
|
||||
const [loadingEmployees, setLoadingEmployees] = useState(false);
|
||||
|
||||
const defaultData = {
|
||||
id: '',
|
||||
nama_shift: '',
|
||||
jam_masuk: '',
|
||||
jam_pulang: '',
|
||||
username: '',
|
||||
nama_employee: '',
|
||||
whatsapp: ''
|
||||
};
|
||||
|
||||
const [FormData, setFormData] = useState(defaultData);
|
||||
const isReadOnly = props.actionMode === 'preview';
|
||||
|
||||
const handleCancel = () => {
|
||||
props.setSelectedData(null);
|
||||
props.setActionMode('list');
|
||||
};
|
||||
|
||||
const fetchEmployees = async () => {
|
||||
setLoadingEmployees(true);
|
||||
try {
|
||||
// Data dummy untuk dropdown karyawan
|
||||
const dummyEmployees = [
|
||||
{ employee_id: '101', nama_employee: 'Andi Pratama' },
|
||||
{ employee_id: '102', nama_employee: 'Budi Santoso' },
|
||||
{ employee_id: '103', nama_employee: 'Citra Lestari' },
|
||||
{ employee_id: '104', nama_employee: 'Dewi Anggraini' },
|
||||
{ employee_id: '105', nama_employee: 'Eko Wahyudi' },
|
||||
{ employee_id: '106', nama_employee: 'Fitriani' },
|
||||
];
|
||||
setEmployees(dummyEmployees);
|
||||
} catch (error) {
|
||||
NotifAlert({ icon: 'error', title: 'Gagal', message: 'Gagal memuat daftar karyawan.' });
|
||||
} finally {
|
||||
setLoadingEmployees(false);
|
||||
}
|
||||
};
|
||||
|
||||
const handleSave = async () => {
|
||||
setConfirmLoading(true);
|
||||
// This is a dummy save function for slicing purposes
|
||||
setTimeout(() => {
|
||||
try {
|
||||
const values = await form.validateFields();
|
||||
let payload;
|
||||
let responseMessage;
|
||||
|
||||
setConfirmLoading(true);
|
||||
if (props.actionMode === 'edit') {
|
||||
payload = { ...props.selectedData, ...values };
|
||||
// await updateJadwalShift(payload.schedule_id, payload);
|
||||
console.log("Updating schedule with payload:", payload);
|
||||
responseMessage = 'Jadwal berhasil diperbarui.';
|
||||
} else { // 'add' mode
|
||||
payload = {
|
||||
employee_id: values.employee_id,
|
||||
shift_name: props.selectedData.shift_name,
|
||||
schedule_date: new Date().toISOString().split('T')[0], // Example date
|
||||
};
|
||||
// await createJadwalShift(payload);
|
||||
console.log("Creating schedule with payload:", payload);
|
||||
responseMessage = 'User berhasil ditambahkan ke jadwal.';
|
||||
}
|
||||
await new Promise(resolve => setTimeout(resolve, 500)); // Simulasi API call
|
||||
|
||||
NotifOk({ icon: 'success', title: 'Berhasil', message: responseMessage });
|
||||
props.setActionMode('list'); // Menutup modal dan memicu refresh di parent
|
||||
} catch (error) {
|
||||
const message = error.response?.data?.message || 'Gagal memperbarui jadwal.';
|
||||
NotifAlert({ icon: 'error', title: 'Gagal', message });
|
||||
} finally {
|
||||
setConfirmLoading(false);
|
||||
NotifOk({
|
||||
icon: 'success',
|
||||
title: 'Berhasil',
|
||||
message: 'Data dummy berhasil disimpan.',
|
||||
});
|
||||
props.setActionMode('list');
|
||||
}, 1000);
|
||||
}
|
||||
};
|
||||
|
||||
useEffect(() => {
|
||||
if (props.selectedData) {
|
||||
setFormData(props.selectedData);
|
||||
} else {
|
||||
setFormData(defaultData);
|
||||
// Hanya jalankan jika modal untuk 'edit' atau 'preview' terbuka
|
||||
if (props.showModal) {
|
||||
fetchEmployees();
|
||||
if (props.actionMode === 'edit' || props.actionMode === 'preview') {
|
||||
form.setFieldsValue({
|
||||
employee_id: props.selectedData.employee_id,
|
||||
shift_name: props.selectedData.shift_name,
|
||||
});
|
||||
} else if (props.actionMode === 'add') {
|
||||
form.setFieldsValue({
|
||||
shift_name: props.selectedData.shift_name,
|
||||
employee_id: null, // Reset employee selection
|
||||
});
|
||||
}
|
||||
}
|
||||
}, [props.showModal, props.selectedData]);
|
||||
|
||||
// Dummy handler for slicing
|
||||
const handleInputChange = (e) => {
|
||||
const { name, value } = e.target;
|
||||
setFormData({ ...FormData, [name]: value });
|
||||
};
|
||||
}, [props.actionMode, props.showModal, props.selectedData, form]);
|
||||
|
||||
return (
|
||||
<Modal
|
||||
title={`${
|
||||
props.actionMode === 'add'
|
||||
? 'Tambah'
|
||||
: props.actionMode === 'preview'
|
||||
? 'Preview'
|
||||
: 'Edit'
|
||||
} Jadwal Shift`}
|
||||
title={isReadOnly ? 'Preview Jadwal' : (props.actionMode === 'edit' ? 'Edit Jadwal' : 'Tambah User')}
|
||||
open={props.showModal}
|
||||
onCancel={handleCancel}
|
||||
width={800}
|
||||
width={600}
|
||||
footer={[
|
||||
<React.Fragment key="modal-footer">
|
||||
<ConfigProvider
|
||||
theme={{
|
||||
components: {
|
||||
Button: {
|
||||
defaultBg: 'white',
|
||||
defaultColor: '#23A55A',
|
||||
defaultBorderColor: '#23A55A',
|
||||
},
|
||||
},
|
||||
}}
|
||||
>
|
||||
<Button onClick={handleCancel}>{props.readOnly ? 'Tutup' : 'Batal'}</Button>
|
||||
</ConfigProvider>
|
||||
<ConfigProvider
|
||||
theme={{
|
||||
components: {
|
||||
Button: {
|
||||
defaultBg: '#23a55a',
|
||||
defaultColor: '#FFFFFF',
|
||||
defaultBorderColor: '#23a55a',
|
||||
},
|
||||
},
|
||||
}}
|
||||
>
|
||||
{!props.readOnly && (
|
||||
<Button loading={confirmLoading} onClick={handleSave}>
|
||||
Simpan
|
||||
</Button>
|
||||
)}
|
||||
</ConfigProvider>
|
||||
<Button key="back" onClick={handleCancel}>
|
||||
{isReadOnly ? 'Tutup' : 'Batal'}
|
||||
</Button>
|
||||
{!isReadOnly && (
|
||||
<Button key="submit" type="primary" loading={confirmLoading} onClick={handleSave} style={{ backgroundColor: '#23A55A' }}>
|
||||
Simpan
|
||||
</Button>
|
||||
)}
|
||||
</React.Fragment>,
|
||||
]}
|
||||
>
|
||||
{FormData && (
|
||||
<div>
|
||||
<Row gutter={[16, 16]}>
|
||||
<Col span={12}>
|
||||
<Text strong>Nama Karyawan</Text>
|
||||
<Input
|
||||
name="nama_employee"
|
||||
value={FormData.nama_employee}
|
||||
onChange={handleInputChange}
|
||||
readOnly={props.readOnly}
|
||||
/>
|
||||
</Col>
|
||||
<Col span={12}>
|
||||
<Text strong>Username</Text>
|
||||
<Input
|
||||
name="username"
|
||||
value={FormData.username}
|
||||
onChange={handleInputChange}
|
||||
readOnly={props.readOnly}
|
||||
/>
|
||||
</Col>
|
||||
<Col span={12}>
|
||||
<Text strong>Nama Shift</Text>
|
||||
<Input
|
||||
name="nama_shift"
|
||||
value={FormData.nama_shift}
|
||||
onChange={handleInputChange}
|
||||
readOnly={props.readOnly}
|
||||
/>
|
||||
</Col>
|
||||
<Col span={12}>
|
||||
<Text strong>Whatsapp</Text>
|
||||
<Input
|
||||
name="whatsapp"
|
||||
value={FormData.whatsapp}
|
||||
onChange={handleInputChange}
|
||||
readOnly={props.readOnly}
|
||||
/>
|
||||
</Col>
|
||||
<Col span={12}>
|
||||
<Text strong>Jam Masuk</Text>
|
||||
<Input
|
||||
name="jam_masuk"
|
||||
value={FormData.jam_masuk}
|
||||
onChange={handleInputChange}
|
||||
readOnly={props.readOnly}
|
||||
/>
|
||||
</Col>
|
||||
<Col span={12}>
|
||||
<Text strong>Jam Pulang</Text>
|
||||
<Input
|
||||
name="jam_pulang"
|
||||
value={FormData.jam_pulang}
|
||||
onChange={handleInputChange}
|
||||
readOnly={props.readOnly}
|
||||
/>
|
||||
</Col>
|
||||
</Row>
|
||||
</div>
|
||||
)}
|
||||
<Spin spinning={loadingEmployees} tip="Memuat data...">
|
||||
<Form form={form} layout="vertical" name="shift_form">
|
||||
{props.actionMode === 'add' ? (
|
||||
<>
|
||||
<Form.Item
|
||||
name="shift_name"
|
||||
label="Shift"
|
||||
>
|
||||
<Input disabled />
|
||||
</Form.Item>
|
||||
<Form.Item
|
||||
name="employee_id"
|
||||
label="Nama Karyawan"
|
||||
rules={[{ required: true, message: 'Nama karyawan wajib dipilih!' }]}
|
||||
>
|
||||
<Select
|
||||
placeholder="Pilih karyawan"
|
||||
showSearch
|
||||
optionFilterProp="children"
|
||||
>
|
||||
{employees.map(emp => (
|
||||
<Option key={emp.employee_id} value={emp.employee_id}>{emp.nama_employee}</Option>
|
||||
))}
|
||||
</Select>
|
||||
</Form.Item>
|
||||
</>
|
||||
) : (
|
||||
<>
|
||||
<Form.Item
|
||||
name="employee_id"
|
||||
label="Nama Karyawan"
|
||||
rules={[{ required: true, message: 'Nama karyawan wajib dipilih!' }]}
|
||||
>
|
||||
<Select placeholder="Pilih karyawan" disabled={isReadOnly} showSearch optionFilterProp="children">
|
||||
{employees.map(emp => (
|
||||
<Option key={emp.employee_id} value={emp.employee_id}>{emp.nama_employee}</Option>
|
||||
))}
|
||||
</Select>
|
||||
</Form.Item>
|
||||
<Form.Item name="shift_name" label="Shift" rules={[{ required: true, message: 'Shift wajib dipilih!' }]}>
|
||||
<Select placeholder="Pilih shift" disabled={isReadOnly}>
|
||||
<Option value="PAGI">PAGI</Option>
|
||||
<Option value="SIANG">SIANG</Option>
|
||||
<Option value="MALAM">MALAM</Option>
|
||||
</Select>
|
||||
</Form.Item>
|
||||
</>
|
||||
)}
|
||||
</Form>
|
||||
</Spin>
|
||||
</Modal>
|
||||
);
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user