Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: 서버 주소 추가 및 서버가 주는 정보에 맞춰 코드 변경한다 #163

Merged
merged 6 commits into from
Jul 21, 2023
Merged
Show file tree
Hide file tree
Changes from 4 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
3 changes: 2 additions & 1 deletion frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
"main": "index.js",
"scripts": {
"test": "jest",
"start": "webpack-dev-server --open --mode development --hot --port 3000",
"dev": "webpack-dev-server --open --mode development --hot --port 3000",
"start": "webpack-dev-server --open --mode production --hot --port 3000",
"build": "webpack --mode production",
"storybook": "storybook dev -p 6006",
"build-storybook": "storybook build",
Expand Down
15 changes: 15 additions & 0 deletions frontend/src/constants/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,18 @@ export const DEFAULT_CENTER = {
} as const;

export const INITIAL_ZOOM_SIZE = 14;

export const INVALID_VALUE_LIST = ['null', '.', '..', '1', '#'] as const;

export const DEVELOP_URL = 'http://localhost:8080/api';
export const PRODUCTION_URL = 'http://1.1.1.1:8080/api';

type ModeType = 'development' | 'production';

const URL: Readonly<Record<ModeType, string>> = {
development: DEVELOP_URL,
production: PRODUCTION_URL,
};

const MODE = process.env.NODE_ENV as ModeType;
export const BASE_URL = URL[MODE];
16 changes: 13 additions & 3 deletions frontend/src/hooks/useSelectedStation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,27 @@ import { useExternalValue } from '@utils/external-state';

import { selectedStationIdStore } from '@stores/selectedStationStore';

import { BASE_URL, INVALID_VALUE_LIST } from '@constants';

import type { StationDetails } from 'types';

export const fetchStationDetails = async (selectedStationId: number) => {
const stationDetails = await fetch(`/stations/${selectedStationId}`, {
const stationDetails = await fetch(`${BASE_URL}/stations/${selectedStationId}`, {
method: 'GET',
}).then<StationDetails>((response) => {
}).then<StationDetails>(async (response) => {
if (!response.ok) {
throw new Error('[error] 충전소 세부 정보를 불러올 수 없습니다.');
Copy link
Collaborator

Choose a reason for hiding this comment

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

상수로 분리해주세요

}

return response.json();
const data: StationDetails = await response.json();

const changedDataList = Object.entries(data).map(([key, value]) => {
if (INVALID_VALUE_LIST.includes(value)) value = null;
Copy link
Member

Choose a reason for hiding this comment

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

중괄호 어디감

Copy link
Member

Choose a reason for hiding this comment

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

습니까?

Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
if (INVALID_VALUE_LIST.includes(value)) value = null;
if (INVALID_VALUE_LIST.includes(value)) {
return [key, null];
}

요게 더 좋지 않을까 하는 생각입니다!


return [key, value];
});

return Object.fromEntries(changedDataList);
});

return stationDetails;
Expand Down
4 changes: 3 additions & 1 deletion frontend/src/hooks/useStations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import { getDisplayPosition } from '@utils/google-maps';

import { stationFilterStore } from '@stores/stationFilterStore';

import { BASE_URL } from '@constants';

import type { StationSummary } from 'types';

export const fetchStation = async (map: google.maps.Map) => {
Expand All @@ -15,7 +17,7 @@ export const fetchStation = async (map: google.maps.Map) => {

const displayPositionParams = String(new URLSearchParams(displayPositionString));

const stations = await fetch(`/stations?${displayPositionParams}`, {
const stations = await fetch(`${BASE_URL}/stations?${displayPositionParams}`, {
method: 'GET',
}).then<StationSummary[]>((response) => response.json());

Expand Down
6 changes: 4 additions & 2 deletions frontend/src/mocks/handlers.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import { rest } from 'msw';

import { DEVELOP_URL } from '@constants';

import { stations } from './data';

import type { StationSummary } from 'types';

export const handlers = [
rest.get('/stations', async (req, res, ctx) => {
rest.get(`${DEVELOP_URL}/stations`, async (req, res, ctx) => {
const { searchParams } = req.url;

const latitude = Number(searchParams.get('latitude'));
Expand Down Expand Up @@ -46,7 +48,7 @@ export const handlers = [
return res(ctx.delay(200), ctx.status(200), ctx.json(foundStations));
}),

rest.get('/stations/:id', async (req, res, ctx) => {
rest.get(`${DEVELOP_URL}/stations/:id`, async (req, res, ctx) => {
const stationId = Number(req.params.id);
const selectedStation = stations.find((station) => station.stationId === stationId);

Expand Down
Loading