Skip to content

Commit

Permalink
πŸ› fix: νšŒμ›κ°€μž… κ³Όμ • 쀑 지역ꡬ λ‹¨μœ„ 선택 μ‹œ state λ°˜μ˜λ˜λ„λ‘ μˆ˜μ • Team-inglo#142
Browse files Browse the repository at this point in the history
  • Loading branch information
naarang committed Dec 24, 2024
1 parent bca39b9 commit 6ee18d8
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 78 deletions.
3 changes: 1 addition & 2 deletions src/components/Information/AddressStep.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,7 @@ const AddressStep = ({ userInfo, onNext }: AddressStepProps) => {
const handleAddressSelection = (selectedAddressName: string) => {
const result = handleAddressSelect(selectedAddressName);
if (!result) return;

setNewAddress({...newAddress, ...result});
setNewAddress({...newAddress, ...result.addressData});
setAddressInput(result.selectedAddressName);
};

Expand Down
96 changes: 20 additions & 76 deletions src/components/WriteDocuments/LaborContractWriteForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,14 @@ import {
LaborContractEmployeeInfo,
} from '@/types/api/document';
import { formatPhoneNumber, parsePhoneNumber } from '@/utils/information';
import { useCallback, useEffect, useState } from 'react';
import { useEffect, useState } from 'react';
import Input from '@/components/Common/Input';
import { InputType } from '@/types/common/input';
import Dropdown, { DropdownModal } from '@/components/Common/Dropdown';
import { phone } from '@/constants/information';
import { Map, MapMarker } from 'react-kakao-maps-sdk';
import { useGetGeoInfo, useSearchAddress } from '@/hooks/api/useKaKaoMap';
import { AddressType, Document } from '@/types/api/map';
import { pick } from '@/utils/map';
import { useGetGeoInfo } from '@/hooks/api/useKaKaoMap';
import { AddressType } from '@/types/api/map';
import { isNotEmpty } from '@/utils/document';
import BottomButtonPanel from '@/components/Common/BottomButtonPanel';
import Button from '@/components/Common/Button';
Expand All @@ -25,6 +24,7 @@ import {
} from '@/hooks/api/useDocument';
import { useCurrentPostIdEmployeeStore } from '@/store/url';
import LoadingItem from '../Common/LoadingItem';
import { useAddressSearch } from '@/hooks/api/useAddressSearch';

type LaborContractFormProps = {
document?: LaborContractDataResponse;
Expand All @@ -45,23 +45,16 @@ const LaborContractWriteForm = ({
middle: '',
end: '',
});
// μ£Όμ†Œ κ²€μƒ‰μš© input μ €μž₯ν•˜λŠ” state
const [addressInput, setAddressInput] = useState('');
// μ£Όμ†Œ 검색 κ²°κ³Όλ₯Ό μ €μž₯ν•˜λŠ” array
const [addressSearchResult, setAddressSearchResult] = useState<Document[]>(
[],
);
// 지도에 ν‘œμ‹œν•  핀에 μ‚¬μš©λ˜λŠ” μœ„/경도 μ’Œν‘œ
const [currentGeoInfo, setCurrentGeoInfo] = useState({
lat: 0,
lon: 0,
});
const {
addressInput, // μ£Όμ†Œ κ²€μƒ‰μš© input μ €μž₯ν•˜λŠ” state
addressSearchResult, // μ£Όμ†Œ 검색 κ²°κ³Όλ₯Ό μ €μž₯ν•˜λŠ” array
currentGeoInfo, // 지도에 ν‘œμ‹œν•  핀에 μ‚¬μš©λ˜λŠ” μœ„/경도 μ’Œν‘œ
setCurrentGeoInfo,
handleAddressSearch, // 검색할 μ£Όμ†Œ μž…λ ₯ μ‹œ μ‹€μ‹œκ°„ 검색
handleAddressSelect, // 검색 κ²°κ³Ό 쀑 μ›ν•˜λŠ” μ£Όμ†Œλ₯Ό 선택할 μ‹œ state에 μž…λ ₯
setAddressInput,
} = useAddressSearch();
const { data, isSuccess } = useGetGeoInfo(setCurrentGeoInfo); // ν˜„μž¬ μ’Œν‘œ κΈ°μ€€ μ£Όμ†Œ νšλ“
// ν‚€μ›Œλ“œλ‘œ μ£Όμ†Œ 검색
const { searchAddress } = useSearchAddress({
onSuccess: (data) => setAddressSearchResult(data),
});

const { mutate: postDocument } = usePostStandardLaborContracts(
Number(currentPostId),
{
Expand Down Expand Up @@ -114,68 +107,19 @@ const LaborContractWriteForm = ({
});
}, [isSuccess]);

// 검색할 μ£Όμ†Œ μž…λ ₯ μ‹œ μ‹€μ‹œκ°„ 검색
const handleAddressSearch = useCallback(
(address: string) => {
setAddressInput(address);
if (address !== '') {
searchAddress(address);
} else {
setAddressSearchResult([]);
}
},
[searchAddress],
);

// 검색 κ²°κ³Ό 쀑 μ›ν•˜λŠ” μ£Όμ†Œλ₯Ό 선택할 μ‹œ state에 μž…λ ₯
const handleAddressSelect = (selectedAddressName: string) => {
// μ‚¬μš©μžκ°€ μ„ νƒν•œ μ£Όμ†Œμ™€ μΌμΉ˜ν•˜λŠ” κ²°κ³Όλ₯Ό 검색 κ²°κ³Όλ₯Ό μ €μž₯ν•˜λŠ” arrayμ—μ„œ 탐색
const selectedAddress = addressSearchResult.find(
(address) => address.address_name === selectedAddressName,
) as Document | undefined;
// κ²€μƒ‰λœ μ£Όμ†Œ 선택 μ‹œ state에 반영
const handleAddressSelection = (selectedAddressName: string) => {
const result = handleAddressSelect(selectedAddressName);
if (!result) return;

if (!selectedAddress) return;

// ꡬ μ£Όμ†Œμ™€ λ„λ‘œλͺ… μ£Όμ†Œλ₯Ό κ΅¬λΆ„ν•˜κΈ° μœ„ν•œ ν”Œλž˜κ·Έ(μΉ΄μΉ΄μ˜€μ—μ„œ λ°˜ν™˜ν•˜λŠ” 속성 λͺ…이 달라짐)
const isRegionAddr =
selectedAddress.address_type === AddressType.REGION_ADDR;
const addressData = isRegionAddr
? selectedAddress.address
: selectedAddress.road_address;

// μΉ΄μΉ΄μ˜€μ—μ„œ λ°˜ν™˜ν•˜λŠ” 데이터 쀑 ν•„μš”ν•œ μ†μ„±λ“€λ§Œ 선택
const selectedProperties = pick(addressData, [
'address_name',
'region_1depth_name',
'region_2depth_name',
'region_3depth_name',
]);

let region4DepthName = ''; // optional property인 region4DeptName
if (isRegionAddr) {
region4DepthName = selectedAddress.address.region_3depth_h_name || '';
} else {
region4DepthName = selectedAddress.road_address.road_name || '';
}

// μ„ νƒν•œ 데이터듀을 state에 update
setNewDocumentData({
...newDocumentData,
address: {
...newDocumentData.address,
...selectedProperties,
region_4depth_name: region4DepthName,
longitude: Number(addressData.x),
latitude: Number(addressData.y),
...result.addressData,
},
});
setAddressInput(selectedAddress.address_name);
setCurrentGeoInfo({
lon: Number(selectedAddress.x),
lat: Number(selectedAddress.y),
});
// 검색 κ²°κ³Ό μ΄ˆκΈ°ν™”
setAddressSearchResult([]);
setAddressInput(result.selectedAddressName);
};

// λ¬Έμ„œ μž‘μ„± μ™„λ£Œ ν•Έλ“€λŸ¬ ν•¨μˆ˜
Expand Down Expand Up @@ -280,7 +224,7 @@ const LaborContractWriteForm = ({
),
(address) => address.address_name,
)}
onSelect={handleAddressSelect}
onSelect={handleAddressSelection}
/>
)}
</div>
Expand Down

0 comments on commit 6ee18d8

Please sign in to comment.