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.

128 lines
3.3KB

  1. import { Box, Button, Paper, Stack } from "@mui/material";
  2. import { HasChildren } from "@types";
  3. import {
  4. QRサービス券取得用トークンリフレッシュ,
  5. QRサービス券取得用トークン取得,
  6. } from "api/qr-service";
  7. import useAPICall from "hooks/useAPICall";
  8. import useAuth from "hooks/useAuth";
  9. import useDashboard from "hooks/useDashBoard";
  10. import { useDialog } from "hooks/useDialog";
  11. import useSnackbarCustom from "hooks/useSnackbarCustom";
  12. import { PageID, TabID } from "pages";
  13. import QRCode from "qrcode";
  14. import { useEffect, useMemo, useState } from "react";
  15. import { getFullPath, getPath } from "routes/path";
  16. export default function サービス券発行用QRコード() {
  17. const { setHeaderTitle, setTabs } = useDashboard(
  18. PageID.サービス券発行用QRコード,
  19. TabID.NONE
  20. );
  21. const size = 200;
  22. const { error } = useSnackbarCustom();
  23. const [token, setToken] = useState("");
  24. const [qr, setQr] = useState("");
  25. const { callAPI: callQRサービス券取得用トークン取得 } = useAPICall({
  26. apiMethod: QRサービス券取得用トークン取得,
  27. backDrop: true,
  28. onSuccess: ({ data }) => {
  29. setToken(data.token);
  30. },
  31. onFailed: () => {
  32. error("QR表示失敗しました");
  33. },
  34. });
  35. const url = useMemo(() => {
  36. if (!token) return "";
  37. return getFullPath(PageID.QRサービス券発行申請, {
  38. query: {
  39. token,
  40. },
  41. });
  42. }, [token]);
  43. const fetch = () => {
  44. callQRサービス券取得用トークン取得({});
  45. };
  46. useEffect(() => {
  47. if (url) {
  48. QRCode.toDataURL(url, {
  49. errorCorrectionLevel: "H",
  50. }).then((data: string) => {
  51. setQr(data);
  52. });
  53. }
  54. }, [url]);
  55. useEffect(() => {
  56. setHeaderTitle("サービス券発行用QRコード");
  57. }, [setHeaderTitle, setTabs]);
  58. useEffect(() => {
  59. fetch();
  60. }, []);
  61. return (
  62. <Box>
  63. <Stack spacing={2}>
  64. <Paper sx={{ p: 2 }}>
  65. <Stack>
  66. <Box mx="auto">
  67. {!!qr && <img src={qr} width={size} height={size}></img>}
  68. </Box>
  69. <Box mx="auto">{url}</Box>
  70. <Box mx="auto">
  71. <RefreshButton fetch={fetch}>QRコードリフレッシュ</RefreshButton>
  72. </Box>
  73. </Stack>
  74. </Paper>
  75. </Stack>
  76. </Box>
  77. );
  78. }
  79. type RefreshButtonProps = {
  80. fetch: VoidFunction;
  81. } & HasChildren;
  82. function RefreshButton({ fetch }: RefreshButtonProps) {
  83. const {} = useAuth();
  84. const { success, error } = useSnackbarCustom();
  85. const { callAPI: callQRサービス券取得用トークンリフレッシュ } = useAPICall({
  86. apiMethod: QRサービス券取得用トークンリフレッシュ,
  87. onSuccess: () => {
  88. success("リフレッシュしました");
  89. fetch();
  90. },
  91. onFailed: () => {
  92. error("失敗しました");
  93. },
  94. });
  95. const { element, open, isAgree } = useDialog({
  96. message: "リフレッシュすると以前のQRコードが使えなくなります",
  97. });
  98. const handleClick = () => {
  99. open();
  100. };
  101. useEffect(() => {
  102. if (isAgree) {
  103. callQRサービス券取得用トークンリフレッシュ({});
  104. }
  105. }, [isAgree]);
  106. return (
  107. <>
  108. {element}
  109. <Button onClick={handleClick}>QRコードリフレッシュ</Button>
  110. </>
  111. );
  112. }