Skip to content

Commit 4fe23fd

Browse files
committed
훅 분리, 리렌더링
1 parent ce2740a commit 4fe23fd

File tree

4 files changed

+97
-66
lines changed

4 files changed

+97
-66
lines changed

src/global/apis/func/index.ts

+21-2
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,18 @@
11
import { subwayFreshLineName } from '@/global/utils';
22
import axios, { AxiosError } from 'axios';
33
import { axiosInstance } from '../axiosInstance';
4-
import { SavedRoute, SearchPathsTypes, SearchStationNameTypes, SubwayLine } from '../entity';
4+
import {
5+
AddRouteTypes,
6+
SavedRoute,
7+
SearchPathsTypes,
8+
SearchStationNameTypes,
9+
SubwayLine,
10+
} from '../entity';
511
import { LoginFetchResponse } from '@/screens/loginScreen/apis/entity';
612
import { API_BASE_URL } from '@env';
713

814
/**
9-
* 인증 토근 재인증 axios
15+
* 인증 토큰 재인증 axios
1016
*/
1117
export const tokenReissueFetch = async ({
1218
accessToken,
@@ -166,3 +172,16 @@ export const getSavedRoutesFetch = async () => {
166172
throw er;
167173
}
168174
};
175+
176+
/**
177+
* 내 경로 저장 axios
178+
*/
179+
export const saveMyRoutesFetch = async (newRoute: object) => {
180+
try {
181+
const res = await axiosInstance.post('/api/v1/my_find_road/add_route', newRoute);
182+
return res;
183+
} catch (err) {
184+
const er = err as AxiosError;
185+
throw er;
186+
}
187+
};

src/global/apis/hook/index.ts

+18
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import {
22
deleteAccountFetch,
33
getSavedRoutesFetch,
44
getSearchRoutesFetch,
5+
saveMyRoutesFetch,
56
searchStationName,
67
} from '../func';
78
import { useMutation, useQuery } from 'react-query';
@@ -110,3 +111,20 @@ export const useGetSavedRoutesQuery = () => {
110111
const { data } = useQuery(['getRoads'], getSavedRoutesFetch);
111112
return { data };
112113
};
114+
115+
/**
116+
* 내 경로 저장 훅
117+
*/
118+
export const useSaveMyRoutesQuery = ({
119+
onSuccess,
120+
onError,
121+
}: {
122+
onSuccess: () => void;
123+
onError?: (error: any) => void;
124+
}) => {
125+
const { data, mutate } = useMutation(saveMyRoutesFetch, {
126+
onSuccess,
127+
onError,
128+
});
129+
return { data, mutate };
130+
};

src/screens/nameNewRouteScreen/index.tsx

+46-53
Original file line numberDiff line numberDiff line change
@@ -3,62 +3,55 @@ import { FontText, Input } from '@/global/ui';
33
import { COLOR } from '@/global/constants';
44
import React, { useState } from 'react';
55
import { useRootNavigation } from '@/navigation/RootNavigation';
6-
import { AxiosError } from 'axios';
7-
import { axiosInstance } from '@/global/apis/axiosInstance';
8-
import { AddRouteTypes } from '@/global/apis/entity';
96
import { useRoute } from '@react-navigation/native';
7+
import { useSaveMyRoutesQuery } from '@/global/apis/hook';
8+
import { useQueryClient } from 'react-query';
109

11-
interface SaveNewRouteProps {
12-
newRouteName: string | undefined;
13-
pathId: number | undefined;
14-
}
1510
const NameNewRouteScreen = () => {
1611
const { params } = useRoute();
1712
const { pathId } = params as { pathId: number };
1813
const navigation = useRootNavigation();
19-
const [newRouteName, setNewRouteName] = useState<string>();
14+
const [roadName, setRoadName] = useState<string>();
15+
const queryClient = useQueryClient();
2016

21-
const saveRoute = async ({ newRouteName, pathId }: SaveNewRouteProps) => {
22-
const newRoute = {
23-
roadName: newRouteName,
24-
totalTime: 0,
25-
subwayTransitCount: 0,
26-
firstStartStation: 'string',
27-
lastEndStation: 'string',
28-
subPaths: [
29-
{
30-
trafficType: 1,
31-
distance: 0,
32-
sectionTime: 0,
33-
stationCount: 0,
34-
lanes: [
35-
{
36-
name: 'string',
37-
subwayCode: 0,
38-
startName: 'string',
39-
endName: pathId,
40-
},
41-
],
42-
subways: [
43-
{
44-
index: pathId,
45-
stationName: '압구정',
46-
},
47-
],
48-
},
49-
],
50-
};
17+
const { mutate } = useSaveMyRoutesQuery({
18+
onSuccess: async () => {
19+
await queryClient.invalidateQueries();
20+
},
21+
onError: async (error: any) => {
22+
await queryClient.invalidateQueries();
23+
console.error(error);
24+
},
25+
});
5126

52-
try {
53-
const res = await axiosInstance.post<{ newRoute: AddRouteTypes }>(
54-
'/api/v1/my_find_road/add_route',
55-
newRoute,
56-
);
57-
return res;
58-
} catch (err) {
59-
const er = err as AxiosError;
60-
throw er;
61-
}
27+
const newRoute = {
28+
roadName: roadName,
29+
totalTime: 0,
30+
subwayTransitCount: 0,
31+
firstStartStation: 'string',
32+
lastEndStation: 'string',
33+
subPaths: [
34+
{
35+
trafficType: 1,
36+
distance: 0,
37+
sectionTime: 0,
38+
stationCount: 0,
39+
lanes: [
40+
{
41+
name: 'string',
42+
subwayCode: 0,
43+
startName: 'string',
44+
endName: pathId,
45+
},
46+
],
47+
subways: [
48+
{
49+
index: pathId,
50+
stationName: '압구정',
51+
},
52+
],
53+
},
54+
],
6255
};
6356

6457
return (
@@ -75,16 +68,16 @@ const NameNewRouteScreen = () => {
7568
<InputBox>
7669
<Input
7770
placeholder="경로 이름을 입력하세요"
78-
value={newRouteName}
79-
onChangeText={(text) => setNewRouteName(text)}
71+
value={roadName}
72+
onChangeText={(text) => setRoadName(text)}
8073
inputMode="email"
8174
maxLength={10}
8275
></Input>
8376
</InputBox>
8477

8578
<TextLengthBox>
8679
<FontText
87-
value={`${newRouteName?.length ? newRouteName.length : 0}/10`}
80+
value={`${roadName?.length ? roadName.length : 0}/10`}
8881
textSize="12px"
8982
textWeight="Regular"
9083
textColor={COLOR.GRAY_999}
@@ -94,10 +87,10 @@ const NameNewRouteScreen = () => {
9487

9588
<BottomBtn
9689
onPress={() => {
97-
saveRoute({ newRouteName, pathId });
90+
mutate(newRoute);
9891
navigation.popToTop();
9992
}}
100-
disabled={!newRouteName}
93+
disabled={!roadName}
10194
>
10295
<FontText
10396
value="완료"

src/screens/savedRoutesScreen/components/RenderSavedRoutes.tsx

+12-11
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,9 @@ import { View, StyleSheet } from 'react-native';
33
import { FontText, TextButton } from '@/global/ui';
44
import { COLOR } from '@/global/constants';
55
import SubwayRoute from '@/screens/selectNewRouteScreen/components/SubwayRoute';
6-
import {
7-
useDeleteSavedSubwayRoute,
8-
useGetSavedRoutesQuery,
9-
} from '@/global/apis/hook';
6+
import { useDeleteSavedSubwayRoute, useGetSavedRoutesQuery } from '@/global/apis/hook';
107
import MyTabModal from '@/global/components/MyTabModal';
8+
import { useQueryClient } from 'react-query';
119

1210
interface RenderSavedRoutesProps {
1311
id: number;
@@ -17,11 +15,14 @@ interface RenderSavedRoutesProps {
1715
const RenderSavedRoutes = () => {
1816
const [popupVisible, setPopupVisible] = useState<boolean>(false);
1917
const [routeToDelete, setRouteToDelete] = useState<number | null>(null);
18+
const queryClient = useQueryClient();
19+
2020
const { deleteMutate } = useDeleteSavedSubwayRoute({
21-
onSuccess: () => {
22-
// 리렌더링
21+
onSuccess: async () => {
22+
await queryClient.invalidateQueries();
2323
},
2424
});
25+
2526
const { data: savedRoutesData } = useGetSavedRoutesQuery();
2627

2728
const renderSavedRoutes = () =>
@@ -30,18 +31,18 @@ const RenderSavedRoutes = () => {
3031
<View style={styles.containerRenderTitle}>
3132
<FontText
3233
value={roadName}
33-
textSize="22px"
34-
textWeight="Bold"
35-
lineHeight="29px"
34+
textSize="18px"
35+
textWeight="SemiBold"
36+
lineHeight="23px"
3637
textColor={COLOR.BASIC_BLACK}
3738
/>
3839
<TextButton
3940
value="삭제"
40-
textSize="16px"
41+
textSize="13px"
4142
textColor={COLOR.GRAY_999}
4243
textWeight="Medium"
4344
onPress={() => showDeletePopup(id)}
44-
lineHeight="21px"
45+
lineHeight="19px"
4546
/>
4647
</View>
4748

0 commit comments

Comments
 (0)