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.

199 lines
5.5KB

  1. import {
  2. Box,
  3. Button,
  4. Divider,
  5. Paper,
  6. Stack,
  7. Step,
  8. StepLabel,
  9. Stepper,
  10. Table,
  11. TableBody,
  12. TableCell,
  13. TableRow,
  14. Typography,
  15. } from "@mui/material";
  16. import { HasChildren } from "@types";
  17. import { getPrefName } from "codes/prefcode";
  18. import useApp from "hooks/useApp";
  19. import useNavigateCustom from "hooks/useNavigateCustom";
  20. import { useMemo, useState } from "react";
  21. import useInputMailStep from "./hooks/useInputMailStep";
  22. import useAPICall from "hooks/useAPICall";
  23. import { mailRequest } from "api/app/receipt-issuing-order";
  24. import useSnackbarCustom from "hooks/useSnackbarCustom";
  25. type TableRowCustomProps = {
  26. title: string;
  27. value: string;
  28. };
  29. const TableRowCustom = ({ title, value }: TableRowCustomProps) => {
  30. return (
  31. <TableRow>
  32. <TableCell sx={{ borderRight: "1px solid rgba(224, 224, 224, 1)" }}>
  33. {title}
  34. </TableCell>
  35. <TableCell>{value}</TableCell>
  36. </TableRow>
  37. );
  38. };
  39. type SectionProps = {
  40. title: string;
  41. subtitle?: string;
  42. } & HasChildren;
  43. const Section = ({ title, subtitle, children }: SectionProps) => {
  44. return (
  45. <Paper
  46. sx={{ py: 2, border: "1px solid rgba(224, 224, 224, 1)" }}
  47. elevation={0}
  48. >
  49. <Box>
  50. <Typography variant="subtitle1">{title}</Typography>
  51. {subtitle && <Typography variant="body2">{subtitle}</Typography>}
  52. </Box>
  53. <Box sx={{ mt: 2 }}>{children}</Box>
  54. </Paper>
  55. );
  56. };
  57. export default function MailOrder() {
  58. const {
  59. tokenResult,
  60. navigateToHome,
  61. fetch,
  62. receiptIssuingOrder: order,
  63. } = useApp();
  64. const { success, error } = useSnackbarCustom();
  65. const { navigate } = useNavigateCustom();
  66. const [mode, setMode] = useState<"input" | "confirm" | "done">("input");
  67. const input = useInputMailStep({
  68. onNext: () => {
  69. setMode("confirm");
  70. },
  71. onPrev: () => {
  72. navigate(-1);
  73. },
  74. });
  75. const requestMailAPI = useAPICall({
  76. apiMethod: mailRequest,
  77. onSuccess: () => {
  78. fetch();
  79. setMode("done");
  80. },
  81. onFailed: () => {
  82. error("登録失敗");
  83. },
  84. });
  85. const currentStep = useMemo(() => {
  86. if (mode === "input") return 0;
  87. if (mode === "confirm") return 1;
  88. if (mode === "done") return 2;
  89. }, [mode]);
  90. const handleConfirm = () => {
  91. if (!order) return;
  92. requestMailAPI.callAPI({
  93. id: order.id,
  94. timestamp: order.updated_at,
  95. ...input.values(),
  96. });
  97. };
  98. if (tokenResult !== "ok") {
  99. return null;
  100. }
  101. return (
  102. <>
  103. <Box sx={{ p: 3, pt: 5, mx: "auto", maxWidth: 500 }} textAlign="center">
  104. <Stack spacing={3}>
  105. <Box>
  106. <Typography variant="h5">領収証郵送依頼</Typography>
  107. </Box>
  108. <Section title="">
  109. <Stepper activeStep={currentStep}>
  110. <Step>
  111. <StepLabel>郵送先情報入力</StepLabel>
  112. </Step>
  113. <Step>
  114. <StepLabel>確認</StepLabel>
  115. </Step>
  116. <Step>
  117. <StepLabel>完了</StepLabel>
  118. </Step>
  119. </Stepper>
  120. {mode === "input" && input.element}
  121. {mode === "confirm" && (
  122. <Stack spacing={2} sx={{ p: 1, py: 3, m: 1 }}>
  123. <Typography variant="h5">郵送先情報確認</Typography>
  124. <Table>
  125. <TableBody>
  126. <TableRowCustom
  127. title="都道府県"
  128. value={getPrefName(input.values("mail_pref_code"))}
  129. />
  130. <TableRowCustom
  131. title="郵便番号"
  132. value={"〒" + input.values("mail_zip_code")}
  133. />
  134. <TableRowCustom
  135. title="市区町村"
  136. value={input.values("mail_address1")}
  137. />
  138. <TableRowCustom
  139. title="番地等"
  140. value={input.values("mail_address2")}
  141. />
  142. <TableRowCustom
  143. title="建物名・部屋番号等"
  144. value={input.values("mail_address3")}
  145. />
  146. <TableRowCustom
  147. title="宛名"
  148. value={input.values("mail_name")}
  149. />
  150. </TableBody>
  151. </Table>
  152. <Box>
  153. <Stack>
  154. <Box>郵送先に間違いがないか確認してください。</Box>
  155. <Box>投函まで数営業日を要しますのでご了承ください。</Box>
  156. </Stack>
  157. </Box>
  158. <Stack direction="row" spacing={2}>
  159. <Button
  160. variant="text"
  161. onClick={() => {
  162. setMode("input");
  163. }}
  164. >
  165. 戻る
  166. </Button>
  167. <Button variant="contained" onClick={handleConfirm}>
  168. 確定
  169. </Button>
  170. </Stack>
  171. </Stack>
  172. )}
  173. {mode === "done" && (
  174. <Stack spacing={2} sx={{ p: 1, py: 3, m: 1 }}>
  175. <Box>郵送依頼を受付いたしました。</Box>
  176. <Box>到着までしばらくお待ちください。</Box>
  177. <Button onClick={navigateToHome}>戻る</Button>
  178. </Stack>
  179. )}
  180. </Section>
  181. </Stack>
  182. </Box>
  183. </>
  184. );
  185. }