lavoce #7

Merged
bragaz_rexita merged 11 commits from lavoce into main 2025-10-24 05:43:32 +00:00
Showing only changes of commit bddd249e07 - Show all commits

View File

@@ -1,13 +1,16 @@
import React, { useEffect, useState } from 'react'; import React, { useEffect, useState } from 'react';
import { Modal, Input, Typography, Switch, Button, ConfigProvider, Divider } from 'antd'; import { Modal, Input, Typography, Switch, Button, ConfigProvider, Divider, Select } from 'antd';
import { NotifOk } from '../../../../components/Global/ToastNotif'; import { NotifOk } from '../../../../components/Global/ToastNotif';
import { createUnit, updateUnit } from '../../../../api/master-unit'; import { createUnit, updateUnit } from '../../../../api/master-unit';
import { getAllTag } from '../../../../api/master-tag'; // Import API untuk Tag
import { validateRun } from '../../../../Utils/validate'; import { validateRun } from '../../../../Utils/validate';
const { Text } = Typography; const { Text } = Typography;
const DetailUnit = (props) => { const DetailUnit = (props) => {
const [confirmLoading, setConfirmLoading] = useState(false); const [confirmLoading, setConfirmLoading] = useState(false);
const [tagList, setTagList] = useState([]);
const [loadingTags, setLoadingTags] = useState(false);
const defaultData = { const defaultData = {
unit_id: '', unit_id: '',
@@ -15,10 +18,28 @@ const DetailUnit = (props) => {
unit_name: '', unit_name: '',
unit_description: '', unit_description: '',
is_active: true, is_active: true,
tag_id: null, // Tambahkan tag_id
}; };
const [formData, setFormData] = useState(defaultData); const [formData, setFormData] = useState(defaultData);
// Fungsi untuk mengambil data Tag
const loadTags = async () => {
setLoadingTags(true);
try {
const params = new URLSearchParams({ limit: 1000, criteria: '' });
const response = await getAllTag(params);
if (response && response.data) {
const activeTags = response.data.filter((tag) => tag.is_active === true);
setTagList(activeTags);
}
} catch (error) {
console.error('Error loading tags:', error);
} finally {
setLoadingTags(false);
}
};
const handleCancel = () => { const handleCancel = () => {
props.setSelectedData(null); props.setSelectedData(null);
props.setActionMode('list'); props.setActionMode('list');
@@ -27,8 +48,10 @@ const DetailUnit = (props) => {
const handleSave = async () => { const handleSave = async () => {
setConfirmLoading(true); setConfirmLoading(true);
// Daftar aturan validasi const validationRules = [
const validationRules = [{ field: 'unit_name', label: 'Unit Name', required: true }]; { field: 'unit_name', label: 'Unit Name', required: true },
{ field: 'tag_id', label: 'Tag', required: true }, // Tambah validasi untuk tag_id
];
if ( if (
validateRun(formData, validationRules, (errorMessages) => { validateRun(formData, validationRules, (errorMessages) => {
@@ -39,14 +62,16 @@ const DetailUnit = (props) => {
}); });
setConfirmLoading(false); setConfirmLoading(false);
}) })
) ) {
return; return;
}
try { try {
const payload = { const payload = {
is_active: formData.is_active,
unit_name: formData.unit_name, unit_name: formData.unit_name,
unit_description: formData.unit_description, unit_description: formData.unit_description,
is_active: formData.is_active,
tag_id: formData.tag_id, // Tambahkan tag_id ke payload
}; };
const response = const response =
@@ -90,6 +115,13 @@ const DetailUnit = (props) => {
}); });
}; };
const handleSelectChange = (name, value) => {
setFormData({
...formData,
[name]: value,
});
};
const handleStatusToggle = (checked) => { const handleStatusToggle = (checked) => {
setFormData({ setFormData({
...formData, ...formData,
@@ -98,6 +130,10 @@ const DetailUnit = (props) => {
}; };
useEffect(() => { useEffect(() => {
if (props.showModal) {
loadTags(); // Panggil fungsi loadTags saat modal muncul
}
if (props.selectedData) { if (props.selectedData) {
setFormData(props.selectedData); setFormData(props.selectedData);
} else { } else {
@@ -116,6 +152,7 @@ const DetailUnit = (props) => {
} Unit`} } Unit`}
open={props.showModal} open={props.showModal}
onCancel={handleCancel} onCancel={handleCancel}
width={600}
footer={[ footer={[
<React.Fragment key="modal-footer"> <React.Fragment key="modal-footer">
<ConfigProvider <ConfigProvider
@@ -175,13 +212,12 @@ const DetailUnit = (props) => {
</div> </div>
<Divider style={{ margin: '12px 0' }} /> <Divider style={{ margin: '12px 0' }} />
{/* Unit Code - Auto Increment & Read Only */}
<div style={{ marginBottom: 12 }}> <div style={{ marginBottom: 12 }}>
<Text strong>Unit Code</Text> <Text strong>Unit Code</Text>
<Input <Input
name="unit_code" name="unit_code"
value={formData.unit_code || ''} value={formData.unit_code || ''}
placeholder={'Unit Code Auto Fill'} placeholder="Dibuat otomatis oleh sistem"
disabled disabled
style={{ style={{
backgroundColor: '#f5f5f5', backgroundColor: '#f5f5f5',
@@ -191,6 +227,33 @@ const DetailUnit = (props) => {
/> />
</div> </div>
<div style={{ marginBottom: 12 }}>
<Text strong>Tag</Text>
<Text style={{ color: 'red' }}> *</Text>
<Select
style={{ width: '100%' }}
placeholder="Pilih Tag"
value={formData.tag_id || undefined}
onChange={(value) => handleSelectChange('tag_id', value)}
disabled={props.readOnly}
loading={loadingTags}
showSearch
allowClear
optionFilterProp="children"
filterOption={(input, option) => {
const text = option.children;
if (!text) return false;
return text.toLowerCase().includes(input.toLowerCase());
}}
>
{tagList.map((tag) => (
<Select.Option key={tag.tag_id} value={tag.tag_id}>
{`${tag.tag_code || ''} - ${tag.tag_name || ''}`}
</Select.Option>
))}
</Select>
</div>
<div style={{ marginBottom: 12 }}> <div style={{ marginBottom: 12 }}>
<Text strong>Unit Name</Text> <Text strong>Unit Name</Text>
<Text style={{ color: 'red' }}> *</Text> <Text style={{ color: 'red' }}> *</Text>
@@ -221,4 +284,4 @@ const DetailUnit = (props) => {
); );
}; };
export default DetailUnit; export default DetailUnit;