You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

142 lines
3.9KB

  1. import { Dictionary } from "@types";
  2. import { PageID, TabID } from "codes/page";
  3. import { get, isArray, isString, replace } from "lodash";
  4. type PathKey = [PageID, TabID?] | PageID;
  5. const makePathKey = (arg: PathKey): string => {
  6. if (isArray(arg)) {
  7. const tabStr = arg[1] !== undefined ? "/" + String(arg[1]) : "";
  8. return String(arg[0]) + tabStr;
  9. } else {
  10. return String(arg);
  11. }
  12. };
  13. const getPageId = (key: PathKey): PageID => {
  14. if (isArray(key)) {
  15. return key[0];
  16. } else {
  17. return key;
  18. }
  19. };
  20. const getTabId = (key: PathKey): TabID => {
  21. if (isArray(key)) {
  22. return key[1] ?? TabID.NONE;
  23. } else {
  24. return TabID.NONE;
  25. }
  26. };
  27. const PATHS = {
  28. [makePathKey(PageID.NONE)]: "/",
  29. // 認証
  30. [makePathKey(PageID.LOGIN)]: "/login",
  31. [makePathKey(PageID.LOGOUT)]: "/logout",
  32. [makePathKey(PageID.CLEAR_CHANGE_CONTRACT)]: "/clear-change-contract",
  33. [makePathKey(PageID.DASHBOARD_OVERVIEW)]: "/dashboard",
  34. // APP
  35. [makePathKey(PageID.APP_RECEIPT_ISSUING_ORDER_INDEX)]:
  36. "/app/receipt-issuing-order/:token",
  37. [makePathKey(PageID.APP_RECEIPT_ISSUING_ORDER_MAIL_ORDER)]:
  38. "/app/receipt-issuing-order/mail",
  39. [makePathKey(PageID.APP_RECEIPT_ISSUING_ORDER_EMAIL_ORDER)]:
  40. "/app/receipt-issuing-order/email",
  41. // 契約関連
  42. [makePathKey(PageID.DASHBOARD_CONTRACT_LIST)]:
  43. "/dashboard/contract/list/:page",
  44. [makePathKey(PageID.DASHBOARD_CONTRACT_DETAIL)]:
  45. "/dashboard/contract/detail/:id",
  46. [makePathKey(PageID.DASHBOARD_CONTRACT_CREATE)]: "/dashboard/contract/create",
  47. // 利用実績関連
  48. [makePathKey(PageID.DASHBOARD_USE_SUMMARY_LIST_BY_CONTRACT)]:
  49. "/dashboard/use-summary/list",
  50. // 領収証発行依頼関連
  51. [makePathKey(
  52. PageID.DASHBOARD_RECEIPT_ISSUING_ORDER_CREATE_CUSTOM_HELLO_TECHNO
  53. )]: "/dashboard/receipt-issusing-order/create",
  54. [makePathKey(
  55. PageID.DASHBOARD_RECEIPT_ISSUING_ORDER_LIST_CUSTOM_HELLO_TECHNO
  56. )]: "/dashboard/receipt-issusing-order/list/:page",
  57. [makePathKey(
  58. PageID.DASHBOARD_RECEIPT_ISSUING_ORDER_DETAIL_CUSTOM_HELLO_TECHNO
  59. )]: "/dashboard/receipt-issusing-order/detail/:id",
  60. // 利用実績関連
  61. [makePathKey(PageID.DASHBOARD_USE_SUMMARY_LIST_CUSTOM_HELLO_TECHNO)]:
  62. "/dashboard/use-summary/hello-techno/list",
  63. [makePathKey(PageID.DASHBOARD_USE_SUMMARY_DETAIL_CUSTOM_HELLO_TECHNO)]:
  64. "/dashboard/use-summary/hello-techno/detail",
  65. // ログインユーザ管理
  66. [makePathKey(PageID.DASHBOARD_LOGIN_USER_LIST)]:
  67. "/dashboard/login-user/list/:page",
  68. [makePathKey(PageID.DASHBOARD_LOGIN_USER_CREATE)]:
  69. "/dashboard/login-user/create",
  70. [makePathKey(PageID.DASHBOARD_LOGIN_USER_CHANGE_PASSWORD)]:
  71. "/dashboard/login-user/change-password/:id",
  72. // その他
  73. [makePathKey(PageID.PAGE_403)]: "403",
  74. [makePathKey(PageID.PAGE_404)]: "404",
  75. };
  76. export type PathOption = {
  77. page?: number;
  78. query?: Dictionary;
  79. };
  80. export function getPath(key: PathKey, option?: PathOption) {
  81. const pageId = getPageId(key);
  82. const tabId = getTabId(key);
  83. let path = getRoute(pageId);
  84. // ページ番号解決
  85. path = replacePathParam(path, "page", option?.page ?? 0);
  86. // その他URLパラメータ変換
  87. if (option?.query !== undefined) {
  88. Object.keys(option.query).forEach((key) => {
  89. const value = get(option.query, key);
  90. if (value === undefined) return;
  91. path = replacePathParam(path, key, value);
  92. });
  93. }
  94. return path;
  95. }
  96. export function getListPagePath(key: PathKey, page: number): string {
  97. return getPath(key, { page });
  98. }
  99. export function getRoute(key: PathKey, exclude?: string): string {
  100. let path = get(PATHS, makePathKey(key));
  101. if (!path) throw new Error("ルート未定義:" + makePathKey(key));
  102. if (exclude) {
  103. path = replace(path, "/" + exclude + "/", "");
  104. }
  105. return path;
  106. }
  107. function replacePathParam(
  108. sourceStr: string,
  109. searchStr: string,
  110. replacement: string | number
  111. ): string {
  112. return replace(
  113. sourceStr,
  114. ":" + searchStr,
  115. isString(replacement) ? replacement : String(replacement)
  116. );
  117. }