'use client'; import { useState } from 'react'; import { X, Loader2 } from 'lucide-react'; import { inventoryApi } from '@/lib/api'; import { toast } from 'react-hot-toast'; interface CreateUserModalProps { show: boolean; onClose: () => void; onUserCreated: () => void; } export default function CreateUserModal({ show, onClose, onUserCreated }: CreateUserModalProps) { const [username, setUsername] = useState(''); const [password, setPassword] = useState(''); const [errors, setErrors] = useState<{ username?: string; password?: string }>({}); const [loading, setLoading] = useState(false); if (!show) return null; const validateForm = () => { const newErrors: typeof errors = {}; if (!username.trim()) { newErrors.username = 'Username is required'; } if (!password.trim()) { newErrors.password = 'Password is required'; } else if (password.length < 6) { newErrors.password = 'Password must be at least 6 characters'; } setErrors(newErrors); return Object.keys(newErrors).length === 0; }; const handleSubmit = async (e: React.FormEvent) => { e.preventDefault(); if (!validateForm()) return; setLoading(true); try { await inventoryApi.createUser({ username: username.trim(), password, role: 'user' }); toast.success('User created successfully'); setUsername(''); setPassword(''); setErrors({}); onUserCreated(); onClose(); } catch (error: any) { const errorMsg = error?.response?.data?.detail || 'Failed to create user'; if (errorMsg.includes('already exists')) { setErrors({ username: 'Username already exists' }); } else { toast.error(errorMsg); } } finally { setLoading(false); } }; const isFormValid = username.trim() && password.length >= 6 && !Object.keys(errors).length; return (
{/* Header */}

Create User

{/* Form */}
{/* Username Field */}
{ setUsername(e.target.value); if (errors.username) setErrors({ ...errors, username: undefined }); }} placeholder="Enter username" className={`w-full px-3 py-2.5 bg-slate-800 border rounded text-white placeholder-slate-500 focus:outline-none focus-visible:ring-2 focus-visible:ring-primary ${ errors.username ? 'border-red-500' : 'border-slate-700' }`} disabled={loading} /> {errors.username && (

{errors.username}

)}
{/* Password Field */}
{ setPassword(e.target.value); if (errors.password) setErrors({ ...errors, password: undefined }); }} placeholder="Enter password" className={`w-full px-3 py-2.5 bg-slate-800 border rounded text-white placeholder-slate-500 focus:outline-none focus-visible:ring-2 focus-visible:ring-primary ${ errors.password ? 'border-red-500' : 'border-slate-700' }`} disabled={loading} /> {errors.password && (

{errors.password}

)}
{/* Buttons */}
); }