'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 { codeWithPinSchema, type CodeWithPinForm } from 'lib/validations/code' interface TransferCodeFormProps { onSuccess: () => void onCancel: () => void } export function TransferCodeForm({ onSuccess, onCancel }: TransferCodeFormProps) { const [loading, setLoading] = useState(false) const [error, setError] = useState(null) const [needsPin, setNeedsPin] = useState(false) const { register, handleSubmit, formState: { errors }, watch } = useForm({ resolver: zodResolver(codeWithPinSchema) }) const codeValue = watch('code') const onSubmit = async (data: CodeWithPinForm) => { try { setLoading(true) setError(null) const response = await fetch('/api/users/transfer-code', { method: 'POST', credentials: 'include', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ code: data.code.toUpperCase(), pin: data.pin || undefined }) }) const result = await response.json() if (!response.ok) { if (response.status === 400 && result.needsPin) { setNeedsPin(true) return } else if (response.status === 401) { setError('Neplatný kód nebo PIN') } else if (response.status === 409) { setError('Kód již máte ve svém profilu') } else { setError(result.error || 'Nastala chyba při převodu kódu') } return } onSuccess() } catch { setError('Došlo k neočekávané chybě') } finally { setLoading(false) } } return (

Převést anonymní kód

Zadejte kód a PIN (pokud je nastaven) pro převedení anonymní místnosti pod váš profil.

{errors.code && (

{errors.code.message}

)}
{(needsPin || codeValue?.length === 5) && (
{errors.pin && (

{errors.pin.message}

)}

Nechte prázdné, pokud místnost není chráněna PIN

)} {error && (
{error}
)}
) }