Skip to content

Commit 409338e

Browse files
refactor: make ComboboxArea component reusable (#48)
1 parent 6f79caa commit 409338e

File tree

3 files changed

+16
-11
lines changed

3 files changed

+16
-11
lines changed
Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ import type { FeatureArea } from '@/lib/config'
1010
import type { GetArea } from '@/lib/const'
1111
import type { Query } from '@/lib/data'
1212
import { ucFirstStr } from '@/lib/utils'
13+
import { useMemo, useState } from 'react'
1314
import { toast } from 'sonner'
14-
import { useMapDashboard } from './hooks/useDashboard'
1515

1616
function areaToOption<A extends FeatureArea>(data: GetArea<A>): ComboboxOption {
1717
return {
@@ -23,7 +23,7 @@ function areaToOption<A extends FeatureArea>(data: GetArea<A>): ComboboxOption {
2323

2424
export type ComboboxAreaProps<A extends FeatureArea> = Omit<
2525
ComboboxProps,
26-
'autoClose' | 'fullWidth' | 'options' | 'onSelect' | 'selected'
26+
'options' | 'onSelect' | 'selected'
2727
> & {
2828
area: A
2929
query: Query<A>
@@ -36,36 +36,35 @@ export default function ComboboxArea<A extends FeatureArea>({
3636
onSelect,
3737
...comboboxProps
3838
}: ComboboxAreaProps<A>) {
39-
const { selectedArea } = useMapDashboard()
39+
const [selectedArea, setSelectedArea] = useState<GetArea<A> | undefined>()
4040
const { data: areas = [], error } = useArea(area, {
4141
...query,
4242
sortBy: 'name',
4343
})
4444

45+
const options = useMemo(() => areas.map((a) => areaToOption<A>(a)), [areas])
46+
4547
if (error) {
4648
toast.error(`Failed to fetch ${area} data`, {
4749
description: error?.message,
4850
closeButton: true,
4951
})
5052
}
5153

52-
const areaData = selectedArea[area]
53-
5454
return (
5555
<Combobox
5656
{...comboboxProps}
57-
options={areas.map((a) => areaToOption<A>(a))}
57+
options={options}
5858
label={comboboxProps.label || ucFirstStr(area)}
5959
placeholder={comboboxProps.placeholder || `Search ${ucFirstStr(area)}`}
6060
onSelect={(opt) => {
6161
const selectedArea = areas.find((a) => a.code === opt.key)
6262
if (selectedArea) {
63+
setSelectedArea(selectedArea)
6364
onSelect?.(selectedArea)
6465
}
6566
}}
66-
selected={areaData ? areaToOption(areaData) : undefined}
67-
autoClose
68-
fullWidth
67+
selected={selectedArea ? areaToOption(selectedArea) : undefined}
6968
/>
7069
)
7170
}

modules/MapDashboard/AreaSelectors.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
'use client'
22

3+
import ComboboxArea from '@/components/ComboboxArea'
34
import { type FeatureArea, config, featureConfig } from '@/lib/config'
45
import type { Query } from '@/lib/data'
56
import {
@@ -9,7 +10,6 @@ import {
910
objectFromEntries,
1011
} from '@/lib/utils'
1112
import { useMemo, useState } from 'react'
12-
import ComboboxArea from './ComboboxArea'
1313
import { useMapDashboard } from './hooks/useDashboard'
1414

1515
const MAX_PAGE_SIZE = config.dataSource.area.pagination.maxPageSize
@@ -60,6 +60,8 @@ export default function AreaSelectors() {
6060
area={area}
6161
query={query[area]}
6262
disabled={parent ? isLoading[parent] : false}
63+
autoClose
64+
fullWidth
6365
onSelect={(selected) => {
6466
changeSelectedArea(area, selected)
6567

modules/Pilkada2024/Sidebar.tsx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
'use client'
22

3+
import ComboboxArea from '@/components/ComboboxArea'
34
import { Combobox } from '@/components/combobox'
45
import { Button } from '@/components/ui/button'
56
import { config } from '@/lib/config'
@@ -8,7 +9,6 @@ import type { Query } from '@/lib/data'
89
import { EraserIcon, LoaderCircleIcon } from 'lucide-react'
910
import { useState } from 'react'
1011
import { toast } from 'sonner'
11-
import ComboboxArea from '../MapDashboard/ComboboxArea'
1212
import { useMapDashboard } from '../MapDashboard/hooks/useDashboard'
1313
import VotesChart from './VotesChart'
1414
import {
@@ -71,6 +71,8 @@ export default function Sidebar() {
7171
area={Area.PROVINCE}
7272
query={{ limit: config.dataSource.area.pagination.maxPageSize }}
7373
disabled={!election}
74+
autoClose
75+
fullWidth
7476
onSelect={(option) => {
7577
changeSelectedArea(Area.PROVINCE, option)
7678
setQuery((prevQuery) => ({
@@ -86,6 +88,8 @@ export default function Sidebar() {
8688
area={Area.REGENCY}
8789
query={regencyQuery}
8890
disabled={!selectedArea.province}
91+
autoClose
92+
fullWidth
8993
onSelect={(option) => {
9094
changeSelectedArea(Area.REGENCY, option)
9195
}}

0 commit comments

Comments
 (0)