mirror of
https://gitlab.com/mbugroup/lti-web-client.git
synced 2026-05-20 21:41:57 +00:00
feat(FE-170,174): add validation for total chick quantity in RecordingForm
This commit is contained in:
@@ -372,13 +372,7 @@ const RecordingForm = ({ type = 'add', initialValues }: RecordingFormProps) => {
|
||||
}, [selectedLocation, selectedKandang, projectFlockKandangLookup]);
|
||||
|
||||
const today = new Date().toISOString().split('T')[0];
|
||||
const existingRecordingsUrl = useMemo(() => {
|
||||
const params = new URLSearchParams({
|
||||
record_date: today,
|
||||
limit: '100',
|
||||
});
|
||||
return `${RecordingApi.basePath}?${params.toString()}`;
|
||||
}, [today]);
|
||||
const existingRecordingsUrl = `${RecordingApi.basePath}`;
|
||||
|
||||
const { data: existingRecordings } = useSWR(
|
||||
existingRecordingsUrl,
|
||||
@@ -627,6 +621,28 @@ const RecordingForm = ({ type = 'add', initialValues }: RecordingFormProps) => {
|
||||
return recordedIds;
|
||||
}, [existingRecordings, today]);
|
||||
|
||||
const getLatestTotalChickQty = useCallback(
|
||||
(projectFlockKandangId: number) => {
|
||||
if (!isResponseSuccess(existingRecordings)) return null;
|
||||
|
||||
const projectFlockRecordings = existingRecordings.data.filter(
|
||||
(recording) =>
|
||||
recording.project_flock_kandang_id === projectFlockKandangId
|
||||
);
|
||||
|
||||
if (projectFlockRecordings.length === 0) return null;
|
||||
|
||||
projectFlockRecordings.sort(
|
||||
(a, b) =>
|
||||
new Date(b.record_datetime).getTime() -
|
||||
new Date(a.record_datetime).getTime()
|
||||
);
|
||||
|
||||
return projectFlockRecordings[0].total_chick_qty;
|
||||
},
|
||||
[existingRecordings]
|
||||
);
|
||||
|
||||
const unifiedStockProducts = useMemo(() => {
|
||||
const options: OptionType[] = [];
|
||||
if (isResponseSuccess(stockProducts) && selectedKandang) {
|
||||
@@ -829,6 +845,25 @@ const RecordingForm = ({ type = 'add', initialValues }: RecordingFormProps) => {
|
||||
});
|
||||
|
||||
// ===== HELPER FUNCTIONS =====
|
||||
const getTotalChickQtyError = useCallback(
|
||||
(qty: number) => {
|
||||
if (type === 'detail') return null;
|
||||
if (!formik.values.project_flock_kandang_id) return null;
|
||||
|
||||
const totalChickQty = getLatestTotalChickQty(
|
||||
formik.values.project_flock_kandang_id
|
||||
);
|
||||
if (!totalChickQty) return null;
|
||||
|
||||
if (qty > totalChickQty) {
|
||||
return `Jumlah ayam tidak boleh melebihi total ayam tersedia! Maksimal: ${totalChickQty.toLocaleString('en-US')}`;
|
||||
}
|
||||
|
||||
return null;
|
||||
},
|
||||
[formik.values.project_flock_kandang_id, getLatestTotalChickQty, type]
|
||||
);
|
||||
|
||||
useCallback((): OptionType | null => {
|
||||
if (
|
||||
!formik.values.project_flock_kandang ||
|
||||
@@ -1945,13 +1980,30 @@ const RecordingForm = ({ type = 'add', initialValues }: RecordingFormProps) => {
|
||||
thousandSeparator=','
|
||||
decimalSeparator='.'
|
||||
placeholder='Masukkan jumlah ayam...'
|
||||
bottomLabel={
|
||||
formik.values.project_flock_kandang_id &&
|
||||
type === 'add'
|
||||
? `Total Ayam: ${
|
||||
getLatestTotalChickQty(
|
||||
formik.values.project_flock_kandang_id
|
||||
) ?? 'N/A'
|
||||
}`
|
||||
: undefined
|
||||
}
|
||||
isError={
|
||||
isRepeaterInputError('body_weights', 'qty', idx)
|
||||
.isError
|
||||
.isError ||
|
||||
(bw.qty
|
||||
? getTotalChickQtyError(Number(bw.qty)) !== null
|
||||
: false)
|
||||
}
|
||||
errorMessage={
|
||||
isRepeaterInputError('body_weights', 'qty', idx)
|
||||
.errorMessage
|
||||
.errorMessage ||
|
||||
(bw.qty
|
||||
? getTotalChickQtyError(Number(bw.qty)) ||
|
||||
undefined
|
||||
: undefined)
|
||||
}
|
||||
readOnly={type === 'detail'}
|
||||
className={{
|
||||
|
||||
Reference in New Issue
Block a user