'use client' import { useState } from 'react' import { useForm } from 'react-hook-form' import { zodResolver } from '@hookform/resolvers/zod' import { useRouter, useSearchParams } from 'next/navigation' import { Button } from 'components/ui/button' import { z } from 'zod' import { pinEntrySchema, type PinEntryForm as PinEntryFormType } from 'lib/validations/code' export function PinSetupForm() { const [loading, setLoading] = useState(false) const [error, setError] = useState(null) const [skipPin, setSkipPin] = useState(false) const [manualCode, setManualCode] = useState('') const router = useRouter() const searchParams = useSearchParams() const codeFromParams = searchParams.get('code') // Define schema typed for the form to ensure TypeScript knows pin + confirmPin exist const setupSchema = pinEntrySchema.extend({ confirmPin: pinEntrySchema.shape.pin }).refine((data) => data.pin === data.confirmPin, { message: "PIN se neshoduje", path: ["confirmPin"], }) const { register, handleSubmit, formState: { errors } } = useForm>({ resolver: zodResolver(setupSchema) }) // Use effectiveCode: prefer query param, otherwise manual input const effectiveCode = (codeFromParams || manualCode || '').toUpperCase() const onSubmit = async (data: PinEntryFormType | { pin?: string }) => { const code = effectiveCode if (!code) { setError('Chybí kód místnosti') return } try { setLoading(true) setError(null) const response = await fetch('/api/codes/setup', { method: 'POST', credentials: 'include', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ code: code.toUpperCase(), pin: (data as any).pin ? (data as any).pin : undefined, userAgent: navigator.userAgent }) }) const result = await response.json() if (!response.ok) { setError(result.error || 'Nastala chyba při nastavování') return } if (result.roomId) { router.push(`/room/${result.roomId}?code=${code}`) } } catch (e) { console.error('PinSetup submit error:', e) setError('Došlo k neočekávané chybě') } finally { setLoading(false) } } const handleSkipPin = async () => { // Ensure we have a code if (!effectiveCode) { setError('Zadejte prosím kód místnosti před pokračováním bez PIN.') return } setSkipPin(true) await onSubmit({ pin: '' }) } if (skipPin) { return (

Upozornění: Bez PIN ochrany se o místnost přijdete při smazání cookies prohlížeče.

) } return (

Ochrana místnosti

Nastavte si 5-číselný PIN pro ochranu přístupu do této místnosti. Bez PIN se o přístup přijdete při smazání cookies.

{/* If no code in query, allow user to type it */} {!codeFromParams && (
setManualCode(e.target.value.toUpperCase())} type="text" id="manualCode" maxLength={5} placeholder="A1B2C" className="w-full px-3 py-2 border border-gray-300 rounded-md focus:outline-none focus:ring-2 focus:ring-brand-500 focus:border-brand-500 uppercase text-center font-mono" style={{ textTransform: 'uppercase' }} />

Zadejte kód místnosti, který chcete vytvořit nebo použít.

)}
{errors?.pin && (

{errors.pin.message}

)}
{errors?.confirmPin && (

{errors.confirmPin.message}

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