feat(FE-170,175): update RecordingForm to include selectedKandang in product URL generation and options filtering

This commit is contained in:
rstubryan
2025-11-10 08:36:55 +07:00
parent 4f88f26b71
commit 3ac0672f7e
@@ -322,7 +322,7 @@ const RecordingForm = ({ type = 'add', initialValues }: RecordingFormProps) => {
: undefined; : undefined;
const stockProductsUrl = useMemo(() => { const stockProductsUrl = useMemo(() => {
if (!selectedLocation) return null; if (!selectedLocation || !selectedKandang) return null;
const params = new URLSearchParams({ const params = new URLSearchParams({
flags: 'PAKAN,OVK', flags: 'PAKAN,OVK',
search: '', search: '',
@@ -330,17 +330,17 @@ const RecordingForm = ({ type = 'add', initialValues }: RecordingFormProps) => {
location_id: selectedLocation.value.toString(), location_id: selectedLocation.value.toString(),
}); });
return `${ProductWarehouseApi.basePath}?${params.toString()}`; return `${ProductWarehouseApi.basePath}?${params.toString()}`;
}, [selectedLocation]); }, [selectedLocation, selectedKandang]);
const depletionProductsUrl = useMemo(() => { const depletionProductsUrl = useMemo(() => {
if (!selectedLocation) return null; if (!selectedLocation || !selectedKandang) return null;
const params = new URLSearchParams({ const params = new URLSearchParams({
search: '', search: '',
limit: '100', limit: '100',
location_id: selectedLocation.value.toString(), location_id: selectedLocation.value.toString(),
}); });
return `${ProductWarehouseApi.basePath}?${params.toString()}`; return `${ProductWarehouseApi.basePath}?${params.toString()}`;
}, [selectedLocation]); }, [selectedLocation, selectedKandang]);
const today = new Date().toISOString().split('T')[0]; const today = new Date().toISOString().split('T')[0];
const existingRecordingsUrl = useMemo(() => { const existingRecordingsUrl = useMemo(() => {
@@ -361,12 +361,14 @@ const RecordingForm = ({ type = 'add', initialValues }: RecordingFormProps) => {
useSWR(depletionProductsUrl, ProductWarehouseApi.getAllFetcher); useSWR(depletionProductsUrl, ProductWarehouseApi.getAllFetcher);
const eggProductsUrl = useMemo(() => { const eggProductsUrl = useMemo(() => {
if (!selectedLocation || !selectedKandang) return null;
const params = new URLSearchParams({ const params = new URLSearchParams({
search: 'telur', search: 'telur',
limit: '100', limit: '100',
location_id: selectedLocation.value.toString(),
}); });
return `${ProductWarehouseApi.basePath}?${params.toString()}`; return `${ProductWarehouseApi.basePath}?${params.toString()}`;
}, [selectedLocation]); }, [selectedLocation, selectedKandang]);
const { data: eggProductsData, isLoading: isLoadingEggProducts } = useSWR( const { data: eggProductsData, isLoading: isLoadingEggProducts } = useSWR(
eggProductsUrl, eggProductsUrl,
@@ -482,24 +484,17 @@ const RecordingForm = ({ type = 'add', initialValues }: RecordingFormProps) => {
const unifiedStockProducts = useMemo(() => { const unifiedStockProducts = useMemo(() => {
const options: OptionType[] = []; const options: OptionType[] = [];
if (isResponseSuccess(stockProducts)) { if (isResponseSuccess(stockProducts) && selectedKandang) {
stockProducts.data.forEach((product) => { stockProducts.data.forEach((product) => {
const warehouse = product.warehouse; const warehouse = product.warehouse;
product.quantity.toLocaleString('en-US');
const hasPakanFlag = product.product.flags?.includes('PAKAN'); const hasPakanFlag = product.product.flags?.includes('PAKAN');
const hasOvkFlag = product.product.flags?.includes('OVK'); const hasOvkFlag = product.product.flags?.includes('OVK');
if (hasPakanFlag) { // Only include products that are in the same location as the selected kandang
if (hasPakanFlag || hasOvkFlag) {
options.push({ options.push({
value: product.id, value: product.id,
label: `${product.product.name} - ${warehouse?.name || ''}`, label: `${product.product.name} - ${warehouse?.name || 'Unknown Warehouse'} (Stok: ${product.quantity.toLocaleString('en-US')})`,
});
}
if (hasOvkFlag) {
options.push({
value: product.id,
label: `${product.product.name} - ${warehouse?.name || ''}`,
}); });
} }
}); });
@@ -519,7 +514,7 @@ const RecordingForm = ({ type = 'add', initialValues }: RecordingFormProps) => {
if (!existingOption) { if (!existingOption) {
options.push({ options.push({
value: stock.product_warehouse_id, value: stock.product_warehouse_id,
label: `${stock.product_warehouse.product.name} - ${stock.product_warehouse.product?.sku || ''}`, label: `${stock.product_warehouse.product.name} - ${stock.product_warehouse.warehouse?.name || 'Unknown'} (Stok: ${stock.product_warehouse.quantity?.toLocaleString('en-US') || 0})`,
}); });
} }
} }
@@ -527,14 +522,15 @@ const RecordingForm = ({ type = 'add', initialValues }: RecordingFormProps) => {
} }
return options; return options;
}, [stockProducts, initialValues, type]); }, [stockProducts, initialValues, type, selectedKandang]);
const depletionProducts = useMemo(() => { const depletionProducts = useMemo(() => {
const options: OptionType[] = []; const options: OptionType[] = [];
if (isResponseSuccess(depletionProductsData)) { if (isResponseSuccess(depletionProductsData) && selectedKandang) {
depletionProductsData.data.forEach((product) => { depletionProductsData.data.forEach((product) => {
const productName = product.product.name; const productName = product.product.name;
// Filter for depletion-related products (culling, mati, afkir)
if ( if (
productName.toLowerCase().includes('culling') || productName.toLowerCase().includes('culling') ||
productName.toLowerCase().includes('mati') || productName.toLowerCase().includes('mati') ||
@@ -542,7 +538,7 @@ const RecordingForm = ({ type = 'add', initialValues }: RecordingFormProps) => {
) { ) {
options.push({ options.push({
value: product.id, value: product.id,
label: product.product.name, label: `${product.product.name} - ${product.warehouse?.name || 'Unknown Warehouse'}`,
}); });
} }
}); });
@@ -560,7 +556,7 @@ const RecordingForm = ({ type = 'add', initialValues }: RecordingFormProps) => {
if (!existingOption) { if (!existingOption) {
options.push({ options.push({
value: depletion.product_warehouse_id, value: depletion.product_warehouse_id,
label: depletion.product_warehouse.product.name, label: `${depletion.product_warehouse.product.name} - ${depletion.product_warehouse.warehouse?.name || 'Unknown'}`,
}); });
} }
} }
@@ -568,14 +564,15 @@ const RecordingForm = ({ type = 'add', initialValues }: RecordingFormProps) => {
} }
return options; return options;
}, [depletionProductsData, initialValues, type]); }, [depletionProductsData, initialValues, type, selectedKandang]);
const eggProducts = useMemo(() => { const eggProducts = useMemo(() => {
const options: OptionType[] = []; const options: OptionType[] = [];
if (isResponseSuccess(eggProductsData)) { if (isResponseSuccess(eggProductsData) && selectedKandang) {
eggProductsData.data.forEach((product) => { eggProductsData.data.forEach((product) => {
const productName = product.product.name; const productName = product.product.name;
// Filter for egg-related products
if ( if (
productName.toLowerCase().includes('telur') || productName.toLowerCase().includes('telur') ||
productName.toLowerCase().includes('egg') || productName.toLowerCase().includes('egg') ||
@@ -585,7 +582,7 @@ const RecordingForm = ({ type = 'add', initialValues }: RecordingFormProps) => {
) { ) {
options.push({ options.push({
value: product.id, value: product.id,
label: product.product.name, label: `${product.product.name} - ${product.warehouse?.name || 'Unknown Warehouse'} (Stok: ${product.quantity.toLocaleString('en-US')})`,
}); });
} }
}); });
@@ -600,7 +597,7 @@ const RecordingForm = ({ type = 'add', initialValues }: RecordingFormProps) => {
if (!existingOption) { if (!existingOption) {
options.push({ options.push({
value: egg.product_warehouse_id, value: egg.product_warehouse_id,
label: egg.product_warehouse.product.name, label: `${egg.product_warehouse.product.name} - ${egg.product_warehouse.warehouse?.name || 'Unknown'} (Stok: ${egg.product_warehouse.quantity?.toLocaleString('en-US') || 0})`,
}); });
} }
} }
@@ -608,7 +605,7 @@ const RecordingForm = ({ type = 'add', initialValues }: RecordingFormProps) => {
} }
return options; return options;
}, [eggProductsData, initialValues, type]); }, [eggProductsData, initialValues, type, selectedKandang]);
const isLayingCategory = const isLayingCategory =
initialValues?.project_flock_category === 'LAYING' || initialValues?.project_flock_category === 'LAYING' ||