aboutsummaryrefslogtreecommitdiff
path: root/src/SignUpForm2.tsx
blob: f35522529e90c96a56d60b063343edff8cccfd6b (about) (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import './SignUp.css'
import { useForm } from 'react-hook-form'
import { zodResolver } from "@hookform/resolvers/zod"
import { useLocation, Navigate, useNavigate } from 'react-router-dom';
import { Form1, Form2, form2Schema } from './signUpSchema'

export const SignUpForm2 = () => {
  const { register, handleSubmit, formState: { errors } } = useForm<Form2>({
    resolver: zodResolver(form2Schema),
  });
  const location = useLocation();
  const form1 = location.state as Form1 | null;
  const navigate = useNavigate();

  // form1 のデータがない場合は form1 にリダイレクトする
  if (form1 === null) {
    return <Navigate replace to="/sign-up/form1" />
  }

  const onsubmit = (form2: Form2) => {
    const state = {
      ...form1,
      ...form2,
    }
    navigate('/sign-up/confirm', { state })
  }
  const onerror = (err: any) => console.log(err);

  return (
    <>
      <h1>会員登録 フェーズ 2</h1>
      <form className="SignUpForm" onSubmit={handleSubmit(onsubmit, onerror)}>
        <div>
          <label htmlFor="email">メールアドレス: </label>
          <input id="email" type="email" {...register('email')}></input>
          <div className="error">{errors.email?.message}</div>
        </div>
        <div>
          <label htmlFor="password">パスワード: </label>
          <input id="password" type="password" {...register('password')}></input>
          <div className="error">{errors.password?.message}</div>
        </div>
        <div>
          <button type="submit">確認画面へ</button>
        </div>
      </form>
    </>
  );
}