Files
frontend/apps/cm/src/pages/users/ui/zip-search-dialog.tsx
T
2025-08-10 13:04:42 +09:00

225 lines
6.1 KiB
TypeScript

import { useState, useRef, useEffect } from "react";
import {
keepPreviousData,
useQuery,
useQueryClient,
} from "@tanstack/react-query";
import {
getCoreRowModel,
getPaginationRowModel,
type PaginationState,
} from "@tanstack/react-table";
import {
Dialog,
DialogContent,
DialogHeader,
DialogFooter,
DialogTitle,
DialogTrigger,
DialogClose,
Button,
Input,
Label,
} from "@common/components";
import { ENDPOINT } from "@/shared/config";
import { useDataTable } from "@/shared/lib";
import { ZipApi, type Zip, type ZipSearchParams } from "../api/zip-service";
interface ZipSearchDialogProps {
onSelectAddress: (data: { zip: string; addr: string }) => void;
}
function ZipSearchDialog({ onSelectAddress }: ZipSearchDialogProps) {
const queryClient = useQueryClient();
const [open, setOpen] = useState(false);
const [selectedAddress, setSelectedAddress] = useState<Zip | null>(null);
const [pagination, setPagination] = useState<PaginationState>({
pageIndex: 0,
pageSize: 10,
});
const [searchQuery, setSearchQuery] = useState<ZipSearchParams>({
roadNm: "",
lotnoMno: null,
lotnoSno: null,
});
const hasFetchedOnce = useRef(false);
const query = useQuery({
queryKey: [ENDPOINT.CMS_SERVICE.ZIPS, pagination],
queryFn: () =>
ZipApi.searchZip({
...searchQuery,
page: pagination.pageIndex,
size: pagination.pageSize,
}),
placeholderData: keepPreviousData,
enabled: false,
});
const handleRowClick = (row: Zip) => {
table.setRowSelection({
[row.zipId.toString()]: true,
});
setSelectedAddress(row);
};
const { tableComponent, tableInstance: table } = useDataTable({
query,
tableOptions: {
columns: [
{
accessorKey: "zoneNo",
header: "우편번호",
},
{
accessorKey: "zipAddr",
header: "주소",
},
],
state: {
pagination,
},
manualPagination: true,
enableMultiRowSelection: false,
getRowId: (row) => row.zipId.toString(),
onPaginationChange: setPagination,
getCoreRowModel: getCoreRowModel(),
getPaginationRowModel: getPaginationRowModel(),
},
dataTableOptions: {
onRowClick: handleRowClick,
viewPagination: true,
variant: "secondary",
},
});
const handleSearch = async () => {
await query.refetch();
queryClient.removeQueries({
queryKey: [ENDPOINT.CMS_SERVICE.ZIPS],
});
setPagination({
pageIndex: 0,
pageSize: 10,
});
table.setRowSelection({});
setSelectedAddress(null);
hasFetchedOnce.current = true;
};
const handleSelectAddress = () => {
if (!selectedAddress) return;
onSelectAddress({
zip: selectedAddress.zoneNo,
addr: selectedAddress.zipAddr,
});
setOpen(false);
};
useEffect(() => {
if (hasFetchedOnce.current) query.refetch();
}, [pagination]);
return (
<Dialog open={open} onOpenChange={setOpen}>
<DialogTrigger asChild>
<Button className="px-6">우편번호 검색</Button>
</DialogTrigger>
<DialogContent className="max-w-[60rem]" aria-describedby={undefined}>
<DialogHeader>
<DialogTitle>주소 검색</DialogTitle>
</DialogHeader>
<form
onSubmit={(e) => {
e.preventDefault();
e.stopPropagation();
handleSearch();
}}
className="mb-4 space-y-2"
>
<div className="card card-border !m-0 flex flex-col gap-4 p-4">
<div className="grid grid-cols-3 gap-4">
<div className="flex flex-col">
<Label className="mb-1 text-base">도로명</Label>
<Input
value={searchQuery.roadNm || ""}
onChange={(e) =>
setSearchQuery({
...searchQuery,
roadNm: e.target.value,
})
}
placeholder="예: 테헤란로"
className="!text-[1.3rem]"
/>
</div>
<div className="flex flex-col">
<Label className="mb-1 text-base">지번본번</Label>
<Input
type="number"
value={searchQuery.lotnoMno ?? ""}
onChange={(e) => {
const value = e.target.value
? parseInt(e.target.value)
: null;
setSearchQuery({
...searchQuery,
lotnoMno: value,
});
}}
placeholder="예: 123"
className="!text-[1.3rem]"
/>
</div>
<div className="flex flex-col">
<Label className="mb-1 text-base">지번부번</Label>
<Input
type="number"
value={searchQuery.lotnoSno ?? ""}
onChange={(e) => {
const value = e.target.value
? parseInt(e.target.value)
: null;
setSearchQuery({
...searchQuery,
lotnoSno: value,
});
}}
placeholder="예: 45"
className="!text-[1.3rem]"
/>
</div>
</div>
<div className="flex justify-end">
<Button type="submit">검색</Button>
</div>
</div>
</form>
{query.data?.content.length && tableComponent}
<DialogFooter className="!justify-center">
<Button
size="lg"
onClick={handleSelectAddress}
disabled={!selectedAddress}
>
선택
</Button>
<DialogClose asChild>
<Button size="lg" color="white">
취소
</Button>
</DialogClose>
</DialogFooter>
</DialogContent>
</Dialog>
);
}
export { ZipSearchDialog };