170 lines
6.1 KiB
TypeScript
170 lines
6.1 KiB
TypeScript
'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 (
|
|
<div data-testid="create-user-modal" className="fixed inset-0 z-50 flex items-center justify-center bg-black/40">
|
|
<div className="bg-surface border border-slate-800 rounded-lg shadow-xl max-w-sm w-full mx-4">
|
|
{/* Header */}
|
|
<div className="flex items-center justify-between p-6 border-b border-slate-800">
|
|
<h2 className="text-lg font-normal text-white">Create User</h2>
|
|
<button
|
|
onClick={onClose}
|
|
className="p-1 text-muted hover:text-secondary rounded focus-visible:ring-2 focus-visible:ring-primary focus-visible:outline-none"
|
|
aria-label="Close"
|
|
>
|
|
<X size={20} />
|
|
</button>
|
|
</div>
|
|
|
|
{/* Form */}
|
|
<form onSubmit={handleSubmit} className="p-6 lg:p-8 xl:p-10 space-y-4">
|
|
{/* Username Field */}
|
|
<div>
|
|
<label htmlFor="username" className="block text-sm font-normal text-secondary mb-2">
|
|
Username
|
|
</label>
|
|
<input
|
|
id="username"
|
|
data-testid="new-user-name"
|
|
type="text"
|
|
value={username}
|
|
onChange={(e) => {
|
|
setUsername(e.target.value);
|
|
if (errors.username) setErrors({ ...errors, username: undefined });
|
|
}}
|
|
placeholder="Enter username"
|
|
className={`w-full px-3 lg:px-4 xl:px-5 py-2 lg:py-3 xl:py-4.5 bg-slate-800 border rounded text-white placeholder:text-muted focus:outline-none focus-visible:ring-2 focus-visible:ring-primary ${
|
|
errors.username ? 'border-red-500' : 'border-slate-700'
|
|
}`}
|
|
disabled={loading}
|
|
/>
|
|
{errors.username && (
|
|
<p className="mt-1.5 text-sm lg:text-base xl:text-lg lg:text-xl xl:text-2xl lg:text-3xl xl:text-4xl text-red-500">{errors.username}</p>
|
|
)}
|
|
</div>
|
|
|
|
{/* Password Field */}
|
|
<div>
|
|
<label htmlFor="password" className="block text-sm font-normal text-secondary mb-2">
|
|
Password
|
|
</label>
|
|
<input
|
|
id="password"
|
|
data-testid="new-user-password"
|
|
type="password"
|
|
value={password}
|
|
onChange={(e) => {
|
|
setPassword(e.target.value);
|
|
if (errors.password) setErrors({ ...errors, password: undefined });
|
|
}}
|
|
placeholder="Enter password"
|
|
className={`w-full px-3 lg:px-4 xl:px-5 py-2 lg:py-3 xl:py-4.5 bg-slate-800 border rounded text-white placeholder:text-muted focus:outline-none focus-visible:ring-2 focus-visible:ring-primary ${
|
|
errors.password ? 'border-red-500' : 'border-slate-700'
|
|
}`}
|
|
disabled={loading}
|
|
/>
|
|
{errors.password && (
|
|
<p className="mt-1.5 text-sm lg:text-base xl:text-lg lg:text-xl xl:text-2xl lg:text-3xl xl:text-4xl text-red-500">{errors.password}</p>
|
|
)}
|
|
</div>
|
|
|
|
{/* Buttons */}
|
|
<div className="flex gap-2 pt-4">
|
|
<button
|
|
type="button"
|
|
onClick={onClose}
|
|
disabled={loading}
|
|
className="flex-1 px-4 py-2.5 text-secondary bg-slate-800 border border-slate-700 rounded font-normal hover:bg-slate-700 transition-colors disabled:opacity-50 focus-visible:ring-2 focus-visible:ring-primary focus-visible:outline-none"
|
|
>
|
|
Cancel
|
|
</button>
|
|
<button
|
|
type="submit"
|
|
data-testid="create-user-submit"
|
|
disabled={!isFormValid || loading}
|
|
className="flex-1 px-4 py-2.5 bg-primary text-primary-foreground rounded font-normal hover:bg-blue-600 transition-colors disabled:opacity-50 flex items-center justify-center gap-2 focus-visible:ring-2 focus-visible:ring-offset-2 focus-visible:ring-offset-slate-900 focus-visible:ring-primary focus-visible:outline-none"
|
|
>
|
|
{loading ? (
|
|
<>
|
|
<Loader2 size={16} className="animate-spin" />
|
|
Creating...
|
|
</>
|
|
) : (
|
|
'Create'
|
|
)}
|
|
</button>
|
|
</div>
|
|
</form>
|
|
</div>
|
|
</div>
|
|
);
|
|
}
|