mirror of
https://gitlab.com/mbugroup/lti-web-client.git
synced 2026-05-25 07:45:47 +00:00
refactor(FE-remove-kandang-fetch): Refactor Kandang select logic to use
derived options
This commit is contained in:
@@ -5,12 +5,12 @@ import { Icon } from '@iconify/react';
|
|||||||
import Modal, { useModal } from '@/components/Modal';
|
import Modal, { useModal } from '@/components/Modal';
|
||||||
import DateInput from '@/components/input/DateInput';
|
import DateInput from '@/components/input/DateInput';
|
||||||
import { OptionType, useSelect } from '@/components/input/SelectInput';
|
import { OptionType, useSelect } from '@/components/input/SelectInput';
|
||||||
import { useState, useEffect, useRef, useCallback } from 'react';
|
import { useState, useEffect, useRef, useCallback, useMemo } from 'react';
|
||||||
import useSWR from 'swr';
|
import useSWR from 'swr';
|
||||||
import { DashboardApi } from '@/services/api/dashboard';
|
import { DashboardApi } from '@/services/api/dashboard';
|
||||||
import { useFormik } from 'formik';
|
import { useFormik } from 'formik';
|
||||||
import { ProjectFlockApi } from '@/services/api/production';
|
import { ProjectFlockApi } from '@/services/api/production';
|
||||||
import { KandangApi, LocationApi } from '@/services/api/master-data';
|
import { LocationApi } from '@/services/api/master-data';
|
||||||
import { generateDashboardPDF } from '@/components/pages/dashboard/export/DashboardPDF';
|
import { generateDashboardPDF } from '@/components/pages/dashboard/export/DashboardPDF';
|
||||||
import {
|
import {
|
||||||
DashboardFilterType,
|
DashboardFilterType,
|
||||||
@@ -42,6 +42,7 @@ import { cn } from '@/lib/helper';
|
|||||||
import DashboardExportStats, {
|
import DashboardExportStats, {
|
||||||
DashboardExportStatsRef,
|
DashboardExportStatsRef,
|
||||||
} from '@/components/pages/dashboard/export/DashboardExportStats';
|
} from '@/components/pages/dashboard/export/DashboardExportStats';
|
||||||
|
import { ProjectFlock } from '@/types/api/production/project-flock';
|
||||||
|
|
||||||
// Helper function to normalize values to array
|
// Helper function to normalize values to array
|
||||||
const normalizeToArray = (
|
const normalizeToArray = (
|
||||||
@@ -71,6 +72,7 @@ const DashboardProduction = () => {
|
|||||||
const [selectedLocationIds, setSelectedLocationIds] = useState<number[]>(
|
const [selectedLocationIds, setSelectedLocationIds] = useState<number[]>(
|
||||||
normalizeToArray(filterValues.location)
|
normalizeToArray(filterValues.location)
|
||||||
);
|
);
|
||||||
|
const [kandangInputValue, setRawKandangInputValue] = useState('');
|
||||||
const [exporting, setExporting] = useState(false);
|
const [exporting, setExporting] = useState(false);
|
||||||
const allChartsRef = useRef<DashboardExportChartsRef>(null);
|
const allChartsRef = useRef<DashboardExportChartsRef>(null);
|
||||||
const allStatsRef = useRef<DashboardExportStatsRef>(null);
|
const allStatsRef = useRef<DashboardExportStatsRef>(null);
|
||||||
@@ -114,9 +116,17 @@ const DashboardProduction = () => {
|
|||||||
options: flockOptions,
|
options: flockOptions,
|
||||||
isLoadingOptions: isLoadingFlockOptions,
|
isLoadingOptions: isLoadingFlockOptions,
|
||||||
loadMore: loadMoreFlock,
|
loadMore: loadMoreFlock,
|
||||||
} = useSelect(ProjectFlockApi.basePath, 'id', 'flock_name', '', {
|
rawData: projectFlocksRawData,
|
||||||
location_id: selectedLocationIds ? selectedLocationIds.toString() : '',
|
} = useSelect<ProjectFlock>(
|
||||||
});
|
ProjectFlockApi.basePath,
|
||||||
|
'id',
|
||||||
|
'flock_name',
|
||||||
|
'',
|
||||||
|
{
|
||||||
|
location_id:
|
||||||
|
selectedLocationIds.length > 0 ? selectedLocationIds.toString() : '',
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
const {
|
const {
|
||||||
setInputValue: setInputValueLocation,
|
setInputValue: setInputValueLocation,
|
||||||
@@ -125,13 +135,6 @@ const DashboardProduction = () => {
|
|||||||
loadMore: loadMoreLocation,
|
loadMore: loadMoreLocation,
|
||||||
} = useSelect(LocationApi.basePath, 'id', 'name');
|
} = useSelect(LocationApi.basePath, 'id', 'name');
|
||||||
|
|
||||||
const {
|
|
||||||
setInputValue: setInputValueKandang,
|
|
||||||
options: kandangOptions,
|
|
||||||
isLoadingOptions: isLoadingKandangOptions,
|
|
||||||
loadMore: loadMoreKandang,
|
|
||||||
} = useSelect(KandangApi.basePath, 'id', 'name', '');
|
|
||||||
|
|
||||||
const comparisonTypeOptions = [
|
const comparisonTypeOptions = [
|
||||||
{ value: 'FARM', label: 'Farm' },
|
{ value: 'FARM', label: 'Farm' },
|
||||||
{ value: 'FLOCK', label: 'Flock' },
|
{ value: 'FLOCK', label: 'Flock' },
|
||||||
@@ -162,11 +165,66 @@ const DashboardProduction = () => {
|
|||||||
|
|
||||||
const { resetForm } = formik;
|
const { resetForm } = formik;
|
||||||
|
|
||||||
|
const selectedFlockIds = useMemo(
|
||||||
|
() => normalizeToArray(formik.values.flock),
|
||||||
|
[formik.values.flock]
|
||||||
|
);
|
||||||
|
|
||||||
|
const derivedKandangOptions = useMemo(() => {
|
||||||
|
if (!isResponseSuccess(projectFlocksRawData)) return [];
|
||||||
|
|
||||||
|
const availableProjectFlocks = projectFlocksRawData.data.filter(
|
||||||
|
(projectFlock) =>
|
||||||
|
selectedFlockIds.length === 0 ||
|
||||||
|
selectedFlockIds.includes(projectFlock.id)
|
||||||
|
);
|
||||||
|
|
||||||
|
const kandangMap = new Map<number, OptionType<number>>();
|
||||||
|
|
||||||
|
availableProjectFlocks.forEach((projectFlock) => {
|
||||||
|
projectFlock.kandangs?.forEach((kandang) => {
|
||||||
|
if (!kandangMap.has(kandang.id)) {
|
||||||
|
kandangMap.set(kandang.id, {
|
||||||
|
value: kandang.id,
|
||||||
|
label: kandang.name,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
const normalizedSearch = kandangInputValue.trim().toLowerCase();
|
||||||
|
const allOptions = Array.from(kandangMap.values());
|
||||||
|
|
||||||
|
if (!normalizedSearch) return allOptions;
|
||||||
|
|
||||||
|
return allOptions.filter((option) =>
|
||||||
|
option.label.toLowerCase().includes(normalizedSearch)
|
||||||
|
);
|
||||||
|
}, [projectFlocksRawData, selectedFlockIds, kandangInputValue]);
|
||||||
|
|
||||||
|
const kandangSelect = useMemo(
|
||||||
|
() => ({
|
||||||
|
setInputValue: setRawKandangInputValue,
|
||||||
|
options: derivedKandangOptions,
|
||||||
|
isLoadingOptions: isLoadingFlockOptions,
|
||||||
|
loadMore: loadMoreFlock,
|
||||||
|
}),
|
||||||
|
[derivedKandangOptions, isLoadingFlockOptions, loadMoreFlock]
|
||||||
|
);
|
||||||
|
|
||||||
|
const {
|
||||||
|
setInputValue: setInputValueKandang,
|
||||||
|
options: kandangOptions,
|
||||||
|
isLoadingOptions: isLoadingKandangOptions,
|
||||||
|
loadMore: loadMoreKandang,
|
||||||
|
} = kandangSelect;
|
||||||
|
|
||||||
const handleResetFilter = useCallback(() => {
|
const handleResetFilter = useCallback(() => {
|
||||||
resetForm();
|
resetForm();
|
||||||
resetFilterValues(); // Clear stored filter values
|
resetFilterValues(); // Clear stored filter values
|
||||||
setAnalysisMode('OVERVIEW');
|
setAnalysisMode('OVERVIEW');
|
||||||
setSelectedLocationIds([]);
|
setSelectedLocationIds([]);
|
||||||
|
setRawKandangInputValue('');
|
||||||
}, [resetForm, resetFilterValues]);
|
}, [resetForm, resetFilterValues]);
|
||||||
|
|
||||||
// ===== Formik Error List =====
|
// ===== Formik Error List =====
|
||||||
@@ -461,6 +519,7 @@ const DashboardProduction = () => {
|
|||||||
formik.setFieldValue('kandang', []);
|
formik.setFieldValue('kandang', []);
|
||||||
formik.setFieldValue('comparisonType', '');
|
formik.setFieldValue('comparisonType', '');
|
||||||
setSelectedLocationIds([]);
|
setSelectedLocationIds([]);
|
||||||
|
setRawKandangInputValue('');
|
||||||
}}
|
}}
|
||||||
color='primary'
|
color='primary'
|
||||||
className={{
|
className={{
|
||||||
@@ -532,6 +591,7 @@ const DashboardProduction = () => {
|
|||||||
// Reset dependent fields when location changes
|
// Reset dependent fields when location changes
|
||||||
formik.setFieldValue('flock', []);
|
formik.setFieldValue('flock', []);
|
||||||
formik.setFieldValue('kandang', []);
|
formik.setFieldValue('kandang', []);
|
||||||
|
setRawKandangInputValue('');
|
||||||
}}
|
}}
|
||||||
errorMessage={formik.errors.location as string}
|
errorMessage={formik.errors.location as string}
|
||||||
options={locationOptions}
|
options={locationOptions}
|
||||||
@@ -564,6 +624,7 @@ const DashboardProduction = () => {
|
|||||||
// Reset dependent fields when location changes
|
// Reset dependent fields when location changes
|
||||||
formik.setFieldValue('flock', []);
|
formik.setFieldValue('flock', []);
|
||||||
formik.setFieldValue('kandang', []);
|
formik.setFieldValue('kandang', []);
|
||||||
|
setRawKandangInputValue('');
|
||||||
}}
|
}}
|
||||||
errorMessage={formik.errors.location as string}
|
errorMessage={formik.errors.location as string}
|
||||||
options={locationOptions}
|
options={locationOptions}
|
||||||
@@ -600,9 +661,11 @@ const DashboardProduction = () => {
|
|||||||
| null
|
| null
|
||||||
| undefined
|
| undefined
|
||||||
}
|
}
|
||||||
onChange={(selected) =>
|
onChange={(selected) => {
|
||||||
formik.setFieldValue('flock', selected)
|
formik.setFieldValue('flock', selected);
|
||||||
}
|
formik.setFieldValue('kandang', []);
|
||||||
|
setInputValueKandang('');
|
||||||
|
}}
|
||||||
errorMessage={formik.errors.flock as string}
|
errorMessage={formik.errors.flock as string}
|
||||||
onInputChange={setInputValueFlock}
|
onInputChange={setInputValueFlock}
|
||||||
onMenuScrollToBottom={loadMoreFlock}
|
onMenuScrollToBottom={loadMoreFlock}
|
||||||
@@ -627,9 +690,11 @@ const DashboardProduction = () => {
|
|||||||
| null
|
| null
|
||||||
| undefined
|
| undefined
|
||||||
}
|
}
|
||||||
onChange={(selected) =>
|
onChange={(selected) => {
|
||||||
formik.setFieldValue('flock', selected)
|
formik.setFieldValue('flock', selected);
|
||||||
}
|
formik.setFieldValue('kandang', []);
|
||||||
|
setInputValueKandang('');
|
||||||
|
}}
|
||||||
errorMessage={formik.errors.flock as string}
|
errorMessage={formik.errors.flock as string}
|
||||||
onInputChange={setInputValueFlock}
|
onInputChange={setInputValueFlock}
|
||||||
onMenuScrollToBottom={loadMoreFlock}
|
onMenuScrollToBottom={loadMoreFlock}
|
||||||
|
|||||||
Reference in New Issue
Block a user