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.

177 line
5.0KB

  1. import { Box, Step, StepLabel, Stepper } from "@mui/material";
  2. import { getHTAdjustData } from "api/custom/hello-techno";
  3. import { createReceiptIssuingOrder } from "api/custom/hello-techno/receipt-issuing-order";
  4. import { PageID, TabID } from "codes/page";
  5. import { getValue } from "components/hook-form/RHFAutoComplete";
  6. import useAPICall from "hooks/useAPICall";
  7. import useDashboard from "hooks/useDashBoard";
  8. import useSnackbarCustom from "hooks/useSnackbarCustom";
  9. import { useEffect, useMemo, useState } from "react";
  10. import useConfirm, { ConfirmDataProps } from "./hooks/useConfirm";
  11. import useInputReceiptStep from "./hooks/useInputReceiptStep";
  12. import useInputSMSSendAddress from "./hooks/useInputSMSSendAddress";
  13. import useSelectParkingStep from "./hooks/useSelectParkingStep";
  14. export default function ReceiptIssuingOrderCreate() {
  15. const { setHeaderTitle, setTabs } = useDashboard(
  16. PageID.DASHBOARD_RECEIPT_ISSUING_ORDER_CREATE_CUSTOM_HELLO_TECHNO,
  17. TabID.NONE
  18. );
  19. const { success, error } = useSnackbarCustom();
  20. const [mode, setMode] = useState<
  21. "parking_select" | "input_receipt" | "input_address" | "confirm" | "done"
  22. >("parking_select");
  23. const step = useMemo(() => {
  24. switch (mode) {
  25. case "parking_select":
  26. return 0;
  27. case "input_receipt":
  28. return 1;
  29. case "input_address":
  30. return 2;
  31. case "confirm":
  32. return 3;
  33. case "done":
  34. return 4;
  35. }
  36. }, [mode]);
  37. const getConfimData = (): ConfirmDataProps => {
  38. return {
  39. customerName: selectParkingStep.values("customerCode.label"),
  40. parkingName: selectParkingStep.values("parkingManagementCode.label"),
  41. adjustSeqNo: selectParkingStep.values("adjustSeqNo"),
  42. amount: inputReceiptStep.values("amount"),
  43. date: inputReceiptStep.values("date"),
  44. address: inputSMSSendAddress.values("address"),
  45. memo: inputReceiptStep.values("memo"),
  46. };
  47. };
  48. const getFormData = () => {
  49. return {
  50. ...selectParkingStep.values(),
  51. ...inputReceiptStep.values(),
  52. ...inputSMSSendAddress.values(),
  53. };
  54. };
  55. const adjustDataAPI = useAPICall({
  56. apiMethod: getHTAdjustData,
  57. backDrop: true,
  58. onSuccess: ({ data }) => {
  59. inputReceiptStep.setAdjustData(data);
  60. setMode("input_receipt");
  61. },
  62. onFailed: () => {
  63. error("精算履歴が存在しません");
  64. },
  65. });
  66. const selectParkingStep = useSelectParkingStep({
  67. onNext: () => {
  68. const { customerCode, parkingManagementCode, adjustSeqNo } =
  69. getFormData();
  70. if (adjustSeqNo) {
  71. adjustDataAPI.callAPI({
  72. customer_code: getValue(customerCode),
  73. parking_management_code: getValue(parkingManagementCode),
  74. adjust_seq_no: adjustSeqNo,
  75. });
  76. } else {
  77. inputReceiptStep.setAdjustData(null);
  78. setMode("input_receipt");
  79. }
  80. },
  81. });
  82. const inputReceiptStep = useInputReceiptStep({
  83. onNext: () => {
  84. setMode("input_address");
  85. },
  86. onPrev: () => {
  87. setMode("parking_select");
  88. },
  89. });
  90. const inputSMSSendAddress = useInputSMSSendAddress({
  91. onNext: () => {
  92. confirm.setData(getConfimData());
  93. setMode("confirm");
  94. },
  95. onPrev: () => {
  96. setMode("input_receipt");
  97. },
  98. });
  99. const confirm = useConfirm({
  100. onNext: () => {
  101. send();
  102. },
  103. onPrev: () => {
  104. setMode("input_address");
  105. },
  106. });
  107. const createAPI = useAPICall({
  108. apiMethod: createReceiptIssuingOrder,
  109. backDrop: true,
  110. onSuccess: () => {
  111. setMode("done");
  112. success("成功しました");
  113. },
  114. onFailed: () => {
  115. error("失敗しました");
  116. },
  117. });
  118. const send = () => {
  119. const { callAPI, makeSendData } = createAPI;
  120. const formData = getFormData();
  121. const sendData = makeSendData({
  122. customer_code: getValue(formData.customerCode),
  123. parking_management_code: getValue(formData.parkingManagementCode),
  124. adjust_seq_no: formData.adjustSeqNo,
  125. receipt_use_date: formData.date,
  126. receipt_amount: formData.amount,
  127. sms_phone_number: formData.address,
  128. memo: formData.memo,
  129. });
  130. callAPI(sendData);
  131. };
  132. useEffect(() => {
  133. setHeaderTitle("領収証発行依頼作成");
  134. setTabs(null);
  135. }, []);
  136. return (
  137. <Box sx={{ p: 1, m: 1 }}>
  138. <Stepper activeStep={step}>
  139. <Step>
  140. <StepLabel>駐車場選択</StepLabel>
  141. </Step>
  142. <Step>
  143. <StepLabel>領収証情報入力</StepLabel>
  144. </Step>
  145. <Step>
  146. <StepLabel>SMS送信先入力</StepLabel>
  147. </Step>
  148. <Step>
  149. <StepLabel>確認</StepLabel>
  150. </Step>
  151. <Step>
  152. <StepLabel>完了</StepLabel>
  153. </Step>
  154. </Stepper>
  155. {mode === "parking_select" && selectParkingStep.element}
  156. {mode === "input_receipt" && inputReceiptStep.element}
  157. {mode === "input_address" && inputSMSSendAddress.element}
  158. {mode === "confirm" && confirm.element}
  159. {mode === "done" && <Box sx={{ p: 1, py: 3, m: 1 }}>受付しました。</Box>}
  160. </Box>
  161. );
  162. }