File size: 5,118 Bytes
2376294
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
769d6f8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2376294
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
'use client'

import { useState } from 'react'
import { useForm } from 'react-hook-form'
import { zodResolver } from '@hookform/resolvers/zod'
import { Button } from 'components/ui/button'
import { registerSchema, type RegisterForm as RegisterFormType } from 'lib/validations/auth'

export function RegisterForm() {
  const [loading, setLoading] = useState(false)
  const [error, setError] = useState<string | null>(null)
  const [success, setSuccess] = useState(false)

  const {
    register,
    handleSubmit,
    formState: { errors }
  } = useForm<RegisterFormType>({
    resolver: zodResolver(registerSchema)
  })

  const onSubmit = async (data: RegisterFormType) => {
    try {
      setLoading(true)
      setError(null)

      const response = await fetch('/api/auth/register', {
        method: 'POST',
        headers: { 'Content-Type': 'application/json' },
        body: JSON.stringify(data)
      })

      const result = await response.json()

      if (!response.ok) {
        setError(result.error || 'Registrace se nezdařila')
        return
      }

      setSuccess(true)
    } catch {
      setError('Došlo k neočekávané chybě')
    } finally {
      setLoading(false)
    }
  }

  if (success) {
    return (
      <div className="text-center space-y-4">
        <div className="p-4 bg-green-50 rounded-lg">
          <h3 className="text-lg font-medium text-green-800">Registrace úspěšná!</h3>
          <p className="text-green-600 mt-2">
            Zkontrolujte svůj email a klikněte na ověřovací odkaz.
          </p>
        </div>
      </div>
    )
  }

  return (
    <form id="form-registration" action="#" method="post" className="form" onSubmit={handleSubmit(onSubmit)}>
      {error ? (
        <div
          className="error"
          style={{
            marginBottom: 12,
            padding: '10px 12px',
            background: '#FEE2E2',
            color: '#991B1B',
            borderRadius: 8,
            fontWeight: 600
          }}
        >
          {error}
        </div>
      ) : null}

      <div className="input">
        <div className="text">Jméno</div>
        <div className="insert">
          <input
            {...register('fname')}
            type="text"
            maxLength={255}
            placeholder="Tvoje jméno"
            autoComplete="given-name"
          />
          <div className="error">{errors.fname?.message}</div>
        </div>
      </div>

      <div className="input">
        <div className="text">Příjmení</div>
        <div className="insert">
          <input
            {...register('sname')}
            type="text"
            maxLength={255}
            placeholder="Tvoje příjmení"
            autoComplete="family-name"
          />
          <div className="error">{errors.sname?.message}</div>
        </div>
      </div>

      <div className="input">
        <div className="text">E-mail</div>
        <div className="insert">
          <input
            {...register('email')}
            type="email"
            maxLength={255}
            placeholder="E-mailová adresa"
            autoComplete="email"
          />
          <div className="error">{errors.email?.message}</div>
        </div>
      </div>

      <div className="input">
        <div className="text">Heslo</div>
        <div className="insert">
          <input
            {...register('password')}
            type="password"
            maxLength={255}
            placeholder="Heslo k přihlášení"
            autoComplete="new-password"
          />
          <div className="error">{errors.password?.message}</div>
          <div className="visible" data-input="password"></div>
        </div>
      </div>

      <div className="input">
        <div className="text">Heslo znovu</div>
        <div className="insert">
          <input
            {...register('confirmPassword')}
            type="password"
            maxLength={255}
            placeholder="Heslo pro kontrolu"
            autoComplete="new-password"
          />
          <div className="error">{errors.confirmPassword?.message}</div>
          <div className="visible" data-input="confirmPassword"></div>
        </div>
      </div>

      <div className="input">
        <div className="text">Pohlaví</div>
        <div className="insert">
          <div className="line">
            <label className="radio">
              <input type="radio" {...register('sex')} value="female" />
              <span className="text">Žena</span>
            </label>
            <label className="radio">
              <input type="radio" {...register('sex')} value="male" />
              <span className="text">Muž</span>
            </label>
          </div>
          <div className="error"></div>
        </div>
      </div>

      {error ? <div className="error" style={{ marginBottom: 12 }}>{error}</div> : null}

      <div className="input">
        <div className="text"></div>
        <div className="insert">
          <input type="submit" value="Registrovat" disabled={loading} />
        </div>
      </div>
    </form>
  )
}