feat: implement field auto-incrementing code
This commit is contained in:
@@ -9,7 +9,7 @@ import {
|
||||
Divider,
|
||||
} from 'antd';
|
||||
import { NotifAlert, NotifOk } from '../../../../components/Global/ToastNotif';
|
||||
import { createPlantSection, updatePlantSection } from '../../../../api/master-plant-section';
|
||||
import { createPlantSection, updatePlantSection, getAllPlantSection } from '../../../../api/master-plant-section';
|
||||
|
||||
const { Text } = Typography;
|
||||
|
||||
@@ -24,6 +24,7 @@ const DetailPlantSection = (props) => {
|
||||
};
|
||||
|
||||
const [FormData, setFormData] = useState(defaultData);
|
||||
const [nextPlantSectionCode, setNextPlantSectionCode] = useState('Auto-fill');
|
||||
|
||||
const handleInputChange = (e) => {
|
||||
const { name, value } = e.target;
|
||||
@@ -103,14 +104,56 @@ const DetailPlantSection = (props) => {
|
||||
});
|
||||
};
|
||||
|
||||
const generateNextPlantSectionCode = async () => {
|
||||
try {
|
||||
const params = new URLSearchParams({ limit: 10000 });
|
||||
const response = await getAllPlantSection(params);
|
||||
|
||||
if (response && response.data && response.data.data) {
|
||||
const sections = response.data.data;
|
||||
|
||||
if (sections.length === 0) {
|
||||
setNextPlantSectionCode('SUB001');
|
||||
return;
|
||||
}
|
||||
|
||||
// Extract numeric part from plant section codes and find the maximum
|
||||
const sectionNumbers = sections
|
||||
.map((section) => {
|
||||
const match = section.sub_section_code?.match(/sub(\d+)/i);
|
||||
return match ? parseInt(match[1], 10) : 0;
|
||||
})
|
||||
.filter((num) => !isNaN(num));
|
||||
|
||||
const maxNumber = sectionNumbers.length > 0 ? Math.max(...sectionNumbers) : 0;
|
||||
const nextNumber = maxNumber + 1;
|
||||
|
||||
// Format with leading zeros (SUB001, SUB002, etc.)
|
||||
const nextCode = `SUB${String(nextNumber).padStart(3, '0')}`;
|
||||
setNextPlantSectionCode(nextCode);
|
||||
} else {
|
||||
setNextPlantSectionCode('SUB001');
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Error generating next plant section code:', error);
|
||||
setNextPlantSectionCode('Auto-fill');
|
||||
}
|
||||
};
|
||||
|
||||
useEffect(() => {
|
||||
if (props.showModal) {
|
||||
// Generate next plant section code only for add mode
|
||||
if (props.actionMode === 'add' && !props.selectedData) {
|
||||
generateNextPlantSectionCode();
|
||||
}
|
||||
}
|
||||
|
||||
if (props.selectedData) {
|
||||
setFormData(props.selectedData);
|
||||
} else {
|
||||
setFormData(defaultData);
|
||||
}
|
||||
}, [props.showModal, props.selectedData]);
|
||||
}, [props.showModal, props.selectedData, props.actionMode]);
|
||||
|
||||
return (
|
||||
<Modal
|
||||
@@ -184,16 +227,21 @@ const DetailPlantSection = (props) => {
|
||||
</div>
|
||||
<Divider style={{ margin: '12px 0' }} />
|
||||
|
||||
{props.actionMode !== 'add' && (
|
||||
<div style={{ marginBottom: 12 }}>
|
||||
<Text strong>Plant Section Code</Text>
|
||||
<Input
|
||||
name="sub_section_code"
|
||||
value={FormData.sub_section_code}
|
||||
disabled
|
||||
/>
|
||||
</div>
|
||||
)}
|
||||
{/* Plant Section Code - Auto Increment & Read Only */}
|
||||
<div style={{ marginBottom: 12 }}>
|
||||
<Text strong>Plant Section Code</Text>
|
||||
<Input
|
||||
name="sub_section_code"
|
||||
value={FormData.sub_section_code || nextPlantSectionCode}
|
||||
placeholder={nextPlantSectionCode}
|
||||
disabled
|
||||
style={{
|
||||
backgroundColor: '#f5f5f5',
|
||||
cursor: 'not-allowed',
|
||||
color: FormData.sub_section_code ? '#000000' : '#bfbfbf'
|
||||
}}
|
||||
/>
|
||||
</div>
|
||||
|
||||
<div style={{ marginBottom: 12 }}>
|
||||
<Text strong>Plant Sub Section Name</Text>
|
||||
|
||||
Reference in New Issue
Block a user