Skip to content

Commit 7273c40

Browse files
committed
지하철역 검색 백엔드 스키마 적용
1 parent 294d474 commit 7273c40

File tree

8 files changed

+100
-70
lines changed

8 files changed

+100
-70
lines changed

src/global/apis/entity/index.ts

+13-4
Original file line numberDiff line numberDiff line change
@@ -56,16 +56,25 @@ export type SubwayLine =
5656
| '용인경전철'
5757
| '의정부경전철'
5858
| '우이신설경전철'
59-
| '김포도시철도';
59+
| '김포도시철도'
60+
| null;
61+
62+
/**
63+
* 지하철 검색 이력 타입
64+
*/
65+
export interface SearchHistoryStationNameTypes {
66+
id: number;
67+
stationName: string;
68+
stationLine: SubwayLine;
69+
}
6070

6171
/**
6272
* 지하철 검색 타입
6373
*/
6474
export interface SearchStationNameTypes {
6575
data: {
66-
id?: number;
67-
stationName: string;
68-
stationLine: SubwayLine;
76+
name: string;
77+
line: SubwayLine;
6978
}[];
7079
}
7180

src/global/apis/func/index.ts

+21-12
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
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+
SavedRoute,
6+
SearchHistoryStationNameTypes,
7+
SearchPathsTypes,
8+
SearchStationNameTypes,
9+
SubwayLine,
10+
} from '../entity';
511
import { LoginFetchResponse, LogoutFetchData } from '@/screens/loginScreen/apis/entity';
612
import { API_BASE_URL } from '@env';
713

