import { Dictionary } from "@types"; import { PageID, TabID } from "pages"; 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_DASHBOARD = { [makePathKey(PageID.DASHBOARD_OVERVIEW)]: "/dashboard", // --契約----------------------- [makePathKey(PageID.DASHBOARD_SEASON_TICKET_CONTRACT_ENTRY)]: "/dashboard/contract/entry", [makePathKey(PageID.DASHBOARD_SEASON_TICKET_CONTRACT_LIST)]: "/dashboard/contract/list/:page", [makePathKey(PageID.DASHBOARD_SEASON_TICKET_CONTRACT_DETAIL)]: "/dashboard/contract/detail/:id", [makePathKey(PageID.DASHBOARD_SEASON_TICKET_CONTRACT_STICKER_RE_ORDER)]: "/dashboard/contract/sticker-re-order", [makePathKey(PageID.DASHBOARD_SEASON_TICKET_CONTRACT_SEASON_TICKET_RE_ORDER)]: "/dashboard/contract/season-ticket-re-order", [makePathKey( PageID.DASHBOARD_SEASON_TICKET_CONTRACT_PARKING_CERTIFICATE_ORDER )]: "/dashboard/contract/parking-certificate-order", [makePathKey(PageID.DASHBOARD_SEASON_TICKET_CONTRACT_TERMINATE_ORDER)]: "/dashboard/contract/terminate-order", [makePathKey( PageID.DASHBOARD_SEASON_TICKET_CONTRACT_UPDATE_VEHICLE_INFO_ORDER )]: "/dashboard/contract/update-vehicle-info-order", [makePathKey(PageID.DASHBOARD_SEASON_TICKET_CONTRACT_CHANGE_PLAN)]: "/dashboard/contract/change-plan", [makePathKey(PageID.DASHBOARD_USER_STUDENT_LICENSE_IMAGES_UPLOAD)]: "/dashboard/contract/upload/student-license", [makePathKey(PageID.DASHBOARD_USER_OTHER_LICENSE_IMAGES_UPLOAD)]: "/dashboard/contract/upload/other-license", [makePathKey(PageID.DASHBOARD_RECEIPT_DOWNLOAD)]: "/dashboard/receipt/download", [makePathKey(PageID.DASHBOARD_USER_DETAIL)]: "/dashboard/user/detail", [makePathKey(PageID.DASHBOARD_ASK)]: "/dashboard/ask", [makePathKey(PageID.DASHBOARD_USER_CHANGE_EMAIL_START)]: "/dashboard/change/email/start", [makePathKey(PageID.DASHBOARD_USER_UPDATE_USER_INFO)]: "/dashboard/update/user/info", [makePathKey(PageID.DASHBOARD_USER_BANK_REGISTER)]: "/dashboard/update/user/back-register", }; const PATHS = { [makePathKey(PageID.NONE)]: "/", // 認証 [makePathKey(PageID.LOGIN)]: "/login", [makePathKey(PageID.LOGOUT)]: "/logout", [makePathKey(PageID.USER_CHANGE_EMAIL_VERIFY)]: "/change/email/verify/:token", [makePathKey(PageID.USER_SETTING_PASSWORD_START)]: "/setting/password/start", [makePathKey(PageID.USER_SETTING_PASSWORD_VERIFY)]: "/setting/password/verify/:token", // 申込/選考 [makePathKey(PageID.SEASON_TICKET_CONTRACT_SELECTION_ENTRY)]: "/season-ticket-contract/selection/entry/:selectionRecordNo/:entryRecordNo/:fs", [makePathKey(PageID.SEASON_TICKET_CONTRACT_ENTRY_CANCEL)]: "/season-ticket-contract/entry/cancel/:entryRecordNo/:fs", // ダッシュボード---------------- ...PATHS_DASHBOARD, // その他 [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) ); }