Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 | 521x 521x 521x 521x 521x 521x 521x 521x 521x 521x 521x 521x 521x 34x 14x 521x 14x 14x 14x 14x 14x 14x 521x 9x 1x 1x 8x 1x 1x 7x 1x 1x 6x 1x 1x 5x 5x 1x 1x 4x 1x 1x 3x 3x 521x 9x 9x 9x 9x 6x 6x 3x 3x 2x 2x 2x 2x 2x 2x 2x 2x 1x 3x 521x 521x 1x 149x 129x 130x 901x 9x 902x | /**
* Create user dialog component
*/
import { useEffect, useState } from 'react';
import { AppDialog } from '@/components/common/AppDialog';
import { Button } from '@/components/ui/button';
import { Input } from '@/components/ui/input';
import { Label } from '@/components/ui/label';
import { Checkbox } from '@/components/ui/checkbox';
import {
Select,
SelectContent,
SelectItem,
SelectTrigger,
SelectValue,
} from '@/components/ui/select';
import { Loader2 } from 'lucide-react';
import { getAccounts, type Account } from '@/services/accounts.service';
import { getRoles, type RoleListItem } from '@/services/roles.service';
import type { CreateUserRequest } from '@/services/users.service';
import { handleApiError } from '@/lib/api';
interface CreateUserDialogProps {
open: boolean;
onOpenChange: (open: boolean) => void;
onSubmit: (data: CreateUserRequest) => Promise<void>;
}
export default function CreateUserDialog({ open, onOpenChange, onSubmit }: CreateUserDialogProps) {
const [email, setEmail] = useState('');
const [password, setPassword] = useState('');
const [confirmPassword, setConfirmPassword] = useState('');
const [accountId, setAccountId] = useState('');
const [roleId, setRoleId] = useState<number | null>(null);
const [isActive, setIsActive] = useState(true);
const [loading, setLoading] = useState(false);
const [error, setError] = useState<string | null>(null);
const [passwordError, setPasswordError] = useState<string | null>(null);
const [accounts, setAccounts] = useState<Account[]>([]);
const [roles, setRoles] = useState<RoleListItem[]>([]);
const [loadingData, setLoadingData] = useState(false);
// Load accounts and roles when dialog opens
useEffect(() => {
if (open) {
loadData();
}
}, [open]);
const loadData = async () => {
setLoadingData(true);
try {
const [accountsRes, rolesRes] = await Promise.all([
getAccounts({ page_size: 100 }),
getRoles(),
]);
setAccounts(accountsRes.items);
setRoles(rolesRes.items);
} catch (err) {
console.error('Failed to load data:', err);
} finally {
setLoadingData(false);
}
};
const validatePassword = (): boolean => {
if (password.length < 12) {
setPasswordError('Password must be at least 12 characters');
return false;
}
if (!/[A-Z]/.test(password)) {
setPasswordError('Password must contain at least one uppercase letter');
return false;
}
if (!/[a-z]/.test(password)) {
setPasswordError('Password must contain at least one lowercase letter');
return false;
}
if (!/\d/.test(password)) {
setPasswordError('Password must contain at least one digit');
return false;
}
// Use a simpler approach - just check for common special characters
const specialChars = /[-!@#$%^&*()_=\\[\]{};':"\\|,.<>]/;
if (!specialChars.test(password)) {
setPasswordError('Password must contain at least one special character');
return false;
}
if (password !== confirmPassword) {
setPasswordError('Passwords do not match');
return false;
}
setPasswordError(null);
return true;
};
const handleSubmit = async (e: React.FormEvent) => {
e.preventDefault();
setLoading(true);
setError(null);
if (!validatePassword()) {
setLoading(false);
return;
}
try {
await onSubmit({
email,
password,
account_id: accountId,
role_id: roleId!,
is_active: isActive,
});
// Reset form
setEmail('');
setPassword('');
setConfirmPassword('');
setAccountId('');
setRoleId(null);
setIsActive(true);
setPasswordError(null);
onOpenChange(false);
} catch (err) {
setError(handleApiError(err));
} finally {
setLoading(false);
}
};
const isFormValid = email && password && confirmPassword && accountId && roleId;
return (
<AppDialog
open={open}
onOpenChange={onOpenChange}
title="Create User"
description="Create a new user in any account. The user will be able to log in immediately."
className="max-w-md"
footer={
<>
<Button
type="button"
variant="outline"
onClick={() => onOpenChange(false)}
disabled={loading}
>
Cancel
</Button>
<Button type="submit" form="create-user-form" disabled={loading || !isFormValid || loadingData}>
{loading && <Loader2 className="mr-2 h-4 w-4 animate-spin" />}
Create User
</Button>
</>
}
>
{loadingData ? (
<div className="flex justify-center py-8">
<Loader2 className="h-6 w-6 animate-spin text-muted-foreground" />
</div>
) : (
<form id="create-user-form" onSubmit={handleSubmit} className="space-y-4">
<div className="space-y-2">
<Label htmlFor="email">Email *</Label>
<Input
id="email"
type="email"
value={email}
onChange={(e) => setEmail(e.target.value)}
placeholder="user@example.com"
required
disabled={loading}
/>
</div>
<div className="space-y-2">
<Label htmlFor="password">Password *</Label>
<Input
id="password"
type="password"
value={password}
onChange={(e) => setPassword(e.target.value)}
placeholder="••••••••••••"
required
disabled={loading}
/>
<p className="text-xs text-muted-foreground">
Min 12 chars, uppercase, lowercase, digit, special char
</p>
</div>
<div className="space-y-2">
<Label htmlFor="confirmPassword">Confirm Password *</Label>
<Input
id="confirmPassword"
type="password"
value={confirmPassword}
onChange={(e) => setConfirmPassword(e.target.value)}
placeholder="••••••••••••"
required
disabled={loading}
/>
</div>
<div className="space-y-2">
<Label htmlFor="account">Account *</Label>
<Select
value={accountId}
onValueChange={setAccountId}
disabled={loading}
>
<SelectTrigger id="account">
<SelectValue placeholder="Select an account" />
</SelectTrigger>
<SelectContent>
{accounts.map((account) => (
<SelectItem key={account.id} value={account.id}>
{account.name} ({account.account_code})
</SelectItem>
))}
</SelectContent>
</Select>
</div>
<div className="space-y-2">
<Label htmlFor="role">Role *</Label>
<Select
value={roleId?.toString() || ''}
onValueChange={(v) => setRoleId(parseInt(v))}
disabled={loading}
>
<SelectTrigger id="role">
<SelectValue placeholder="Select a role" />
</SelectTrigger>
<SelectContent>
{roles.map((role) => (
<SelectItem key={role.id} value={role.id.toString()}>
{role.name}
</SelectItem>
))}
</SelectContent>
</Select>
</div>
<div className="flex items-center space-x-2">
<Checkbox
id="isActive"
checked={isActive}
onCheckedChange={(checked) => setIsActive(checked as boolean)}
disabled={loading}
/>
<Label htmlFor="isActive" className="cursor-pointer">
Active (can log in)
</Label>
</div>
{passwordError && (
<div className="text-sm text-destructive bg-destructive/10 p-3 rounded-md">
{passwordError}
</div>
)}
{error && (
<div className="text-sm text-destructive bg-destructive/10 p-3 rounded-md">
{typeof error === 'string' ? error : JSON.stringify(error)}
</div>
)}
</form>
)}
</AppDialog>
);
}
|