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 | 58x 58x 58x 58x 58x 58x 58x 58x 25x 6x 58x 6x 6x 6x 5x 1x 6x 58x 58x 58x 1x 8x | /**
* Create group 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 { Textarea } from '@/components/ui/textarea';
import {
Select,
SelectContent,
SelectItem,
SelectTrigger,
SelectValue,
} from '@/components/ui/select';
import { Loader2 } from 'lucide-react';
import type { CreateGroupRequest } from '@/services/groups.service';
import { getAccounts, type Account } from '@/services/accounts.service';
interface CreateGroupDialogProps {
open: boolean;
onOpenChange: (open: boolean) => void;
onSubmit: (data: CreateGroupRequest) => Promise<void>;
}
export default function CreateGroupDialog({ open, onOpenChange, onSubmit }: CreateGroupDialogProps) {
const [name, setName] = useState('');
const [description, setDescription] = useState('');
const [accountId, setAccountId] = useState('');
const [loading, setLoading] = useState(false);
const [error, setError] = useState<string | null>(null);
const [accounts, setAccounts] = useState<Account[]>([]);
const [loadingAccounts, setLoadingAccounts] = useState(false);
// Load accounts when dialog opens
useEffect(() => {
if (open) {
loadAccounts();
}
}, [open]);
const loadAccounts = async () => {
setLoadingAccounts(true);
try {
const response = await getAccounts({ page_size: 100 });
setAccounts(response.items);
} catch (err) {
console.error('Failed to load accounts:', err);
} finally {
setLoadingAccounts(false);
}
};
const handleSubmit = async (e: React.FormEvent) => {
e.preventDefault();
setLoading(true);
setError(null);
try {
await onSubmit({
name,
description: description || null,
account_id: accountId || undefined,
});
// Reset form
setName('');
setDescription('');
setAccountId('');
onOpenChange(false);
} catch (err: unknown) {
const error = err as { response?: { data?: { detail?: string | unknown } }; message?: string };
const errorMsg = error.response?.data?.detail || error.message || 'Failed to create group';
setError(typeof errorMsg === 'string' ? errorMsg : JSON.stringify(errorMsg));
} finally {
setLoading(false);
}
};
const isFormValid = name.trim().length > 0 && accountId;
return (
<AppDialog
open={open}
onOpenChange={onOpenChange}
title="Create Group"
description="Create a new group to organize users and manage permissions."
className="max-w-md"
footer={
<>
<Button
type="button"
variant="outline"
onClick={() => onOpenChange(false)}
disabled={loading}
>
Cancel
</Button>
<Button type="submit" form="create-group-form" disabled={loading || !isFormValid || loadingAccounts}>
{loading && <Loader2 className="mr-2 h-4 w-4 animate-spin" />}
Create Group
</Button>
</>
}
>
{loadingAccounts ? (
<div className="flex justify-center py-8">
<Loader2 className="h-6 w-6 animate-spin text-muted-foreground" />
</div>
) : (
<form id="create-group-form" onSubmit={handleSubmit} className="space-y-4">
<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.id})
</SelectItem>
))}
</SelectContent>
</Select>
</div>
<div className="space-y-2">
<Label htmlFor="name">Name *</Label>
<Input
id="name"
value={name}
onChange={(e) => setName(e.target.value)}
placeholder="e.g., Managers, Developers"
required
disabled={loading}
maxLength={100}
/>
<p className="text-xs text-muted-foreground">
1-100 characters
</p>
</div>
<div className="space-y-2">
<Label htmlFor="description">Description</Label>
<Textarea
id="description"
value={description}
onChange={(e) => setDescription(e.target.value)}
placeholder="Optional description of the group's purpose"
disabled={loading}
maxLength={500}
rows={3}
/>
<p className="text-xs text-muted-foreground">
Optional, max 500 characters
</p>
</div>
{error && (
<div className="text-sm text-destructive bg-destructive/10 p-3 rounded-md">
{error}
</div>
)}
</form>
)}
</AppDialog>
);
}
|