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.

184 lines
4.7KB

  1. import { Box, Button, Stack, Typography } from "@mui/material";
  2. import { HasChildren } from "@types";
  3. import {
  4. uploadOtherLicenseImages,
  5. uploadStudentLicenseImages,
  6. } from "api/season-ticket-contract";
  7. import RequireChip from "components/chip/RequireChip";
  8. import { FormProvider } from "components/hook-form";
  9. import { RHFUpload } from "components/hook-form/RHFUpload";
  10. import StackRow from "components/stack/StackRow";
  11. import { useSeasonTicketContractContext } from "contexts/dashboard/SeasonTicketContractContext";
  12. import useAPICall from "hooks/useAPICall";
  13. import useAuth from "hooks/useAuth";
  14. import useDashboard from "hooks/useDashBoard";
  15. import useNavigateCustom from "hooks/useNavigateCustom";
  16. import useSnackbarCustom from "hooks/useSnackbarCustom";
  17. import { PageID, TabID } from "pages";
  18. import { useEffect, useMemo, useState } from "react";
  19. import { useForm } from "react-hook-form";
  20. import { getPath } from "routes/path";
  21. type AreaBoxProps = {
  22. label: string;
  23. require?: boolean;
  24. } & HasChildren;
  25. function AreaBox({ label, children, require }: AreaBoxProps) {
  26. return (
  27. <Box>
  28. <StackRow>
  29. <Typography variant="subtitle1">〇{label}</Typography>
  30. <RequireChip require={require ?? false} />
  31. </StackRow>
  32. {children}
  33. </Box>
  34. );
  35. }
  36. type FormProps = {
  37. file1: File[];
  38. file2: File[];
  39. file3: File[];
  40. };
  41. export default function OtherLicenseImagesUpload() {
  42. const { setHeaderTitle, setTabs } = useDashboard(
  43. PageID.DASHBOARD_USER_OTHER_LICENSE_IMAGES_UPLOAD,
  44. TabID.NONE
  45. );
  46. const { navigateWhenChanged, navigate } = useNavigateCustom();
  47. const { selectedseasonTicketContract, initialized } =
  48. useSeasonTicketContractContext();
  49. const [done, setDone] = useState(false);
  50. const { error } = useSnackbarCustom();
  51. const form = useForm<FormProps>({
  52. defaultValues: {
  53. file1: [],
  54. file2: [],
  55. file3: [],
  56. },
  57. });
  58. const { callAPI: callUploadOtherLicenseImages } = useAPICall({
  59. apiMethod: uploadOtherLicenseImages,
  60. backDrop: true,
  61. onSuccess: () => {
  62. setDone(true);
  63. },
  64. onFailed: () => {
  65. error("失敗しました");
  66. },
  67. });
  68. const file1 = form.watch("file1");
  69. const file2 = form.watch("file2");
  70. const file3 = form.watch("file3");
  71. const canSend = useMemo(() => {
  72. return file1.length !== 0;
  73. }, [file1]);
  74. const handleSubmit = (data: FormProps) => {
  75. if (!selectedseasonTicketContract) return;
  76. const files = [...file1, ...file2, ...file3];
  77. callUploadOtherLicenseImages({
  78. images: files,
  79. season_ticket_contract_record_no:
  80. selectedseasonTicketContract.season_ticekt_contract_record_no ?? "",
  81. });
  82. };
  83. useEffect(() => {
  84. if (file1.length === 0) {
  85. if (file2.length !== 0) {
  86. form.setValue("file2", []);
  87. }
  88. if (file3.length !== 0) {
  89. form.setValue("file3", []);
  90. }
  91. }
  92. if (file2.length === 0) {
  93. if (file3.length !== 0) {
  94. form.setValue("file3", []);
  95. }
  96. }
  97. }, [file1, file2, file3]);
  98. useEffect(() => {
  99. if (initialized && !selectedseasonTicketContract) {
  100. navigateWhenChanged(
  101. getPath(PageID.DASHBOARD_SEASON_TICKET_CONTRACT_LIST)
  102. );
  103. }
  104. }, [initialized, selectedseasonTicketContract]);
  105. useEffect(() => {
  106. setHeaderTitle("障害者手帳アップロード");
  107. setTabs(null);
  108. }, []);
  109. if (!selectedseasonTicketContract) {
  110. return null;
  111. }
  112. if (done) {
  113. return (
  114. <Stack spacing={2}>
  115. <Box>アップロードしました</Box>
  116. <Box>
  117. <Button
  118. onClick={() => {
  119. navigate(-1);
  120. }}
  121. >
  122. 戻る
  123. </Button>
  124. </Box>
  125. </Stack>
  126. );
  127. }
  128. return (
  129. <FormProvider methods={form} onSubmit={form.handleSubmit(handleSubmit)}>
  130. <Stack spacing={2}>
  131. <Box>
  132. <Button
  133. onClick={() => {
  134. navigate(-1);
  135. }}
  136. >
  137. 戻る
  138. </Button>
  139. </Box>
  140. <AreaBox label="前回アップロード日時">
  141. <Typography>
  142. {selectedseasonTicketContract.student_license_images_upload_datetime ??
  143. "-"}
  144. </Typography>
  145. </AreaBox>
  146. <AreaBox label="1枚目">
  147. <RHFUpload name="file1" />
  148. </AreaBox>
  149. {file1.length !== 0 && (
  150. <AreaBox label="2枚目">
  151. <RHFUpload name="file2" />
  152. </AreaBox>
  153. )}
  154. {file2.length !== 0 && (
  155. <AreaBox label="3枚目">
  156. <RHFUpload name="file3" />
  157. </AreaBox>
  158. )}
  159. <Box>
  160. <Button type="submit" variant="contained" disabled={!canSend}>
  161. 送信
  162. </Button>
  163. </Box>
  164. </Stack>
  165. </FormProvider>
  166. );
  167. }