@@ -80,8 +86,10 @@ export const searchStationName = async (params: { stationName: string }) => {
8086
*/
8187
export const searchHistoryFetch = async () => {
8288
try {
83-
const res = await axiosInstance.get<SearchStationNameTypes>('/api/v1/recentSearch');
84-
return res.data;
89+
const res = await axiosInstance.get<{ data: SearchHistoryStationNameTypes[] }>(
90+
'/api/v1/recentSearch',
91+
);
92+
return res.data.data;
8593
} catch (err) {
8694
const er = err as AxiosError;
8795
throw er;
@@ -91,16 +99,17 @@ export const searchHistoryFetch = async () => {
9199
/**
92100
* 지하철역 검색 히스토리 저장 axios
93101
*/
94-
export const searchAddHistoryFetch = async (data: { stationName: string; stationLine: string }) => {
102+
export const searchAddHistoryFetch = async (data: {
103+
stationName: string;
104+
stationLine: SubwayLine;
105+
}) => {
95106
try {
96-
const res = await axiosInstance.post<{
97-
// 백엔드 : stationCode 대응
98-
id?: number;
99-
stationName: string;
100-
stationLine: SubwayLine;
101-
}>('/api/v1/recentSearch/add', data);
102-
103-
return subwayFreshLineName([res.data])[0];
107+
const res = await axiosInstance.post<{ data: SearchHistoryStationNameTypes }>(
108+
'/api/v1/recentSearch/add',
109+
data,
110+
);
111+
console.log(res.data);
112+
return res.data.data;
104113
} catch (err) {
105114
const er = err as AxiosError;
106115
throw er;

src/global/apis/hook/index.ts

+7-10
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import {
1010
searchPathsFetch,
1111
} from '@/global/apis/func';
1212
import { subwayFreshLineName } from '@/global/utils';
13-
import { SubwayStrEnd } from '../entity';
13+
import { SubwayLine, SubwayStrEnd } from '../entity';
1414

1515
export const useQuitMutation = ({ onSuccess }: { onSuccess: () => void }) => {
1616
const { mutate: quitMutate } = useMutation(quitFetch, { onSuccess });
@@ -114,19 +114,16 @@ export const useDeleteSavedSubwayRoute = ({ onSuccess }: { onSuccess: () => void
114114

115115
return { data, deleteMutate: mutate };
116116
};
117-
export const useAddResetSearch = ({
117+
export const useAddRecentSearch = ({
118118
onSuccess,
119119
}: {
120-
onSuccess: (data: {
121-
// 백엔드 : stationCode 대응
122-
id?: number;
123-
stationName: string;
124-
stationLine: string;
125-
}) => void;
120+
onSuccess: (data: { id: number; stationName: string; stationLine: SubwayLine }) => void;
126121
}) => {
127122
const { data, mutate } = useMutation(searchAddHistoryFetch, {
128123
onSuccess,
129124
});
130-
131-
return { data, addRecentMutate: mutate };
125+
const freshData = data
126+
? { line: data.stationLine, name: data.stationName }
127+
: { line: null, name: '' };
128+
return { data: subwayFreshLineName([freshData]), addRecentMutate: mutate };
132129
};

src/global/components/SwapSubwayStation.tsx

+14-6
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,13 @@ const SwapSubwayStation = ({ isWrap, showHeader }: SwapProps) => {
4444
setSubwayStation(({ departure, arrival }) => ({
4545
departure: {
4646
...arrival,
47-
name: arrival.name === ARRIVAL_STATION ? DEPARTURE_STATION : arrival.name,
47+
stationName:
48+
arrival.stationName === ARRIVAL_STATION ? DEPARTURE_STATION : arrival.stationName,
4849
},
4950
arrival: {
5051
...departure,
51-
name: departure.name === DEPARTURE_STATION ? ARRIVAL_STATION : departure.name,
52+
stationName:
53+
departure.stationName === DEPARTURE_STATION ? ARRIVAL_STATION : departure.stationName,
5254
},
5355
}));
5456
};
@@ -68,19 +70,25 @@ const SwapSubwayStation = ({ isWrap, showHeader }: SwapProps) => {
6870
>
6971
<InnerBox>
7072
<StationButton
71-
value={subwayStation.departure.name ? subwayStation.departure.name : DEPARTURE_STATION}
73+
value={
74+
subwayStation.departure.stationName
75+
? subwayStation.departure.stationName
76+
: DEPARTURE_STATION
77+
}
7278
textSize="16px"
7379
textWeight="Regular"
7480
lineHeight="21px"
75-
textColor={subwayStation.departure.name ? COLOR.BASIC_BLACK : COLOR.GRAY_999}
81+
textColor={subwayStation.departure.stationName ? COLOR.BASIC_BLACK : COLOR.GRAY_999}
7682
onPress={() => navigateSubwaySearch(DEPARTURE_STATION)}
7783
/>
7884
<StationButton
79-
value={subwayStation.arrival.name ? subwayStation.arrival.name : ARRIVAL_STATION}
85+
value={
86+
subwayStation.arrival.stationName ? subwayStation.arrival.stationName : ARRIVAL_STATION
87+
}
8088
textSize="16px"
8189
textWeight="Regular"
8290
lineHeight="21px"
83-
textColor={subwayStation.arrival.name ? COLOR.BASIC_BLACK : COLOR.GRAY_999}
91+
textColor={subwayStation.arrival.stationName ? COLOR.BASIC_BLACK : COLOR.GRAY_999}
8492
onPress={() => navigateSubwaySearch(ARRIVAL_STATION)}
8593
/>
8694
</InnerBox>

src/global/utils/subwayLine.ts

+13-8
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
import { COLOR } from '@/global/constants';
2-
import { SearchStationNameTypes, SubwayCode } from '../apis/entity';
2+
import {
3+
SearchHistoryStationNameTypes,
4+
SearchStationNameTypes,
5+
SubwayCode,
6+
SubwayLine,
7+
} from '../apis/entity';
38

49
// 인천선 === 인천1
510
// 인천2호선 === 인천2
@@ -12,19 +17,19 @@ import { SearchStationNameTypes, SubwayCode } from '../apis/entity';
1217
*/
1318
export const subwayFreshLineName = (list: SearchStationNameTypes['data']) => {
1419
return list.map((item) => {
15-
switch (item.stationLine) {
20+
switch (item.line) {
1621
case '인천선':
17-
return { stationName: item.stationName, stationLine: '인천1' };
22+
return { stationName: item.name, stationLine: '인천1' as SubwayLine };
1823
case '인천2호선':
19-
return { stationName: item.stationName, stationLine: '인천2' };
24+
return { stationName: item.name, stationLine: '인천2' as SubwayLine };
2025
case '용인경전철':
21-
return { stationName: item.stationName, stationLine: '애버라인' };
26+
return { stationName: item.name, stationLine: '애버라인' as SubwayLine };
2227
case '우이신설경전철':
23-
return { stationName: item.stationName, stationLine: '우이신설' };
28+
return { stationName: item.name, stationLine: '우이신설' as SubwayLine };
2429
case '김포도시철도':
25-
return { stationName: item.stationName, stationLine: '김포골드' };
30+
return { stationName: item.name, stationLine: '김포골드' as SubwayLine };
2631
default:
27-
return item;
32+
return { stationName: item.name, stationLine: item.line as SubwayLine };
2833
}
2934
});
3035
};

src/navigation/types/navigation.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Path, SubPath } from '@/global/apis/entity';
1+
import { Path, SubPath, SubwayLine } from '@/global/apis/entity';
22

33
export type RootStackParamList = {
44
Temp: undefined;
@@ -14,8 +14,8 @@ export type EditRouteStackParamList = {
1414
AddNewRoute: undefined;
1515
SubwaySearch: { isBackBtn: boolean };
1616
SubwayPathResult: {
17-
departure: { name: string; line: string };
18-
arrival: { name: string; line: string };
17+
departure: { stationName: string; stationLine: SubwayLine };
18+
arrival: { stationName: string; stationLine: SubwayLine };
1919
};
2020
SubwayPathDetail: undefined;
2121
NameNewRoute: { screen: string; params: number };
@@ -24,8 +24,8 @@ export type EditRouteStackParamList = {
2424
export type SearchStackParamList = {
2525
SubwaySearch: { isBackBtn: boolean };
2626
SubwayPathResult: {
27-
departure: { name: string; line: string };
28-
arrival: { name: string; line: string };
27+
departure: { stationName: string; stationLine: SubwayLine };
28+
arrival: { stationName: string; stationLine: SubwayLine };
2929
};
3030
SubwayPathDetail: { state?: SubPath[]; pathId?: number };
3131
};

src/screens/subwaySearchScreen/components/SearchResultList.tsx

+20-19
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@ import { useSearchNavigation } from '@/navigation/SearchNavigation';
99
import { useAppDispatch, useAppSelect } from '@/store';
1010
import { getSeletedStation } from '@/store/modules';
1111
import { getSearchText } from '@/store/modules/subwaySearchModule';
12-
import { useAddResetSearch, useSearchStationName } from '@/global/apis/hook';
13-
import { SearchStationNameTypes } from '@/global/apis/entity';
12+
import { useAddRecentSearch, useSearchStationName } from '@/global/apis/hook';
13+
import { SearchHistoryStationNameTypes, SubwayLine } from '@/global/apis/entity';
1414

1515
interface SearchResultListProps {
16-
historyList: SearchStationNameTypes;
16+
historyList: SearchHistoryStationNameTypes[];
1717
}
1818

1919
const SearchResultList = ({ historyList }: SearchResultListProps) => {
@@ -25,16 +25,16 @@ const SearchResultList = ({ historyList }: SearchResultListProps) => {
2525
);
2626

2727
const { searchResultData } = useSearchStationName(searchText);
28-
const { addRecentMutate } = useAddResetSearch({
29-
onSuccess: (data) => {
28+
const { addRecentMutate } = useAddRecentSearch({
29+
onSuccess: ({ stationLine, stationName }) => {
3030
saveStationData({
31-
name: data.stationName,
32-
line: data.stationLine,
31+
stationName,
32+
stationLine,
3333
});
3434
},
3535
});
3636

37-
const saveStationData = (data: { name: string; line: string }) => {
37+
const saveStationData = (data: { stationName: string; stationLine: SubwayLine }) => {
3838
dispatch(getSearchText(''));
3939
if (stationType === '출발역') {
4040
dispatch(
@@ -43,7 +43,7 @@ const SearchResultList = ({ historyList }: SearchResultListProps) => {
4343
stationData: data,
4444
}),
4545
);
46-
selectedStation.arrival.name
46+
selectedStation.arrival.stationName
4747
? searchNavigation.navigate('SubwayPathResult', {
4848
departure: data,
4949
arrival: selectedStation.arrival,
@@ -56,7 +56,7 @@ const SearchResultList = ({ historyList }: SearchResultListProps) => {
5656
stationData: data,
5757
}),
5858
);
59-
selectedStation.departure.name
59+
selectedStation.departure.stationName
6060
? searchNavigation.navigate('SubwayPathResult', {
6161
departure: selectedStation.departure,
6262
arrival: data,
@@ -66,6 +66,7 @@ const SearchResultList = ({ historyList }: SearchResultListProps) => {
6666
};
6767

6868
const stationBtnHandler = ({ stationName, stationLine }: (typeof searchResultData)[0]) => {
69+
if (!stationLine) return;
6970
addRecentMutate({ stationName, stationLine });
7071
};
7172

@@ -84,27 +85,27 @@ const SearchResultList = ({ historyList }: SearchResultListProps) => {
8485
</Header>
8586

8687
<Ul marginTop="18px">
87-
{historyList.data.map((history) => (
88+
{historyList.map(({ id, stationName, stationLine }) => (
8889
<Li
89-
key={history.id}
90+
key={id}
9091
onPress={() =>
91-
saveStationData({
92-
name: history.stationName,
93-
line: history.stationLine,
92+
stationBtnHandler({
93+
stationName,
94+
stationLine,
9495
})
9596
}
9697
>
9798
<Icon name="clock" size={25} color={COLOR.BE_GRAY} />
9899
<StationInfoBox>
99100
<FontText
100-
value={history.stationName}
101+
value={stationName}
101102
textSize="16px"
102103
textWeight="Medium"
103104
lineHeight="21px"
104105
textColor="#000"
105106
/>
106107
<FontText
107-
value={history.stationLine}
108+
value={stationLine!}
108109
textSize="14px"
109110
textWeight="Regular"
110111
lineHeight="21px"
@@ -123,7 +124,7 @@ const SearchResultList = ({ historyList }: SearchResultListProps) => {
123124
{/* 입력어가 있고 && 검색 결과가 있으면 결과 표시 */}
124125
{/* 입력어가 있고 && 검색 결과가 없으면 없음 표시 */}
125126
<Ul marginTop="28px">
126-
{searchResultData.map(({ stationLine, stationName }, idx) => (
127+
{searchResultData.map(({ stationName, stationLine }, idx) => (
127128
<Li key={idx} onPress={() => stationBtnHandler({ stationLine, stationName })}>
128129
<LocateIcon source={iconPath['location_pin_gray']} width={25} height={25} />
129130
<StationInfoBox>
@@ -135,7 +136,7 @@ const SearchResultList = ({ historyList }: SearchResultListProps) => {
135136
textColor="#000"
136137
/>
137138
<FontText
138-
value={stationLine}
139+
value={stationLine!}
139140
textSize="14px"
140141
textWeight="Regular"
141142
lineHeight="21px"

src/store/modules/subwaySearchModule.ts

+7-6
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1+
import { SubwayLine } from '@/global/apis/entity';
12
import { createSlice } from '@reduxjs/toolkit';
23
import type { PayloadAction } from '@reduxjs/toolkit';
34

45
export interface StationDataTypes {
5-
name: string;
6-
line: string;
6+
stationName: string;
7+
stationLine: SubwayLine;
78
}
89

910
interface InitialStateTypes {
@@ -20,12 +21,12 @@ const initialState: InitialStateTypes = {
2021
searchText: '',
2122
selectedStation: {
2223
departure: {
23-
name: '',
24-
line: '',
24+
stationName: '',
25+
stationLine: null,
2526
},
2627
arrival: {
27-
name: '',
28-
line: '',
28+
stationName: '',
29+
stationLine: null,
2930
},
3031
},
3132
};

0 commit comments

Comments
 (0)