"use client" import React, { useState } from "react" import { Button } from "@/components/ui/button" import { Input } from "@/components/ui/input" import { Label } from "@/components/ui/label" import { Card, CardContent, CardDescription, CardFooter, CardHeader, CardTitle } from "@/components/ui/card" import { Checkbox } from "@/components/ui/checkbox" import { useAuth } from "@/lib/auth-context" import { type UserRole } from "@/lib/types" import { Mail, Lock, User, UserPlus, AlertCircle, Building2 } from "lucide-react" interface RegisterFormProps { onSwitchToLogin: () => void } export function RegisterForm({ onSwitchToLogin }: RegisterFormProps) { // Access authentication context for registration functionality and room data const { register, rooms } = useAuth() // State variables for form inputs and UI feedback const [firstname, setFirstname] = useState("") const [lastname, setLastname] = useState("") const [email, setEmail] = useState("") const [password, setPassword] = useState("") const [role, setRole] = useState("LEHRKRAFT") // Default role for new users const [selectedRoomIds, setSelectedRoomIds] = useState([]) // Stores IDs of rooms for 'RAUMBETREUER' role const [error, setError] = useState("") // Stores error messages const [isLoading, setIsLoading] = useState(false) // Tracks loading state for the form const [privacyAccepted, setPrivacyAccepted] = useState(false) // Tracks privacy policy acceptance // Handles toggling selection of supervised rooms for 'RAUMBETREUER' const handleRoomToggle = (roomId: number) => { setSelectedRoomIds((prev) => (prev.includes(roomId) ? prev.filter((r) => r !== roomId) : [...prev, roomId])) } // Handles form submission for user registration const handleSubmit = async (e: React.FormEvent) => { e.preventDefault() // Prevent default form submission behavior setError("") // Clear previous errors // Validate if a 'RAUMBETREUER' has selected at least one room if (role === "RAUMBETREUER" && selectedRoomIds.length === 0) { setError("Bitte wählen Sie mindestens einen Raum aus") return } if (!privacyAccepted) { setError("Bitte akzeptieren Sie die Datenschutzerklärung") return } setIsLoading(true) // Show loading indicator // Attempt to register the new user with provided details const success = await register( { firstname, lastname, email, role, roomIds: role === "RAUMBETREUER" ? selectedRoomIds : undefined, // Only send room IDs if role is 'RAUMBETREUER' password } ) // Handle registration success or failure if (!success) { setError("Registrierung fehlgeschlagen") } if (success) { onSwitchToLogin() // Switch to login form upon successful registration } setIsLoading(false) // Hide loading indicator } return ( Konto erstellen Geben Sie Ihre Daten ein
{/* Display error message if present */} {error && (
{error}
)} {/* First Name and Last Name inputs */}
setFirstname(e.target.value)} className="pl-10" required />
setLastname(e.target.value)} className="pl-10" required />
{/* Email input field */}
setEmail(e.target.value)} className="pl-10" required />
{/* Password input field */}
setPassword(e.target.value)} className="pl-10" required minLength={4} />
{/* Role selection */}
{/* Privacy Policy Checkbox - ADDED */}
setPrivacyAccepted(checked as boolean)} />

Die Zustimmung ist erforderlich für die Registrierung.

{role === "RAUMBETREUER" && (
{rooms.map((room) => (
handleRoomToggle(room.id)} />
))}
{selectedRoomIds.length > 0 && (

{selectedRoomIds.length} Raum/Räume ausgewählt

)}
)}
{/* Submit button with loading state */} {/* Link to switch to login form */}

Bereits registriert?{" "}

) }