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.

135 line
3.8KB

  1. import { HasChildren } from "@types";
  2. import { 駐車場マスタ } from "api/parking";
  3. import {
  4. QRサービス券駐車場グループ,
  5. QRサービス券駐車場グループ一覧取得,
  6. } from "api/qr-service";
  7. import useAPICall from "hooks/useAPICall";
  8. import useDashboard from "hooks/useDashBoard";
  9. import useNavigateCustom from "hooks/useNavigateCustom";
  10. import useSnackbarCustom from "hooks/useSnackbarCustom";
  11. import useQRサービス券駐車場グループ管理Tabs from "layouts/dashbord/tab/QRサービス券駐車場グループ管理Tabs";
  12. import { PageID, TabID } from "pages";
  13. import { createContext, useEffect, useMemo, useState } from "react";
  14. import { useParams } from "react-router-dom";
  15. import { getPath } from "routes/path";
  16. import 駐車場マスタストア from "storage/cache/駐車場マスタ";
  17. type Context = {
  18. parkings: 駐車場マスタ[];
  19. groups: QRサービス券駐車場グループ[];
  20. selectedGroup: QRサービス券駐車場グループ | null;
  21. fetch: () => Promise<void>;
  22. moveToList: VoidFunction;
  23. moveToDetail: (id: string) => void;
  24. };
  25. export const QRサービス券駐車場グループ管理Context = createContext<Context>({
  26. parkings: [],
  27. groups: [],
  28. selectedGroup: null,
  29. fetch: async () => {},
  30. moveToList: () => {},
  31. moveToDetail: (id: string) => {},
  32. });
  33. type Props = HasChildren;
  34. function QRサービス券駐車場グループ管理ContextProvider({ children }: Props) {
  35. const { id: paramId } = useParams();
  36. const [parkings, setParkings] = useState<駐車場マスタ[]>([]);
  37. const [groups, setGroups] = useState<QRサービス券駐車場グループ[]>([]);
  38. const { success, error } = useSnackbarCustom();
  39. const { navigateWhenChanged } = useNavigateCustom();
  40. const selectedGroup = useMemo(() => {
  41. return groups.find((g) => g.id === paramId) ?? null;
  42. }, [groups, paramId]);
  43. const { callAPI: callQRサービス券駐車場グループ一覧取得 } = useAPICall({
  44. apiMethod: QRサービス券駐車場グループ一覧取得,
  45. backDrop: true,
  46. onSuccess: ({ data }) => {
  47. setGroups(data.list);
  48. },
  49. onFailed: () => {
  50. error("データ取得失敗");
  51. },
  52. });
  53. const fetch = async () => {
  54. await callQRサービス券駐車場グループ一覧取得({});
  55. };
  56. const moveToList = () => {
  57. navigateWhenChanged(
  58. getPath(
  59. [
  60. PageID.QRサービス券駐車場グループ管理,
  61. TabID.QRサービス券駐車場グループ管理_一覧,
  62. ],
  63. {}
  64. )
  65. );
  66. };
  67. const moveToDetail = (id: string) => {
  68. navigateWhenChanged(
  69. getPath(
  70. [
  71. PageID.QRサービス券駐車場グループ管理,
  72. TabID.QRサービス券駐車場グループ管理_詳細,
  73. ],
  74. {
  75. query: {
  76. id,
  77. },
  78. }
  79. )
  80. );
  81. };
  82. const { setHeaderTitle } = useDashboard(
  83. PageID.QRサービス券駐車場グループ管理
  84. );
  85. useEffect(() => {
  86. setHeaderTitle("QRサービス券駐車場グループ管理");
  87. }, []);
  88. useEffect(() => {
  89. fetch();
  90. 駐車場マスタストア.get().then((parkings) => {
  91. setParkings(parkings);
  92. });
  93. }, []);
  94. return (
  95. <QRサービス券駐車場グループ管理Context.Provider
  96. value={{
  97. parkings,
  98. groups,
  99. selectedGroup,
  100. fetch,
  101. moveToList,
  102. moveToDetail,
  103. }}
  104. >
  105. <TabInit />
  106. {children}
  107. </QRサービス券駐車場グループ管理Context.Provider>
  108. );
  109. }
  110. function TabInit() {
  111. const { setTabs, tabId } = useDashboard();
  112. const { element } = useQRサービス券駐車場グループ管理Tabs();
  113. useEffect(() => {
  114. setTabs(element);
  115. }, [tabId]);
  116. return null;
  117. }
  118. export default QRサービス券駐車場グループ管理ContextProvider;