Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

110 Zeilen
2.7KB

  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.DASHBOARD_OVERVIEW)]: "/dashboard",
  33. // 契約関連
  34. [makePathKey(PageID.DASHBOARD_CONTRACT_LIST)]:
  35. "/dashboard/contract/list/:page",
  36. [makePathKey(PageID.DASHBOARD_CONTRACT_DETAIL)]: "/dashboard/contract/detail",
  37. // 領収証発行依頼関連
  38. [makePathKey(PageID.DASHBOARD_RECEIPT_ISSUING_ORDER_CREATE)]:
  39. "/dashboard/receipt-issusing-order/create",
  40. [makePathKey(PageID.DASHBOARD_RECEIPT_ISSUING_ORDER_LIST)]:
  41. "/dashboard/receipt-issusing-order/list/:page",
  42. [makePathKey(PageID.DASHBOARD_RECEIPT_ISSUING_ORDER_DETAIL)]:
  43. "/dashboard/receipt-issusing-order/detail",
  44. // その他
  45. [makePathKey(PageID.PAGE_403)]: "403",
  46. [makePathKey(PageID.PAGE_404)]: "404",
  47. };
  48. export type PathOption = {
  49. page?: number;
  50. query?: Dictionary;
  51. };
  52. export function getPath(key: PathKey, option?: PathOption) {
  53. const pageId = getPageId(key);
  54. const tabId = getTabId(key);
  55. let path = getRoute(pageId);
  56. // ページ番号解決
  57. path = replacePathParam(path, "page", option?.page ?? 0);
  58. // その他URLパラメータ変換
  59. if (option?.query !== undefined) {
  60. Object.keys(option.query).forEach((key) => {
  61. const value = get(option.query, key);
  62. if (value === undefined) return;
  63. path = replacePathParam(path, key, value);
  64. });
  65. }
  66. return path;
  67. }
  68. export function getListPagePath(key: PathKey, page: number): string {
  69. return getPath(key, { page });
  70. }
  71. export function getRoute(key: PathKey, exclude?: string): string {
  72. let path = get(PATHS, makePathKey(key));
  73. if (!path) throw new Error("ルート未定義:" + makePathKey(key));
  74. if (exclude) {
  75. path = replace(path, "/" + exclude + "/", "");
  76. }
  77. return path;
  78. }
  79. function replacePathParam(
  80. sourceStr: string,
  81. searchStr: string,
  82. replacement: string | number
  83. ): string {
  84. return replace(
  85. sourceStr,
  86. ":" + searchStr,
  87. isString(replacement) ? replacement : String(replacement)
  88. );
  89. }