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.

196 line
5.4KB

  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 { mailRequest } from "api/app/receipt-issuing-order";
  17. import { getPrefName } from "codes/prefcode";
  18. import useAPICall from "hooks/useAPICall";
  19. import useApp from "hooks/useApp";
  20. import useSnackbarCustom from "hooks/useSnackbarCustom";
  21. import { useMemo, useState } from "react";
  22. import useInputMailStep from "./hooks/useInputMailStep";
  23. type TableRowCustomProps = {
  24. title: string;
  25. value: string;
  26. };
  27. const TableRowCustom = ({ title, value }: TableRowCustomProps) => {
  28. return (
  29. <TableRow>
  30. <TableCell sx={{ borderRight: "1px solid rgba(224, 224, 224, 1)" }}>
  31. {title}
  32. </TableCell>
  33. <TableCell>{value}</TableCell>
  34. </TableRow>
  35. );
  36. };
  37. type SectionProps = {
  38. title: string;
  39. subtitle?: string;
  40. } & HasChildren;
  41. const Section = ({ title, subtitle, children }: SectionProps) => {
  42. return (
  43. <Paper
  44. sx={{ py: 2, border: "1px solid rgba(224, 224, 224, 1)" }}
  45. elevation={0}
  46. >
  47. <Box>
  48. <Typography variant="subtitle1">{title}</Typography>
  49. {subtitle && <Typography variant="body2">{subtitle}</Typography>}
  50. </Box>
  51. <Box sx={{ mt: 2 }}>{children}</Box>
  52. </Paper>
  53. );
  54. };
  55. export default function MailOrder() {
  56. const {
  57. tokenResult,
  58. token,
  59. navigateToHome,
  60. fetch,
  61. receiptIssuingOrder: order,
  62. } = useApp();
  63. const { error } = useSnackbarCustom();
  64. const [mode, setMode] = useState<"input" | "confirm" | "done">("input");
  65. const input = useInputMailStep({
  66. onNext: () => {
  67. setMode("confirm");
  68. },
  69. onPrev: () => {
  70. navigateToHome();
  71. },
  72. });
  73. const requestMailAPI = useAPICall({
  74. apiMethod: mailRequest,
  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. requestMailAPI.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">領収証郵送依頼</Typography>
  105. </Box>
  106. <Section title="">
  107. <Stepper activeStep={currentStep}>
  108. <Step>
  109. <StepLabel>郵送先情報入力</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">郵送先情報確認</Typography>
  122. <Table>
  123. <TableBody>
  124. <TableRowCustom
  125. title="都道府県"
  126. value={getPrefName(input.values("mail_pref_code"))}
  127. />
  128. <TableRowCustom
  129. title="郵便番号"
  130. value={"〒" + input.values("mail_zip_code")}
  131. />
  132. <TableRowCustom
  133. title="市区町村"
  134. value={input.values("mail_address1")}
  135. />
  136. <TableRowCustom
  137. title="番地等"
  138. value={input.values("mail_address2")}
  139. />
  140. <TableRowCustom
  141. title="建物名・部屋番号等"
  142. value={input.values("mail_address3")}
  143. />
  144. <TableRowCustom
  145. title="宛名"
  146. value={input.values("mail_name")}
  147. />
  148. </TableBody>
  149. </Table>
  150. <Box>
  151. <Stack>
  152. <Box>郵送先に間違いがないか確認してください。</Box>
  153. <Box>投函まで数営業日を要しますのでご了承ください。</Box>
  154. </Stack>
  155. </Box>
  156. <Stack direction="row" spacing={2}>
  157. <Button
  158. variant="text"
  159. onClick={() => {
  160. setMode("input");
  161. }}
  162. >
  163. 戻る
  164. </Button>
  165. <Button variant="contained" onClick={handleConfirm}>
  166. 確定
  167. </Button>
  168. </Stack>
  169. </Stack>
  170. )}
  171. {mode === "done" && (
  172. <Stack spacing={2} sx={{ p: 1, py: 3, m: 1 }}>
  173. <Box>郵送依頼を受付いたしました。</Box>
  174. <Box>到着までしばらくお待ちください。</Box>
  175. <Button onClick={navigateToHome}>戻る</Button>
  176. </Stack>
  177. )}
  178. </Section>
  179. </Stack>
  180. </Box>
  181. </>
  182. );
  183. }