summaryrefslogtreecommitdiff
path: root/client/components/sign_up/_sign_up.jsx
blob: bbbd51bbbbbbaf9783845358a85fe2cf2db138b5 (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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import { useState } from 'react';

export const SignUp = () => {
  const [name, setName] = useState('');
  const [email, setEmail] = useState('');
  const [emailConfirmation, setEmailConfirmation] = useState('');
  const [password, setPassword] = useState('');
  const [passwordConfiramation, setPasswordConfirmation] = useState('');
  const [errorMessage, setErrorMessage] = useState('');

  const signUp = async () => {
    if (email === '') {
      setErrorMessage('Email cannot be blank');
      return;
    }
    if (email !== emailConfirmation) {
      setErrorMessage('Email does not match.');
      return;
    }
    if (password === '') {
      setErrorMessage('Password cannot be blank');
      return;
    }
    if (password !== passwordConfiramation) {
      setErrorMessage('Password does not match');
      return;
    }
    if (name === '') {
      setErrorMessage('Name cannot be blank.');
      return;
    }
    try {
      await fetch('/users', {
        method: 'POST',
        headers: {
          'Content-Type': 'application/json',
        },
        body: JSON.stringify({
          name,
          email,
          password,
        }),
      });
    } catch (e) {
      console.log(e.message);
    }
  };

  return (
    <div>
      <div>Name</div>
      <input
        type="text"
        value={name}
        onChange={(e) => setName(e.target.value)}
      />
      <div>Email</div>
      <input
        type="email"
        value={email}
        onChange={(e) => setEmail(e.target.value)}
      />
      <div>Confirm Email</div>
      <input
        type="email"
        value={emailConfirmation}
        onChange={(e) => setEmailConfirmation(e.target.value)}
      />
      <div>Password</div>
      <input
        type="password"
        value={password}
        onChange={(e) => setPassword(e.target.value)}
      />
      <div>Confirm Password</div>
      <input
        type="password"
        value={passwordConfiramation}
        onChange={(e) => setPasswordConfirmation(e.target.value)}
      />
      <div>
        <button type="button" onClick={signUp}>Sign up</button>
      </div>
    </div>
  );
};