import { TextInput, BtnWrapper, InputLabel, SelectInput, InputGroup } from '../../../styles/Components'; import Button from '../../common/button/Button'; import { SearchBarLayout, SearchPeriod } from '../../common/SearchBar'; import { useCallback, useEffect, useState } from 'react'; import { LandAuctionView, LandInfoData } from '../../../apis'; import { landAuctionStatus, landSearchType, landSize, opLandCategoryType } from '../../../assets/data'; import { opLandInfoStatusType } from '../../../assets/data/options'; export const useLandInfoSearch = (token, initialPageSize) => { const [searchParams, setSearchParams] = useState({ landType: 'ID', landData: '', landSize: 'ALL', category: 'ALL', status: 'ALL', startDate: '', endDate: '', orderBy: 'DESC', pageSize: initialPageSize, currentPage: 1 }); const [loading, setLoading] = useState(false); const [data, setData] = useState(null); useEffect(() => { // fetchData(searchParams); // 컴포넌트 마운트 시 초기 데이터 로드 const initialLoad = async () => { await fetchData(searchParams); }; initialLoad(); }, [token]); const fetchData = useCallback(async (params) => { if (!token) return; try { setLoading(true); const result = await LandInfoData( token, params.landType, params.landData, params.landSize, params.category, params.status, params.startDate && new Date(params.startDate).toISOString(), params.endDate && new Date(params.endDate).toISOString(), params.orderBy, params.pageSize, params.currentPage ); setData(result); return result; } catch (error) { console.error('Error fetching auction data:', error); throw error; } finally { setLoading(false); } }, [token]); const updateSearchParams = useCallback((newParams) => { setSearchParams(prev => ({ ...prev, ...newParams })); }, []); const handleSearch = useCallback(async (newParams = {}) => { const updatedParams = { ...searchParams, ...newParams, currentPage: newParams.currentPage || 1 // Reset to first page on new search }; updateSearchParams(updatedParams); return await fetchData(updatedParams); }, [searchParams, fetchData]); const handleReset = useCallback(async () => { const resetParams = { landType: 'ID', landData: '', landSize: 'ALL', category: 'ALL', status: 'ALL', startDate: '', endDate: '', orderBy: 'DESC', pageSize: initialPageSize, currentPage: 1 }; setSearchParams(resetParams); return await fetchData(resetParams); }, [initialPageSize, fetchData]); const handlePageChange = useCallback(async (newPage) => { return await handleSearch({ currentPage: newPage }); }, [handleSearch]); const handlePageSizeChange = useCallback(async (newSize) => { return await handleSearch({ pageSize: newSize, currentPage: 1 }); }, [handleSearch]); const handleOrderByChange = useCallback(async (newOrder) => { return await handleSearch({ orderBy: newOrder }); }, [handleSearch]); return { searchParams, loading, data, handleSearch, handleReset, handlePageChange, handlePageSizeChange, handleOrderByChange, updateSearchParams }; }; const LandInfoSearchBar = ({ searchParams, onSearch, onReset }) => { const handleSubmit = event => { event.preventDefault(); onSearch(searchParams); }; const searchList = [ <> onSearch({landType: e.target.value })}> {landSearchType.map((data, index) => ( ))} onSearch({ landData: e.target.value })} /> , <> 랜드크기 onSearch({ landSize: e.target.value }, false)} > {landSize.map((data, index) => ( ))} , <> 랜드상태 onSearch({ status: e.target.value }, false)} > {opLandInfoStatusType.map((data, index) => ( ))} , ]; const optionList = [ <> 카테고리 onSearch({ category: e.target.value }, false)}> {opLandCategoryType.map((data, index) => ( ))} , <> 일자 onSearch({ startDate: date }, false)} endDate={searchParams.endDate} handleEndDate={date => onSearch({ endDate: date }, false)} /> , <>,<>, <>