mirror of
https://gitlab.com/mbugroup/lti-web-client.git
synced 2026-05-24 15:25:46 +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]);
|
}, [selectedLocation, selectedKandang, projectFlockKandangLookup]);
|
||||||
|
|
||||||
const today = new Date().toISOString().split('T')[0];
|
const today = new Date().toISOString().split('T')[0];
|
||||||
const existingRecordingsUrl = useMemo(() => {
|
const existingRecordingsUrl = `${RecordingApi.basePath}`;
|
||||||
const params = new URLSearchParams({
|
|
||||||
record_date: today,
|
|
||||||
limit: '100',
|
|
||||||
});
|
|
||||||
return `${RecordingApi.basePath}?${params.toString()}`;
|
|
||||||
}, [today]);
|
|
||||||
|
|
||||||
const { data: existingRecordings } = useSWR(
|
const { data: existingRecordings } = useSWR(
|
||||||
existingRecordingsUrl,
|
existingRecordingsUrl,
|
||||||
@@ -627,6 +621,28 @@ const RecordingForm = ({ type = 'add', initialValues }: RecordingFormProps) => {
|
|||||||
return recordedIds;
|
return recordedIds;
|
||||||
}, [existingRecordings, today]);
|
}, [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 unifiedStockProducts = useMemo(() => {
|
||||||
const options: OptionType[] = [];
|
const options: OptionType[] = [];
|
||||||
if (isResponseSuccess(stockProducts) && selectedKandang) {
|
if (isResponseSuccess(stockProducts) && selectedKandang) {
|
||||||
@@ -829,6 +845,25 @@ const RecordingForm = ({ type = 'add', initialValues }: RecordingFormProps) => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
// ===== HELPER FUNCTIONS =====
|
// ===== 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 => {
|
useCallback((): OptionType | null => {
|
||||||
if (
|
if (
|
||||||
!formik.values.project_flock_kandang ||
|
!formik.values.project_flock_kandang ||
|
||||||
@@ -1945,13 +1980,30 @@ const RecordingForm = ({ type = 'add', initialValues }: RecordingFormProps) => {
|
|||||||
thousandSeparator=','
|
thousandSeparator=','
|
||||||
decimalSeparator='.'
|
decimalSeparator='.'
|
||||||
placeholder='Masukkan jumlah ayam...'
|
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={
|
isError={
|
||||||
isRepeaterInputError('body_weights', 'qty', idx)
|
isRepeaterInputError('body_weights', 'qty', idx)
|
||||||
.isError
|
.isError ||
|
||||||
|
(bw.qty
|
||||||
|
? getTotalChickQtyError(Number(bw.qty)) !== null
|
||||||
|
: false)
|
||||||
}
|
}
|
||||||
errorMessage={
|
errorMessage={
|
||||||
isRepeaterInputError('body_weights', 'qty', idx)
|
isRepeaterInputError('body_weights', 'qty', idx)
|
||||||
.errorMessage
|
.errorMessage ||
|
||||||
|
(bw.qty
|
||||||
|
? getTotalChickQtyError(Number(bw.qty)) ||
|
||||||
|
undefined
|
||||||
|
: undefined)
|
||||||
}
|
}
|
||||||
readOnly={type === 'detail'}
|
readOnly={type === 'detail'}
|
||||||
className={{
|
className={{
|
||||||
|
|||||||
Reference in New Issue
Block a user