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.

143 lines
4.0KB

  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. [makePathKey(PageID.APP_PRIVACY_POLICY)]: "/app/privacy-policy",
  42. // 契約関連
  43. [makePathKey(PageID.DASHBOARD_CONTRACT_LIST)]:
  44. "/dashboard/contract/list/:page",
  45. [makePathKey(PageID.DASHBOARD_CONTRACT_DETAIL)]:
  46. "/dashboard/contract/detail/:id",
  47. [makePathKey(PageID.DASHBOARD_CONTRACT_CREATE)]: "/dashboard/contract/create",
  48. // 利用実績関連
  49. [makePathKey(PageID.DASHBOARD_USE_SUMMARY_LIST_BY_CONTRACT)]:
  50. "/dashboard/use-summary/list",
  51. // 領収証発行依頼関連
  52. [makePathKey(
  53. PageID.DASHBOARD_RECEIPT_ISSUING_ORDER_CREATE_CUSTOM_HELLO_TECHNO
  54. )]: "/dashboard/receipt-issusing-order/create",
  55. [makePathKey(
  56. PageID.DASHBOARD_RECEIPT_ISSUING_ORDER_LIST_CUSTOM_HELLO_TECHNO
  57. )]: "/dashboard/receipt-issusing-order/list/:page",
  58. [makePathKey(
  59. PageID.DASHBOARD_RECEIPT_ISSUING_ORDER_DETAIL_CUSTOM_HELLO_TECHNO
  60. )]: "/dashboard/receipt-issusing-order/detail/:id",
  61. // 利用実績関連
  62. [makePathKey(PageID.DASHBOARD_USE_SUMMARY_LIST_CUSTOM_HELLO_TECHNO)]:
  63. "/dashboard/use-summary/hello-techno/list",
  64. [makePathKey(PageID.DASHBOARD_USE_SUMMARY_DETAIL_CUSTOM_HELLO_TECHNO)]:
  65. "/dashboard/use-summary/hello-techno/detail",
  66. // ログインユーザ管理
  67. [makePathKey(PageID.DASHBOARD_LOGIN_USER_LIST)]:
  68. "/dashboard/login-user/list/:page",
  69. [makePathKey(PageID.DASHBOARD_LOGIN_USER_CREATE)]:
  70. "/dashboard/login-user/create",
  71. [makePathKey(PageID.DASHBOARD_LOGIN_USER_CHANGE_PASSWORD)]:
  72. "/dashboard/login-user/change-password/:id",
  73. // その他
  74. [makePathKey(PageID.PAGE_403)]: "403",
  75. [makePathKey(PageID.PAGE_404)]: "404",
  76. };
  77. export type PathOption = {
  78. page?: number;
  79. query?: Dictionary;
  80. };
  81. export function getPath(key: PathKey, option?: PathOption) {
  82. const pageId = getPageId(key);
  83. const tabId = getTabId(key);
  84. let path = getRoute(pageId);
  85. // ページ番号解決
  86. path = replacePathParam(path, "page", option?.page ?? 0);
  87. // その他URLパラメータ変換
  88. if (option?.query !== undefined) {
  89. Object.keys(option.query).forEach((key) => {
  90. const value = get(option.query, key);
  91. if (value === undefined) return;
  92. path = replacePathParam(path, key, value);
  93. });
  94. }
  95. return path;
  96. }
  97. export function getListPagePath(key: PathKey, page: number): string {
  98. return getPath(key, { page });
  99. }
  100. export function getRoute(key: PathKey, exclude?: string): string {
  101. let path = get(PATHS, makePathKey(key));
  102. if (!path) throw new Error("ルート未定義:" + makePathKey(key));
  103. if (exclude) {
  104. path = replace(path, "/" + exclude + "/", "");
  105. }
  106. return path;
  107. }
  108. function replacePathParam(
  109. sourceStr: string,
  110. searchStr: string,
  111. replacement: string | number
  112. ): string {
  113. return replace(
  114. sourceStr,
  115. ":" + searchStr,
  116. isString(replacement) ? replacement : String(replacement)
  117. );
  118. }