import React, { useState } from 'react'; import { toast } from 'sonner'; import { api } from '@/api'; import { Button } from '@/components/ui/button'; import { Input } from '@/components/ui/input'; import { Label } from '@/components/ui/label'; import { Card, CardHeader, CardTitle, CardContent } from '@/components/ui/card'; export default function AddUserCard({ onCreated }) { const [username, setUsername] = useState(''); const [password, setPassword] = useState(''); const [loading, setLoading] = useState(false); const [error, setError] = useState(''); const handleCreate = async (e) => { e.preventDefault(); setError(''); if (password.length < 8) { const msg = 'Password must be at least 8 characters.'; setError(msg); toast.error(msg); return; } setLoading(true); try { await api.createUser({ username, password }); toast.success(`User "${username}" created.`); setUsername(''); setPassword(''); setError(''); onCreated(); } catch (err) { const errorMessage = err.message || 'Failed to create user.'; setError(errorMessage); toast.error(errorMessage); } finally { setLoading(false); } }; return ( Add User
setUsername(e.target.value)} placeholder="username" required />
setPassword(e.target.value)} placeholder="Password" required />
{error && (
{error}
)} ); }