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.

114 line
2.9KB

  1. import { Dictionary } from "@types";
  2. import { PageID, TabID } from "pages";
  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_DASHBOARD = {
  28. [makePathKey(PageID.DASHBOARD_ENPTY)]: "/dashboard/loading",
  29. [makePathKey(PageID.DASHBOARD_OVERVIEW)]: "/dashboard",
  30. [makePathKey(PageID.ログインユーザ_顧客一覧)]:
  31. "/dashboard/login-user/customer/list",
  32. [makePathKey(PageID.ログインユーザ_顧客新規登録)]:
  33. "/dashboard/login-user/customer/register",
  34. [makePathKey(PageID.ログインユーザ_店舗一覧)]:
  35. "/dashboard/login-user/shop/list",
  36. [makePathKey(PageID.サービス券発行用QRコード)]: "/dashboard/qrcode/generate",
  37. [makePathKey(PageID.サービス券利用履歴)]: "/dashboard/qrcode/history",
  38. };
  39. const PATHS = {
  40. [makePathKey(PageID.NONE)]: "/",
  41. // 認証
  42. [makePathKey(PageID.LOGIN)]: "/login",
  43. [makePathKey(PageID.LOGOUT)]: "/logout",
  44. [makePathKey(PageID.QRサービス券発行申請)]: "qr-service/acquitision/:token",
  45. // ダッシュボード----------------
  46. ...PATHS_DASHBOARD,
  47. // その他
  48. [makePathKey(PageID.PAGE_403)]: "403",
  49. [makePathKey(PageID.PAGE_404)]: "404",
  50. };
  51. export type PathOption = {
  52. page?: number;
  53. query?: Dictionary;
  54. };
  55. export function getPath(key: PathKey, option?: PathOption) {
  56. const pageId = getPageId(key);
  57. const tabId = getTabId(key);
  58. let path = getRoute(pageId);
  59. // ページ番号解決
  60. path = replacePathParam(path, "page", option?.page ?? 0);
  61. // その他URLパラメータ変換
  62. if (option?.query !== undefined) {
  63. Object.keys(option.query).forEach((key) => {
  64. const value = get(option.query, key);
  65. if (value === undefined) return;
  66. path = replacePathParam(path, key, value);
  67. });
  68. }
  69. return path;
  70. }
  71. export function getListPagePath(key: PathKey, page: number): string {
  72. return getPath(key, { page });
  73. }
  74. export function getRoute(key: PathKey, exclude?: string): string {
  75. let path = get(PATHS, makePathKey(key));
  76. if (!path) throw new Error("ルート未定義:" + makePathKey(key));
  77. if (exclude) {
  78. path = replace(path, "/" + exclude + "/", "");
  79. }
  80. return path;
  81. }
  82. function replacePathParam(
  83. sourceStr: string,
  84. searchStr: string,
  85. replacement: string | number
  86. ): string {
  87. return replace(
  88. sourceStr,
  89. ":" + searchStr,
  90. isString(replacement) ? replacement : String(replacement)
  91. );
  92. }