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.

148 lines
3.8KB

  1. import { yupResolver } from "@hookform/resolvers/yup";
  2. import { Box, Button, Stack, Typography } from "@mui/material";
  3. import { HasChildren } from "@types";
  4. import { getHTCustomers, getHTParkings } from "api/custom/hello-techno";
  5. import {
  6. FormProvider,
  7. RHFAutoComplete,
  8. RHFTextField,
  9. } from "components/hook-form";
  10. import {
  11. AutoCompleteOption,
  12. AutoCompleteOptionType,
  13. } from "components/hook-form/RHFAutoComplete";
  14. import useAPICall from "hooks/useAPICall";
  15. import { useEffect, useState } from "react";
  16. import { useForm } from "react-hook-form";
  17. import * as Yup from "yup";
  18. type AreaBoxProps = {
  19. title: string;
  20. } & HasChildren;
  21. function AreaBox({ title, children }: AreaBoxProps) {
  22. return (
  23. <Box sx={{ maxWidth: 500 }}>
  24. <Typography variant="subtitle1">{title}</Typography>
  25. {children}
  26. </Box>
  27. );
  28. }
  29. type FormProps = {
  30. customerCode: AutoCompleteOptionType;
  31. parkingManagementCode: AutoCompleteOptionType;
  32. adjustSeqNo: string;
  33. };
  34. type Props = {
  35. onNext?: VoidFunction;
  36. };
  37. export default function useSelectParkingStep({ onNext }: Props) {
  38. const [customers, setCustomers] = useState<AutoCompleteOption[]>([]);
  39. const [parkings, setParkings] = useState<AutoCompleteOption[]>([]);
  40. const customerAPI = useAPICall({
  41. apiMethod: getHTCustomers,
  42. onSuccess: ({ data }) => {
  43. const options: AutoCompleteOption[] = data.records.map(
  44. ({ customer_code, name }) => {
  45. return {
  46. label: name,
  47. value: customer_code,
  48. };
  49. }
  50. );
  51. setCustomers(options);
  52. },
  53. });
  54. const parkingAPI = useAPICall({
  55. apiMethod: getHTParkings,
  56. onSuccess: ({ data }) => {
  57. const options: AutoCompleteOption[] = data.records.map(
  58. ({ parking_management_code, name }) => {
  59. return {
  60. label: name,
  61. value: parking_management_code,
  62. };
  63. }
  64. );
  65. setParkings(options);
  66. },
  67. });
  68. const form = useForm<FormProps>({
  69. defaultValues: {
  70. customerCode: null,
  71. parkingManagementCode: null,
  72. adjustSeqNo: "",
  73. },
  74. resolver: yupResolver(
  75. Yup.object().shape({
  76. customerCode: Yup.object().required("必須項目です"),
  77. parkingManagementCode: Yup.object().required("必須項目です"),
  78. adjustSeqNo: Yup.number()
  79. .nullable()
  80. .transform((value, originalValue) =>
  81. String(originalValue).trim() === "" ? null : value
  82. )
  83. .typeError("数値を入力してください"),
  84. })
  85. ),
  86. });
  87. const customerCode = form.watch("customerCode.value");
  88. const handleSubmit = () => {
  89. if (onNext) {
  90. onNext();
  91. }
  92. };
  93. // 顧客一覧取得
  94. useEffect(() => {
  95. customerAPI.callAPI({});
  96. }, []);
  97. // 駐車場一覧取得
  98. useEffect(() => {
  99. setParkings([]);
  100. form.setValue("parkingManagementCode", null);
  101. if (customerCode) {
  102. parkingAPI.callAPI({ customer_code: customerCode });
  103. }
  104. }, [customerCode]);
  105. const element = (
  106. <FormProvider methods={form} onSubmit={form.handleSubmit(handleSubmit)}>
  107. <Stack spacing={2} sx={{ p: 1, m: 1 }}>
  108. <AreaBox title="運営会社">
  109. <RHFAutoComplete
  110. name="customerCode"
  111. options={customers}
  112. size="small"
  113. />
  114. </AreaBox>
  115. <AreaBox title="駐車場">
  116. <RHFAutoComplete
  117. name="parkingManagementCode"
  118. options={parkings}
  119. size="small"
  120. />
  121. </AreaBox>
  122. <AreaBox title="精算連番">
  123. <RHFTextField name="adjustSeqNo" size="small" />
  124. </AreaBox>
  125. <Stack direction="row" spacing={2}>
  126. <Button variant="contained" type="submit">
  127. 次へ
  128. </Button>
  129. </Stack>
  130. </Stack>
  131. </FormProvider>
  132. );
  133. return { element, values: form.getValues, setValue: form.setValue };
  134. }