"use client"; import { useState, useEffect } from "react"; import useSWR from 'swr'; import { Button, Input, Label, Select, SelectTrigger, SelectContent, SelectItem, SelectValue } from "@common/components"; import { useAlertStore } from "@/shared/lib"; import { type SearchFormProps, type SearchFormValues, defaultSearchFormProps } from "../type/SearchForm.type"; import { statisticsAPi } from "@/entities/statistics/api/statistics-service"; import type { ApiResponse, SearchCondition, Sgg } from "@/entities/statistics/model/statistics-interface"; const getSidoListFetcher = async (): Promise> => { return statisticsAPi.getSidoList(); }; const getSggListFetcher = async (key: [string, string]): Promise> => { return statisticsAPi.getSggList(key[1]); }; const SearchForm = (props: SearchFormProps = {}) => { const { setMessage: alert } = useAlertStore((state) => state); const mergedProps = { ...defaultSearchFormProps, ...props }; const currentYear = new Date().getFullYear(); const defaultYear = currentYear - 1; const { data: sidoList } = useSWR>( "sido-list", getSidoListFetcher, { revalidateOnFocus: false, revalidateOnReconnect: false, revalidateIfStale: false, dedupingInterval: 2000, shouldRetryOnError: false, onSuccess: (data) => { if (data.status !== 'success') { alert(`시도 목록을 불러오는데 실패했습니다. 새로고침 후 다시 시도해주세요.`); } }, onError: () => { alert(`시도 목록을 불러오는데 실패했습니다. 새로고침 후 다시 시도해주세요.`); }, } ); const [values, setValues] = useState({ sido: props.defaultValues?.sido || '', gugun: props.defaultValues?.gugun || '', year: props.defaultValues?.year || defaultYear.toString(), order: props.defaultValues?.order || '', fctName: props.defaultValues?.fctName || '', bsnmCd: props.defaultValues?.bsnmCd || '', }); const sggKey: [string, string] | null = values.sido && values.sido !== "ALL" ? ["sgg-list", values.sido] : null; const { data: sggList } = useSWR>( sggKey, getSggListFetcher, { revalidateOnFocus: false, revalidateOnReconnect: false, revalidateIfStale: false, dedupingInterval: 2000, shouldRetryOnError: false, onSuccess: (data) => { if (data.status !== 'success') { alert(`시군구 목록을 불러오는데 실패했습니다. 새로고침 후 다시 시도해주세요.`); } }, onError: () => { alert(`시군구 목록을 불러오는데 실패했습니다. 새로고침 후 다시 시도해주세요.`); }, } ); const sggOptions = sggList?.data ?? []; const handleSubmit = (e: React.FormEvent) => { e.preventDefault(); props.onSubmit?.(values); }; const handleReset = () => { const resetValues: SearchFormValues = { sido: '', gugun: '', year: String(defaultYear), order: '', fctName: '', bsnmCd: '', }; setValues(resetValues); props.onReset?.(); }; const handleSidoChange = (value: string) => { const selectedBsnm = sidoList?.data.sidoList.find((v) => v.sido === value)?.bsnmCd || ''; setValues((prev) => ({ ...prev, sido: value, gugun: '', bsnmCd: selectedBsnm, })); }; const handleSggChange = (value: string) => { const listForBsnm = sggList?.data ?? []; const selectedBsnm = value === 'ALL' ? (sidoList?.data.sidoList.find((v) => v.sido === values.sido)?.bsnmCd || '') : (listForBsnm.find((v) => v.gugun === value)?.bsnmCd || ''); setValues((prev) => ({ ...prev, gugun: value, bsnmCd: selectedBsnm, })); }; useEffect(() => { props.onValuesChange?.(values); }, [values]); return (
{mergedProps.sido &&
} {mergedProps.gugun &&
} {mergedProps.year &&
} {mergedProps.order &&
}
{(mergedProps.fctName || mergedProps.userMenual) &&
{mergedProps.fctName &&
setValues({ ...values, fctName: e.target.value })} autoComplete="off" />
} {mergedProps.userMenual &&
}
}
); }; export default SearchForm;