Skip to content

Commit e9e05f0

Browse files
committed
feat: 현재 route에서 page id를 계산하는 함수 추가
1 parent f0a1408 commit e9e05f0

File tree

2 files changed

+40
-7
lines changed

2 files changed

+40
-7
lines changed

packages/common/src/utils/api.ts

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,51 @@
1+
import * as R from "remeda";
12

2-
import BackendAPISchemas from '../schemas';
3+
import BackendAPISchemas from "../schemas";
34

4-
export const buildNestedSiteMap: (flattened: BackendAPISchemas.FlattenedSiteMapSchema[]) => BackendAPISchemas.NestedSiteMapSchema[] = (flattened) => {
5+
export const buildNestedSiteMap: (
6+
flattened: BackendAPISchemas.FlattenedSiteMapSchema[]
7+
) => { [key: string]: BackendAPISchemas.NestedSiteMapSchema } = (flattened) => {
58
const map: Record<string, BackendAPISchemas.NestedSiteMapSchema> = {};
69
const roots: BackendAPISchemas.NestedSiteMapSchema[] = [];
710

11+
const siteMapIdRouteCodeMap = flattened.reduce((acc, item) => {
12+
acc[item.id] = item.route_code;
13+
return acc;
14+
}, {} as Record<string, string>);
15+
816
flattened.forEach((item) => {
917
map[item.id] = {
1018
...item,
11-
children: [],
19+
children: {},
1220
};
1321
});
1422

1523
flattened.forEach((item) => {
1624
if (item.parent_sitemap) {
17-
map[item.parent_sitemap].children.push(map[item.id]);
25+
map[item.parent_sitemap].children[siteMapIdRouteCodeMap[item.id]] = map[item.id];
1826
} else {
1927
roots.push(map[item.id]);
2028
}
2129
});
2230

23-
return roots;
24-
}
31+
return roots.reduce((acc, item) => {
32+
acc[item.route_code] = item;
33+
return acc;
34+
}, {} as Record<string, BackendAPISchemas.NestedSiteMapSchema>);
35+
};
36+
37+
export const findSiteMapUsingRoute = (route: string, siteMapData: BackendAPISchemas.NestedSiteMapSchema): BackendAPISchemas.NestedSiteMapSchema | null => {
38+
const currentRouteCodes = ['', ...route.split('/').filter((code) => !R.isEmpty(code))];
39+
40+
let currentSitemap: BackendAPISchemas.NestedSiteMapSchema | null | undefined = siteMapData.children[currentRouteCodes[0]];
41+
if (currentSitemap === undefined) return null;
42+
43+
for (const routeCode of currentRouteCodes.slice(1)) {
44+
if ((currentSitemap = currentSitemap.children[routeCode] || null) === null) {
45+
break;
46+
}
47+
}
48+
return currentSitemap;
49+
};
50+
51+
export const parseCss = (t: unknown): React.CSSProperties => (R.isString(t) && !R.isEmpty(t) && JSON.parse(t)) || {};

packages/common/src/utils/index.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
import { buildNestedSiteMap as _buildNestedSiteMap } from './api';
1+
import {
2+
buildNestedSiteMap as _buildNestedSiteMap,
3+
findSiteMapUsingRoute as _findSiteMapUsingRoute,
4+
parseCss as _parseCss,
5+
} from './api';
26
import { getCookie as _getCookie } from "./cookie";
37
import {
48
getFormValue as _getFormValue,
@@ -7,6 +11,8 @@ import {
711

812
namespace Utils {
913
export const buildNestedSiteMap = _buildNestedSiteMap;
14+
export const findSiteMapUsingRoute = _findSiteMapUsingRoute;
15+
export const parseCss = _parseCss;
1016
export const getCookie = _getCookie;
1117
export const isFormValid = _isFormValid;
1218
export const getFormValue = _getFormValue;

0 commit comments

Comments
 (0)