Skip to content

Commit

Permalink
๐Ÿ› fix : ๋ˆ„๋ฝ๋œ ์ง€์—ญ๊ตฌ ๋‹จ์œ„ ์„ ํƒ ๊ธฐ๋Šฅ ๋ฐ ํ›… ๋ถ„๋ฆฌ ์ ์šฉ #127
Browse files Browse the repository at this point in the history
  • Loading branch information
MrMirror21 committed Dec 24, 2024
1 parent 3877293 commit 1856907
Showing 1 changed file with 20 additions and 76 deletions.
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 1856907

Please sign in to comment.