-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUniversityMultiSelect.tsx
More file actions
191 lines (176 loc) · 6.62 KB
/
Copy pathUniversityMultiSelect.tsx
File metadata and controls
191 lines (176 loc) · 6.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
'use client';
import { useInfiniteQuery } from '@tanstack/react-query';
import { CheckIcon, ChevronsUpDownIcon, Loader2Icon, PlusIcon } from 'lucide-react';
import { useEffect, useMemo, useRef, useState } from 'react';
import { Button } from '@/components/ui/button';
import {
Command,
CommandEmpty,
CommandGroup,
CommandInput,
CommandItem,
CommandList,
} from '@/components/ui/command';
import { Popover, PopoverContent, PopoverTrigger } from '@/components/ui/popover';
import { apiClient, throwIfError } from '@/lib/api/client';
import { queryKeys } from '@/lib/query/keys';
import { cn } from '@/lib/utils';
import type { UniversityOption } from './UniversitySelect';
const UNIVERSITY_PAGE_SIZE = 100;
type UniversityMultiSelectProps = {
onAdd: (universities: UniversityOption[]) => void;
disabled?: boolean;
};
export function UniversityMultiSelect({ onAdd, disabled = false }: UniversityMultiSelectProps) {
const [open, setOpen] = useState(false);
const [searchQuery, setSearchQuery] = useState('');
const [selectedUniversities, setSelectedUniversities] = useState<UniversityOption[]>([]);
const loadMoreRef = useRef<HTMLDivElement | null>(null);
const { data, isLoading, isFetchingNextPage, hasNextPage, fetchNextPage } = useInfiniteQuery({
queryKey: queryKeys.admin.universities({ q: searchQuery, pageSize: UNIVERSITY_PAGE_SIZE }),
queryFn: async ({ pageParam = 1 }) => {
const result = await apiClient.GET('/api/admin/universities', {
params: {
query: {
page: pageParam,
pageSize: UNIVERSITY_PAGE_SIZE,
q: searchQuery || undefined,
},
},
});
return throwIfError(result);
},
initialPageParam: 1,
getNextPageParam: (lastPage) =>
lastPage.pagination.hasNext ? lastPage.pagination.page + 1 : undefined,
});
const universities = useMemo(() => {
const allUniversities = (data?.pages ?? []).flatMap((page) => page.data) as UniversityOption[];
const seenIds = new Set<string>();
return allUniversities.filter((university) => {
if (seenIds.has(university.id)) {
return false;
}
seenIds.add(university.id);
return true;
});
}, [data?.pages]);
const selectedIds = useMemo(
() => new Set(selectedUniversities.map((university) => university.id)),
[selectedUniversities],
);
useEffect(() => {
const node = loadMoreRef.current;
if (!node || !hasNextPage) {
return;
}
const observer = new IntersectionObserver((entries) => {
const [entry] = entries;
if (entry?.isIntersecting && hasNextPage && !isFetchingNextPage) {
fetchNextPage();
}
});
observer.observe(node);
return () => observer.disconnect();
}, [fetchNextPage, hasNextPage, isFetchingNextPage]);
const toggleUniversity = (university: UniversityOption) => {
setSelectedUniversities((current) => {
if (current.some((item) => item.id === university.id)) {
return current.filter((item) => item.id !== university.id);
}
return [...current, university];
});
};
const handleAdd = () => {
if (selectedUniversities.length === 0) {
return;
}
onAdd(selectedUniversities);
setSelectedUniversities([]);
setSearchQuery('');
setOpen(false);
};
return (
<Popover open={open} onOpenChange={setOpen}>
<PopoverTrigger
render={<Button variant='outline' className='w-72 justify-between' disabled={disabled} />}
>
<span className={cn(selectedUniversities.length === 0 && 'text-muted-foreground')}>
{selectedUniversities.length === 0
? '大学を複数選択...'
: `${selectedUniversities.length}校を選択中`}
</span>
<ChevronsUpDownIcon className='h-4 w-4 opacity-50' />
</PopoverTrigger>
<PopoverContent className='w-72 p-0'>
<Command shouldFilter={false}>
<CommandInput placeholder='検索...' value={searchQuery} onValueChange={setSearchQuery} />
<CommandList>
{isLoading ? (
<div className='flex items-center justify-center gap-2 py-6 text-sm text-muted-foreground'>
<Loader2Icon className='h-4 w-4 animate-spin' />
読み込み中...
</div>
) : (
<>
<CommandEmpty>見つかりません</CommandEmpty>
<CommandGroup>
{universities.map((university) => {
const selected = selectedIds.has(university.id);
return (
<CommandItem
key={university.id}
value={university.id}
data-checked={selected}
onSelect={() => toggleUniversity(university)}
>
<span className='flex h-4 w-4 items-center justify-center rounded-sm border border-border'>
{selected && <CheckIcon className='h-3 w-3' />}
</span>
<span className='min-w-0 truncate'>{university.name}</span>
</CommandItem>
);
})}
</CommandGroup>
<div ref={loadMoreRef} className='h-1' />
{hasNextPage && !isFetchingNextPage && (
<div className='px-2 pb-2'>
<Button
type='button'
variant='ghost'
size='sm'
className='w-full'
onClick={() => fetchNextPage()}
>
さらに読み込む
</Button>
</div>
)}
{isFetchingNextPage && (
<div className='flex items-center justify-center gap-2 py-2 text-sm text-muted-foreground'>
<Loader2Icon className='h-4 w-4 animate-spin' />
さらに読み込み中...
</div>
)}
</>
)}
</CommandList>
<div className='flex items-center justify-between gap-2 border-t p-2'>
<span className='text-xs text-muted-foreground'>
{selectedUniversities.length}校選択
</span>
<Button
type='button'
size='sm'
onClick={handleAdd}
disabled={selectedUniversities.length === 0}
>
<PlusIcon className='h-3 w-3' />
候補に追加
</Button>
</div>
</Command>
</PopoverContent>
</Popover>
);
}