Skip to content

Commit

Permalink
refactor: save버튼이 active 상태일때 get요청 계속 보내고, 혹시 찜한 스토어가 없을 때 에러처리
Browse files Browse the repository at this point in the history
  • Loading branch information
thisishwarang committed Jan 21, 2025
1 parent 7d0f612 commit a17f682
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 5 deletions.
15 changes: 11 additions & 4 deletions src/apis/view/useFetchLikesStoreCoordinate.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { useSuspenseQuery } from '@tanstack/react-query';
import { useQuery } from '@tanstack/react-query';

import { instance } from '@apis/instance';

import { END_POINT, queryKey } from '@constants';

import {
ApiResponseType,
ErrorResponse,
StoreCoordinate,
StoreCoordinateListResponse,
} from '@types';
Expand All @@ -17,14 +18,20 @@ const fetchLikesStoreCoordinate = async (): Promise<StoreCoordinate[]> => {
>(END_POINT.FETCH_LIKES_STORE_COORDINATE_LIST);
return response.data.data.stores;
} catch (error) {
console.log(error);
const errorResponse = error as ErrorResponse;
if (errorResponse.response.data.code === 40402) {
return [];
}
throw error;
}
};

export const useFetchLikesStoreCoordinate = () => {
return useSuspenseQuery({
export const useFetchLikesStoreCoordinate = (isSaveActive: boolean) => {
return useQuery({
queryKey: [queryKey.LIKES_STORE_COORDINATE_LIST],
queryFn: () => fetchLikesStoreCoordinate(),
enabled: isSaveActive,
staleTime: 0,
gcTime: 0,
});
};
5 changes: 4 additions & 1 deletion src/pages/view/hooks/useKakaoMap.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ export const useKakaoMap = (
const { data: storeCoordinateList } = useFetchStoreCoordinateList(
currentLocation.stationEnName
);
const { data: likesStoreCoordinateList } = useFetchLikesStoreCoordinate();

const [selectedStoreId, setSelectedStoreId] = useState<number>(0);
const [storeMarkerList, setStoreMarkerList] = useState<
Expand All @@ -43,6 +42,9 @@ export const useKakaoMap = (
// gps 버튼 활성화 상태
const [isGpsActive, setIsGpsActive] = useState(false);

const { data: likesStoreCoordinateList } =
useFetchLikesStoreCoordinate(isSaveActive);

const fetchCurrentPosition = () => {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(
Expand Down Expand Up @@ -116,6 +118,7 @@ export const useKakaoMap = (

useEffect(() => {
if (currentLocation) {
setIsSaveActive(false);
if (currentLocation.stationEnName === 'ALL') {
fetchCurrentPosition();
} else {
Expand Down
9 changes: 9 additions & 0 deletions src/types/apis/commonType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,12 @@ export interface MutateResposneType {
code: number;
message: string;
}

export interface ErrorResponse {
response: {
data: {
code: number;
message: string;
};
};
}

0 comments on commit a17f682

Please sign in to comment.