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.

169 lines
4.2KB

  1. import {
  2. Box,
  3. Button,
  4. Paper,
  5. Stack,
  6. Step,
  7. StepLabel,
  8. Stepper,
  9. Table,
  10. TableBody,
  11. TableCell,
  12. TableRow,
  13. Typography,
  14. } from "@mui/material";
  15. import { HasChildren } from "@types";
  16. import { emailRequest } from "api/app/receipt-issuing-order";
  17. import useAPICall from "hooks/useAPICall";
  18. import useApp from "hooks/useApp";
  19. import useSnackbarCustom from "hooks/useSnackbarCustom";
  20. import { useMemo, useState } from "react";
  21. import useInputEmailStep from "./hooks/useInputEmailStep";
  22. type TableRowCustomProps = {
  23. title: string;
  24. value: string;
  25. };
  26. const TableRowCustom = ({ title, value }: TableRowCustomProps) => {
  27. return (
  28. <TableRow>
  29. <TableCell sx={{ borderRight: "1px solid rgba(224, 224, 224, 1)" }}>
  30. {title}
  31. </TableCell>
  32. <TableCell>{value}</TableCell>
  33. </TableRow>
  34. );
  35. };
  36. type SectionProps = {
  37. title: string;
  38. subtitle?: string;
  39. } & HasChildren;
  40. const Section = ({ title, subtitle, children }: SectionProps) => {
  41. return (
  42. <Paper
  43. sx={{ py: 2, border: "1px solid rgba(224, 224, 224, 1)" }}
  44. elevation={0}
  45. >
  46. <Box>
  47. <Typography variant="subtitle1">{title}</Typography>
  48. {subtitle && <Typography variant="body2">{subtitle}</Typography>}
  49. </Box>
  50. <Box sx={{ mt: 2 }}>{children}</Box>
  51. </Paper>
  52. );
  53. };
  54. export default function EmailOrder() {
  55. const {
  56. tokenResult,
  57. token,
  58. navigateToHome,
  59. fetch,
  60. receiptIssuingOrder: order,
  61. } = useApp();
  62. const { error } = useSnackbarCustom();
  63. const [mode, setMode] = useState<"input" | "confirm" | "done">("input");
  64. const input = useInputEmailStep({
  65. onNext: () => {
  66. setMode("confirm");
  67. },
  68. onPrev: () => {
  69. navigateToHome();
  70. },
  71. });
  72. const requestEmailAPI = useAPICall({
  73. apiMethod: emailRequest,
  74. backDrop: true,
  75. onSuccess: () => {
  76. fetch();
  77. setMode("done");
  78. },
  79. onFailed: () => {
  80. error("登録失敗");
  81. },
  82. });
  83. const currentStep = useMemo(() => {
  84. if (mode === "input") return 0;
  85. if (mode === "confirm") return 1;
  86. if (mode === "done") return 2;
  87. }, [mode]);
  88. const handleConfirm = () => {
  89. if (!order) return;
  90. requestEmailAPI.callAPI({
  91. access_token: token,
  92. timestamp: order.updated_at,
  93. ...input.values(),
  94. });
  95. };
  96. if (tokenResult !== "ok") {
  97. return null;
  98. }
  99. return (
  100. <>
  101. <Box sx={{ p: 3, pt: 5, mx: "auto", maxWidth: 500 }} textAlign="center">
  102. <Stack spacing={3}>
  103. <Box>
  104. <Typography variant="h5">領収証Email送付依頼</Typography>
  105. </Box>
  106. <Section title="">
  107. <Stepper activeStep={currentStep}>
  108. <Step>
  109. <StepLabel>Email送信先入力</StepLabel>
  110. </Step>
  111. <Step>
  112. <StepLabel>確認</StepLabel>
  113. </Step>
  114. <Step>
  115. <StepLabel>完了</StepLabel>
  116. </Step>
  117. </Stepper>
  118. {mode === "input" && input.element}
  119. {mode === "confirm" && (
  120. <Stack spacing={2} sx={{ p: 1, py: 3, m: 1 }}>
  121. <Typography variant="h5">Email送信先情報確認</Typography>
  122. <Table>
  123. <TableBody>
  124. <TableRowCustom
  125. title="Email"
  126. value={input.values("email")}
  127. />
  128. </TableBody>
  129. </Table>
  130. <Stack direction="row" spacing={2}>
  131. <Button
  132. variant="text"
  133. onClick={() => {
  134. setMode("input");
  135. }}
  136. >
  137. 戻る
  138. </Button>
  139. <Button variant="contained" onClick={handleConfirm}>
  140. 確定
  141. </Button>
  142. </Stack>
  143. </Stack>
  144. )}
  145. {mode === "done" && (
  146. <Stack spacing={2} sx={{ p: 1, py: 3, m: 1 }}>
  147. <Box>受付いたしました。</Box>
  148. <Button onClick={navigateToHome}>戻る</Button>
  149. </Stack>
  150. )}
  151. </Section>
  152. </Stack>
  153. </Box>
  154. </>
  155. );
  156. }