Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

132 lines
3.9KB

  1. import { yupResolver } from "@hookform/resolvers/yup";
  2. import { Box, Button, Stack, Typography } from "@mui/material";
  3. import { HasChildren } from "@types";
  4. import { startPasswordSetting } from "api/auth";
  5. import RequireChip from "components/chip/RequireChip";
  6. import { FormProvider, RHFTextField } from "components/hook-form";
  7. import StackRow from "components/stack/StackRow";
  8. import useAPICall from "hooks/useAPICall";
  9. import useNavigateCustom from "hooks/useNavigateCustom";
  10. import { PageID } from "pages";
  11. import { useState } from "react";
  12. import { useForm } from "react-hook-form";
  13. import { getPath } from "routes/path";
  14. import { object as YupObject, string as YupString } from "yup";
  15. type AreaBoxProps = {
  16. label: string;
  17. require?: boolean;
  18. } & HasChildren;
  19. function AreaBox({ label, children, require }: AreaBoxProps) {
  20. return (
  21. <Box>
  22. <StackRow>
  23. <Typography variant="subtitle1">〇{label}</Typography>
  24. <RequireChip require={require ?? false} />
  25. </StackRow>
  26. {children}
  27. </Box>
  28. );
  29. }
  30. type FormProps = {
  31. email: string;
  32. retype_email: string;
  33. };
  34. export default function PasswordSettingStart() {
  35. const { navigateWhenChanged } = useNavigateCustom();
  36. const [done, setDone] = useState<boolean | null>(null);
  37. const form = useForm<FormProps>({
  38. defaultValues: {
  39. email: "",
  40. retype_email: "",
  41. },
  42. resolver: yupResolver(
  43. YupObject().shape({
  44. email: YupString().required("必須項目です"),
  45. retype_email: YupString()
  46. .required("必須項目です")
  47. .test("retype", "一致しません", function (value) {
  48. return value === this.parent.email;
  49. }),
  50. })
  51. ),
  52. });
  53. const { callAPI: callStartPasswordSetting } = useAPICall({
  54. apiMethod: startPasswordSetting,
  55. backDrop: true,
  56. form,
  57. onSuccess: () => {
  58. setDone(true);
  59. },
  60. onFailed: () => {},
  61. });
  62. const handleSubmit = (data: FormProps) => {
  63. callStartPasswordSetting({ ...data });
  64. };
  65. const toLogin = () => {
  66. navigateWhenChanged(getPath(PageID.LOGIN));
  67. };
  68. if (done === true) {
  69. return (
  70. <Box sx={{ p: 3, pt: 5, mx: "auto", maxWidth: 500 }} textAlign="center">
  71. <Stack spacing={2}>
  72. <Box>
  73. <Typography>
  74. メールアドレスに設定手続きのご案内を送信しました。
  75. </Typography>
  76. <Typography>ご確認ください。</Typography>
  77. </Box>
  78. <Box>
  79. <Button onClick={toLogin}>ログインへ</Button>
  80. </Box>
  81. </Stack>
  82. </Box>
  83. );
  84. }
  85. return (
  86. <Box sx={{ p: 3, pt: 5, mx: "auto", maxWidth: 500 }} textAlign="center">
  87. <FormProvider methods={form} onSubmit={form.handleSubmit(handleSubmit)}>
  88. <Stack spacing={2}>
  89. <Box>
  90. <Button onClick={toLogin}>戻る</Button>
  91. </Box>
  92. <Box>
  93. <Typography>
  94. 登録済みのメールアドレスへ変更用のURLを送信します。
  95. </Typography>
  96. </Box>
  97. <AreaBox label="Email" require={true}>
  98. <Box textAlign="left">
  99. <Typography variant="body2">8-20文字で設定可能です。</Typography>
  100. <Typography variant="body2">
  101. 半角英数字と記号(@.-)が使えます。
  102. </Typography>
  103. </Box>
  104. <RHFTextField name="email" />
  105. </AreaBox>
  106. <AreaBox label="Email (確認用)" require={true}>
  107. <Box textAlign="left">
  108. <Typography variant="body2">
  109. 確認のため再度同じパスワードを入力してください。
  110. </Typography>
  111. </Box>
  112. <RHFTextField name="retype_email" />
  113. </AreaBox>
  114. <Box>
  115. <Button type="submit">送信</Button>
  116. </Box>
  117. </Stack>
  118. </FormProvider>
  119. </Box>
  120. );
  121. }