Skip to content

Commit

Permalink
Merge pull request #151 from prgrms-web-devcourse/feat/courts/query-s…
Browse files Browse the repository at this point in the history
…tring-restore

✨feat: 쿼리스트링 여부에 따른 지도 center restore
  • Loading branch information
Jonghyeon authored Dec 19, 2021
2 parents ac6fc0a + 2adfa18 commit 4ef7ea7
Showing 1 changed file with 38 additions and 17 deletions.
55 changes: 38 additions & 17 deletions src/pages/courts/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ const Courts: NextPage = () => {
(geocoder as Geocoder).coord2Address(longitude, latitude, callback);
};

const handleChangedMapBounds = useCallback(
const fetchCourtsByBoundsAndDatetime = useCallback(
async (map: kakao.maps.Map) => {
const bounds = map.getBounds();
const swLatlng = bounds.getSouthWest();
Expand Down Expand Up @@ -212,11 +212,17 @@ const Courts: NextPage = () => {
[currentDate, selectedSlot]
);

const handleMarkerClick = useCallback((court: any) => {
setIsOpen(true);
searchAddrFromCoords(court.latitude, court.longitude);
setSelectedCourt(court);
}, []);
const handleMarkerClick = useCallback(
(court: any) => {
setIsOpen(true);
searchAddrFromCoords(court.latitude, court.longitude);
setSelectedCourt(court);
router.push(`/courts?courtId=${court.courtId}`, undefined, {
shallow: true,
});
},
[router]
);

const handleChangeSnap = useCallback((snap: number) => {
setSnap(snap);
Expand All @@ -229,21 +235,37 @@ const Courts: NextPage = () => {
}, []);

useEffect(() => {
const fetchCourts = async () => {
if (map) {
await handleChangedMapBounds(map);
const restoreCourts = async (courtId: number) => {
try {
const court: any = await courtApi.getCourtDetail(courtId);

const { latitude, longitude } = court;

if (court) {
setCenter([latitude, longitude]);
setIsOpen(true);
searchAddrFromCoords(latitude, longitude);
setSelectedCourt(court);
}
} catch (error) {
console.error(error);
}
};

handleGetCurrentLocation();
fetchCourts();
}, [handleGetCurrentLocation, map]);
const { courtId } = router.query;

if (courtId) {
restoreCourts(+courtId);
} else {
handleGetCurrentLocation();
}
}, [map]);

useEffect(() => {
if (map) {
handleChangedMapBounds(map);
if (map && map?.getLevel() <= 4) {
fetchCourtsByBoundsAndDatetime(map);
}
}, [map, handleChangedMapBounds]);
}, [map, fetchCourtsByBoundsAndDatetime, level, center]);

return (
<>
Expand All @@ -261,7 +283,7 @@ const Courts: NextPage = () => {
<Map.KakaoMap
level={level}
center={center}
onDragEnd={handleChangedMapBounds}
onDragEnd={fetchCourtsByBoundsAndDatetime}
>
<Map.ZoomButton onZoomIn={handleZoomIn} onZoomOut={handleZoomOut} />
<Map.CurrentLocationButton
Expand All @@ -276,7 +298,6 @@ const Courts: NextPage = () => {
selectedSlot={selectedSlot}
onChange={handleChangeSlot}
/>

{map &&
courts &&
courts.map((court: any) => (
Expand Down

0 comments on commit 4ef7ea7

Please sign in to comment.