|
- import { Dictionary } from "@types";
- import { PageID, TabID } from "codes/page";
- import { get, isArray, isString, replace } from "lodash";
-
- type PathKey = [PageID, TabID?] | PageID;
- const makePathKey = (arg: PathKey): string => {
- if (isArray(arg)) {
- const tabStr = arg[1] !== undefined ? "/" + String(arg[1]) : "";
- return String(arg[0]) + tabStr;
- } else {
- return String(arg);
- }
- };
-
- const getPageId = (key: PathKey): PageID => {
- if (isArray(key)) {
- return key[0];
- } else {
- return key;
- }
- };
-
- const getTabId = (key: PathKey): TabID => {
- if (isArray(key)) {
- return key[1] ?? TabID.NONE;
- } else {
- return TabID.NONE;
- }
- };
-
- const PATHS = {
- [makePathKey(PageID.NONE)]: "/",
-
- // 認証
- [makePathKey(PageID.LOGIN)]: "/login",
- [makePathKey(PageID.LOGOUT)]: "/logout",
-
- [makePathKey(PageID.DASHBOARD_OVERVIEW)]: "/dashboard",
-
- // 契約関連
- [makePathKey(PageID.DASHBOARD_CONTRACT_LIST)]:
- "/dashboard/contract/list/:page",
- [makePathKey(PageID.DASHBOARD_CONTRACT_DETAIL)]: "/dashboard/contract/detail",
-
- // 領収証発行依頼関連
- [makePathKey(PageID.DASHBOARD_RECEIPT_ISSUING_ORDER_CREATE)]:
- "/dashboard/receipt-issusing-order/create",
- [makePathKey(PageID.DASHBOARD_RECEIPT_ISSUING_ORDER_LIST)]:
- "/dashboard/receipt-issusing-order/list/:page",
- [makePathKey(PageID.DASHBOARD_RECEIPT_ISSUING_ORDER_DETAIL)]:
- "/dashboard/receipt-issusing-order/detail",
-
- // その他
- [makePathKey(PageID.PAGE_403)]: "403",
- [makePathKey(PageID.PAGE_404)]: "404",
- };
-
- export type PathOption = {
- page?: number;
- query?: Dictionary;
- };
- export function getPath(key: PathKey, option?: PathOption) {
- const pageId = getPageId(key);
- const tabId = getTabId(key);
-
- let path = getRoute(pageId);
-
- // ページ番号解決
- path = replacePathParam(path, "page", option?.page ?? 0);
-
- // その他URLパラメータ変換
- if (option?.query !== undefined) {
- Object.keys(option.query).forEach((key) => {
- const value = get(option.query, key);
- if (value === undefined) return;
-
- path = replacePathParam(path, key, value);
- });
- }
-
- return path;
- }
-
- export function getListPagePath(key: PathKey, page: number): string {
- return getPath(key, { page });
- }
-
- export function getRoute(key: PathKey, exclude?: string): string {
- let path = get(PATHS, makePathKey(key));
- if (!path) throw new Error("ルート未定義:" + makePathKey(key));
-
- if (exclude) {
- path = replace(path, "/" + exclude + "/", "");
- }
-
- return path;
- }
-
- function replacePathParam(
- sourceStr: string,
- searchStr: string,
- replacement: string | number
- ): string {
- return replace(
- sourceStr,
- ":" + searchStr,
- isString(replacement) ? replacement : String(replacement)
- );
- }
|