Skip to content

Commit 71cee4a

Browse files
committed
feat: API에 언어 지원을 추가
1 parent a4deb78 commit 71cee4a

File tree

5 files changed

+22
-9
lines changed

5 files changed

+22
-9
lines changed

packages/common/src/apis/client.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ export class BackendAPIClientError extends Error {
5959
}
6060
}
6161

62+
type supportedLanguages = "ko" | "en";
63+
6264
type AxiosRequestWithoutPayload = <T = unknown, R = AxiosResponse<T>, D = unknown>(
6365
url: string,
6466
config?: AxiosRequestConfig<D>
@@ -70,6 +72,7 @@ type AxiosRequestWithPayload = <T = unknown, R = AxiosResponse<T>, D = unknown>(
7072
) => Promise<R>;
7173

7274
export class BackendAPIClient {
75+
readonly language: supportedLanguages;
7376
readonly baseURL: string;
7477
protected readonly csrfCookieName: string;
7578
private readonly backendAPI: AxiosInstance;
@@ -78,9 +81,14 @@ export class BackendAPIClient {
7881
baseURL: string,
7982
timeout: number,
8083
csrfCookieName: string = "csrftoken",
81-
withCredentials: boolean = false
84+
withCredentials: boolean = false,
85+
language: supportedLanguages = "ko"
8286
) {
83-
const headers = { "Content-Type": "application/json" };
87+
const headers = {
88+
"Content-Type": "application/json",
89+
"Accept-Language": language,
90+
};
91+
this.language = language;
8492
this.baseURL = baseURL;
8593
this.csrfCookieName = csrfCookieName;
8694
this.backendAPI = axios.create({

packages/common/src/apis/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
import { BackendAPIClient } from "./client";
1+
import { BackendAPIClient, BackendAPIClientError as _BackendAPIClientError } from "./client";
22
import BackendAPISchemas from "../schemas/backendAPI";
33

44
namespace BackendAPIs {
5+
export const BackendAPIClientError = _BackendAPIClientError;
56
export const listSiteMaps = (client: BackendAPIClient) => () =>
67
client.get<BackendAPISchemas.FlattenedSiteMapSchema[]>("v1/cms/sitemap/");
78
export const retrievePage = (client: BackendAPIClient) => (id: string) =>

packages/common/src/components/mdx_components/map.tsx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,14 @@ import { Box, Button, Stack, Tab, Tabs } from "@mui/material";
22
import * as React from "react";
33
import { renderToStaticMarkup } from "react-dom/server";
44

5+
import { useCommonContext } from "../../hooks/useCommonContext";
6+
57
type SupportedMapType = "kakao" | "google" | "naver";
68
const MAP_TYPES: SupportedMapType[] = ["kakao", "google", "naver"];
79

810
type LangType = "ko" | "en";
911

1012
export type MapPropType = {
11-
language: LangType;
1213
geo: {
1314
lat: number;
1415
lng: number;
@@ -54,7 +55,8 @@ const MapData: { [key in SupportedMapType]: MapDataType } = {
5455
},
5556
};
5657

57-
export const Map: React.FC<MapPropType> = ({ language, geo, placeName, placeCode, googleMapIframeSrc }) => {
58+
export const Map: React.FC<MapPropType> = ({ geo, placeName, placeCode, googleMapIframeSrc }) => {
59+
const { language } = useCommonContext();
5860
const kakaoMapRef = React.useRef<HTMLDivElement>(null);
5961
const [mapState, setMapState] = React.useState<MapStateType>({ tab: 0 });
6062
const selectedMapType = MAP_TYPES[mapState.tab] || "kakao";

packages/common/src/contexts/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import * as React from "react";
33

44
namespace GlobalContext {
55
export type ContextOptions = {
6+
language: "ko" | "en";
67
frontendDomain?: string;
78
baseUrl: string;
89
debug?: boolean;
@@ -13,6 +14,7 @@ namespace GlobalContext {
1314
};
1415

1516
export const context = React.createContext<ContextOptions>({
17+
language: "ko",
1618
frontendDomain: "",
1719
baseUrl: "",
1820
debug: false,

packages/common/src/hooks/useAPI.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,19 @@ namespace BackendAPIHooks {
1818
};
1919

2020
export const useBackendClient = () => {
21-
const { backendApiDomain, backendApiTimeout } = useBackendContext();
22-
return new BackendAPIClient(backendApiDomain, backendApiTimeout);
21+
const { language, backendApiDomain, backendApiTimeout } = useBackendContext();
22+
return new BackendAPIClient(backendApiDomain, backendApiTimeout, "", false, language);
2323
};
2424

2525
export const useFlattenSiteMapQuery = (client: BackendAPIClient) =>
2626
useSuspenseQuery({
27-
queryKey: QUERY_KEYS.SITEMAP_LIST,
27+
queryKey: [client.language, ...QUERY_KEYS.SITEMAP_LIST],
2828
queryFn: BackendAPIs.listSiteMaps(client),
2929
});
3030

3131
export const usePageQuery = (client: BackendAPIClient, id: string) =>
3232
useSuspenseQuery({
33-
queryKey: [...QUERY_KEYS.PAGE, id],
33+
queryKey: [client.language, ...QUERY_KEYS.PAGE, id],
3434
queryFn: () => BackendAPIs.retrievePage(client)(id),
3535
});
3636
}

0 commit comments

Comments
 (0)