選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

110 行
2.4KB

  1. import { yupResolver } from "@hookform/resolvers/yup";
  2. import {
  3. Button,
  4. Dialog,
  5. DialogActions,
  6. DialogContent,
  7. DialogTitle,
  8. } from "@mui/material";
  9. import {
  10. ReceiptIssuingOrder,
  11. completeMailPost,
  12. } from "api/receipt-issuing-order";
  13. import { FormProvider } from "components/hook-form";
  14. import RHFDatePicker from "components/hook-form/RHFDatePicker";
  15. import useAPICall from "hooks/useAPICall";
  16. import useBackDrop from "hooks/useBackDrop";
  17. import useSnackbarCustom from "hooks/useSnackbarCustom";
  18. import { useEffect, useState } from "react";
  19. import { useForm } from "react-hook-form";
  20. import { now } from "utils/datetime";
  21. import * as Yup from "yup";
  22. type FormProps = {
  23. date: Date | null;
  24. };
  25. export default function useMailPostCompleteDialog(
  26. order: ReceiptIssuingOrder | null,
  27. onComplete?: VoidFunction
  28. ) {
  29. const [show, setShow] = useState(false);
  30. const { setShowBackDrop } = useBackDrop();
  31. const { success, error } = useSnackbarCustom();
  32. const form = useForm<FormProps>({
  33. defaultValues: {
  34. date: now(),
  35. },
  36. resolver: yupResolver(
  37. Yup.object().shape({
  38. date: Yup.date()
  39. .required("必須項目です")
  40. .typeError("正しく入力してください"),
  41. })
  42. ),
  43. });
  44. const { callAPI } = useAPICall({
  45. apiMethod: completeMailPost,
  46. backDrop: true,
  47. onSuccess: () => {
  48. success("登録しました");
  49. setShow(false);
  50. if (onComplete) {
  51. onComplete();
  52. }
  53. },
  54. onFailed: () => {
  55. error("失敗しました");
  56. },
  57. });
  58. const open = () => {
  59. setShow(true);
  60. };
  61. const close = () => {
  62. setShow(false);
  63. };
  64. const handleSubmit = (data: FormProps) => {
  65. if (!order) return;
  66. callAPI({
  67. id: order.id,
  68. status_mail_post_date: data.date,
  69. timestamp: order.updated_at,
  70. });
  71. };
  72. const element = (
  73. <Dialog open={show} onClose={close}>
  74. <DialogTitle>投稿完了登録</DialogTitle>
  75. <FormProvider methods={form} onSubmit={form.handleSubmit(handleSubmit)}>
  76. <DialogContent>
  77. <RHFDatePicker name="date" label="投函完了日" />
  78. </DialogContent>
  79. <DialogActions>
  80. <Button onClick={close}>CANCEL</Button>
  81. <Button type="submit">OK</Button>
  82. </DialogActions>
  83. </FormProvider>
  84. </Dialog>
  85. );
  86. return {
  87. // param
  88. show,
  89. // Element
  90. element,
  91. // function
  92. open,
  93. close,
  94. setShow,
  95. };
  96. }