fix: implement lazy loading in kandang select input

This commit is contained in:
ValdiANS
2026-03-09 17:14:35 +07:00
parent 0263db9fae
commit efcecf4f66
2 changed files with 46 additions and 26 deletions
@@ -16,7 +16,7 @@ import {
SelectValue, SelectValue,
} from '@/figma-make/components/base/select'; } from '@/figma-make/components/base/select';
import { Badge } from '@/figma-make/components/base/badge'; import { Badge } from '@/figma-make/components/base/badge';
import { Users, AlertCircle, Info } from 'lucide-react'; import { Users, AlertCircle, Info, Loader2 } from 'lucide-react';
import { DateRangePicker } from '@/figma-make/components/base/date-range-picker'; import { DateRangePicker } from '@/figma-make/components/base/date-range-picker';
import { import {
BarChart, BarChart,
@@ -36,10 +36,10 @@ import { DailyChecklistSummary } from '@/types/api/daily-checklist/daily-checkli
import { AxiosError } from 'axios'; import { AxiosError } from 'axios';
import { httpClientFetcher, SWRHttpKey } from '@/services/http/client'; import { httpClientFetcher, SWRHttpKey } from '@/services/http/client';
import { DailyChecklistApi } from '@/services/api/daily-checklist/daily-checklist'; import { DailyChecklistApi } from '@/services/api/daily-checklist/daily-checklist';
import { KandangApi } from '@/services/api/master-data';
import { useSelect } from '@/components/input/SelectInput'; import { useSelect } from '@/components/input/SelectInput';
import { isResponseError, isResponseSuccess } from '@/lib/api-helper'; import { isResponseError, isResponseSuccess } from '@/lib/api-helper';
import { formatDate } from '@/lib/helper'; import { formatDate } from '@/lib/helper';
import { DailyChecklistKandangApi } from '@/services/api/daily-checklist/kandang';
const KANDANG_COLORS = [ const KANDANG_COLORS = [
'#0069e0', // Blue (primary) '#0069e0', // Blue (primary)
@@ -77,16 +77,20 @@ export function Dashboard() {
httpClientFetcher httpClientFetcher
); );
const { options: kandangOptions } = useSelect( const {
KandangApi.basePath, options: kandangOptions,
'id', loadMore: loadMoreKandang,
'name', isLoadingMore: isLoadingMoreKandang,
'search', } = useSelect(DailyChecklistKandangApi.basePath, 'id', 'name');
{
page: '1', const handleKandangScroll = (e: React.UIEvent<HTMLDivElement>) => {
limit: '100', const target = e.target as HTMLDivElement;
if (target.scrollHeight - target.scrollTop <= target.clientHeight + 10) {
if (!isLoadingMoreKandang) {
loadMoreKandang();
}
} }
); };
const kandangColorMap: { [key: string]: string } = {}; const kandangColorMap: { [key: string]: string } = {};
(kandangOptions || []).forEach((k, index) => { (kandangOptions || []).forEach((k, index) => {
@@ -164,7 +168,7 @@ export function Dashboard() {
> >
<SelectValue placeholder='Semua Kandang' /> <SelectValue placeholder='Semua Kandang' />
</SelectTrigger> </SelectTrigger>
<SelectContent> <SelectContent onScroll={handleKandangScroll}>
<SelectItem value='ALL'>Semua Kandang</SelectItem> <SelectItem value='ALL'>Semua Kandang</SelectItem>
{kandangOptions.map((kandang) => ( {kandangOptions.map((kandang) => (
<SelectItem <SelectItem
@@ -174,6 +178,11 @@ export function Dashboard() {
{kandang.label} {kandang.label}
</SelectItem> </SelectItem>
))} ))}
{isLoadingMoreKandang && (
<div className='flex justify-center p-2'>
<Loader2 className='h-4 w-4 animate-spin text-gray-500' />
</div>
)}
</SelectContent> </SelectContent>
</Select> </Select>
</div> </div>
@@ -11,7 +11,7 @@ import {
SelectValue, SelectValue,
} from '@/figma-make/components/base/select'; } from '@/figma-make/components/base/select';
import { useSelect } from '@/components/input/SelectInput'; import { useSelect } from '@/components/input/SelectInput';
import { AreaApi, KandangApi, LocationApi } from '@/services/api/master-data'; import { AreaApi, LocationApi } from '@/services/api/master-data';
import useSWR from 'swr'; import useSWR from 'swr';
import { BaseApiResponse } from '@/types/api/api-general'; import { BaseApiResponse } from '@/types/api/api-general';
import { DailyChecklistReport } from '@/types/api/daily-checklist/daily-checklist'; import { DailyChecklistReport } from '@/types/api/daily-checklist/daily-checklist';
@@ -26,7 +26,8 @@ import { ColumnDef } from '@tanstack/react-table';
import { PhaseApi } from '@/services/api/daily-checklist/phase'; import { PhaseApi } from '@/services/api/daily-checklist/phase';
import { EmployeeApi } from '@/services/api/daily-checklist/employee'; import { EmployeeApi } from '@/services/api/daily-checklist/employee';
import { Button } from '@/figma-make/components/base/button'; import { Button } from '@/figma-make/components/base/button';
import { Download } from 'lucide-react'; import { Download, Loader2 } from 'lucide-react';
import { DailyChecklistKandangApi } from '@/services/api/daily-checklist/kandang';
const MONTH_OPTIONS = [ const MONTH_OPTIONS = [
{ value: '1', label: 'Januari' }, { value: '1', label: 'Januari' },
@@ -129,18 +130,23 @@ export function DailyChecklistReportsContent() {
} }
); );
const { options: kandangOptions } = useSelect( const {
KandangApi.basePath, options: kandangOptions,
'id', loadMore: loadMoreKandang,
'name', isLoadingMore: isLoadingMoreKandang,
'search', } = useSelect(DailyChecklistKandangApi.basePath, 'id', 'name', 'search', {
{ area_id: tableFilterState.area_id,
page: '1', location_id: tableFilterState.location_id,
limit: '100', });
area_id: tableFilterState.area_id,
location_id: tableFilterState.location_id, const handleKandangScroll = (e: React.UIEvent<HTMLDivElement>) => {
const target = e.target as HTMLDivElement;
if (target.scrollHeight - target.scrollTop <= target.clientHeight + 10) {
if (!isLoadingMoreKandang) {
loadMoreKandang();
}
} }
); };
const { options: phaseOptions } = useSelect( const { options: phaseOptions } = useSelect(
PhaseApi.basePath, PhaseApi.basePath,
@@ -435,7 +441,7 @@ export function DailyChecklistReportsContent() {
> >
<SelectValue placeholder='Semua Kandang' /> <SelectValue placeholder='Semua Kandang' />
</SelectTrigger> </SelectTrigger>
<SelectContent> <SelectContent onScroll={handleKandangScroll}>
<SelectItem value='ALL'>Semua Kandang</SelectItem> <SelectItem value='ALL'>Semua Kandang</SelectItem>
{kandangOptions.map((kandang) => ( {kandangOptions.map((kandang) => (
<SelectItem <SelectItem
@@ -445,6 +451,11 @@ export function DailyChecklistReportsContent() {
{kandang.label} {kandang.label}
</SelectItem> </SelectItem>
))} ))}
{isLoadingMoreKandang && (
<div className='flex justify-center p-2'>
<Loader2 className='h-4 w-4 animate-spin text-gray-500' />
</div>
)}
</SelectContent> </SelectContent>
</Select> </Select>
</div> </div>