Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions src/api/getStation.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { apiClient } from '@/api/apiClient';
import type { StationType } from '@/types/StationType';

type ResponseType = {
data: {
id: number;
line_cd: number;
line_name: string;
station_cd: number;
station_name: string;
station_name_k: string;
direction_1?: string;
direction_2?: string;
}[];
error?: string;
};

export const getStation = async (searchStationName: string) => {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

返り値の型だけ定義したほうがよいかも!

try {
const response = await apiClient.get<ResponseType>(`/search?name=${searchStationName}`);
if (response.data.error) {
console.error(response.data.error);
return [];
}

const stationList = response.data.data.map((data): StationType => {
return {
id: data.id,
line_cd: data.line_cd,
station_cd: data.station_cd,
stationName: data.station_name,
stationLineName: data.line_name,
stationDirection: [data.direction_1, data.direction_2].filter((v) => v),
};
});

return stationList;
} catch (e) {
console.error(e);
return [];
}
};
23 changes: 3 additions & 20 deletions src/components/pages/change-stations/ChangeStations.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import SearchIcon from '@mui/icons-material/Search';
import React, { useEffect, useState } from 'react';
import styled from 'styled-components';
import { apiClient } from '@/api/apiClient';
import { getStation } from '@/api/getStation';
import { StationCard } from '@/components/common/settings/change-stations/StationCard';
import { StationDirectionModal } from '@/components/common/settings/change-stations/StationDirectionModal';
import { SettingHeader } from '@/components/common/settings/SettingHeader';
Expand Down Expand Up @@ -33,25 +33,8 @@ export const ChangeStations = () => {
if (!searchStationName) return setStationsList([]);

const timeoutId = setTimeout(async () => {
try {
const responseData = (
await apiClient.get<ResponseStationType>(`/search?name=${searchStationName}`)
).data;
console.log('responseData:', responseData);
const newStationList = responseData.data.map((data): StationType => {
return {
id: data.id,
line_cd: data.line_cd,
station_cd: data.station_cd,
stationName: data.station_name,
stationLineName: data.line_name,
stationDirection: [data.direction_1, data.direction_2].filter((v) => v),
};
});
setStationsList(newStationList);
} catch (error) {
console.log(error);
}
const newStationList = await getStation(searchStationName);
setStationsList(newStationList);
}, 500);

return () => {
Expand Down