Adds user management
This commit is contained in:
@@ -0,0 +1,63 @@
|
||||
import { useTranslation } from 'react-i18next';
|
||||
import { Button } from '@/components/ui/button';
|
||||
import {
|
||||
Dialog,
|
||||
DialogContent,
|
||||
DialogHeader,
|
||||
DialogTitle,
|
||||
DialogFooter,
|
||||
DialogDescription,
|
||||
} from '@/components/ui/dialog';
|
||||
import type { UserListItem } from '@/api/types';
|
||||
|
||||
interface DeleteUserDialogProps {
|
||||
user: UserListItem | null;
|
||||
open: boolean;
|
||||
onOpenChange: (open: boolean) => void;
|
||||
onConfirm: () => void;
|
||||
isPending: boolean;
|
||||
}
|
||||
|
||||
export function DeleteUserDialog({
|
||||
user,
|
||||
open,
|
||||
onOpenChange,
|
||||
onConfirm,
|
||||
isPending,
|
||||
}: DeleteUserDialogProps) {
|
||||
const { t } = useTranslation();
|
||||
if (!user) return null;
|
||||
|
||||
return (
|
||||
<Dialog open={open} onOpenChange={onOpenChange}>
|
||||
<DialogContent className="sm:max-w-md" data-testid="delete-user-dialog">
|
||||
<DialogHeader>
|
||||
<DialogTitle>{t('users.delete.title')}</DialogTitle>
|
||||
<DialogDescription>
|
||||
{user.invitationPending
|
||||
? t('users.delete.descriptionPending', { email: user.email })
|
||||
: t('users.delete.description', { name: user.name, email: user.email })}
|
||||
</DialogDescription>
|
||||
</DialogHeader>
|
||||
<DialogFooter className="gap-2">
|
||||
<Button
|
||||
variant="outline"
|
||||
onClick={() => onOpenChange(false)}
|
||||
disabled={isPending}
|
||||
data-testid="delete-user-cancel"
|
||||
>
|
||||
{t('common.cancel')}
|
||||
</Button>
|
||||
<Button
|
||||
variant="destructive"
|
||||
onClick={onConfirm}
|
||||
disabled={isPending}
|
||||
data-testid="delete-user-confirm"
|
||||
>
|
||||
{isPending ? '...' : t('users.delete.confirmButton')}
|
||||
</Button>
|
||||
</DialogFooter>
|
||||
</DialogContent>
|
||||
</Dialog>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,90 @@
|
||||
import { beforeEach, describe, expect, it } from 'vitest';
|
||||
import { screen } from '@testing-library/react';
|
||||
import userEvent from '@testing-library/user-event';
|
||||
import { http, HttpResponse } from 'msw';
|
||||
import { renderApp, mockAuthenticated } from '@/test/utils';
|
||||
import { server } from '@/mocks/server';
|
||||
import { API_BASE } from '@/mocks/auth/fixtures';
|
||||
import { _resetSetupStatusCache } from '@/router';
|
||||
|
||||
beforeEach(() => {
|
||||
_resetSetupStatusCache();
|
||||
});
|
||||
|
||||
async function openDialog() {
|
||||
mockAuthenticated();
|
||||
renderApp('/users');
|
||||
await screen.findByTestId('users-invite-button', {}, { timeout: 5000 });
|
||||
await userEvent.click(screen.getByTestId('users-invite-button'));
|
||||
expect(screen.getByTestId('invite-dialog-email')).toBeInTheDocument();
|
||||
}
|
||||
|
||||
describe('InviteUserDialog', () => {
|
||||
it('shows email and role fields on open', async () => {
|
||||
await openDialog();
|
||||
expect(screen.getByTestId('invite-dialog-email')).toBeInTheDocument();
|
||||
expect(screen.getByTestId('invite-dialog-role')).toBeInTheDocument();
|
||||
expect(screen.getByTestId('invite-dialog-submit')).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('shows validation error when submitting with empty email', async () => {
|
||||
await openDialog();
|
||||
await userEvent.click(screen.getByTestId('invite-dialog-submit'));
|
||||
expect(await screen.findByTestId('invite-email-error')).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('shows validation error when submitting without selecting a role', async () => {
|
||||
await openDialog();
|
||||
await userEvent.type(screen.getByTestId('invite-dialog-email'), 'new@example.com');
|
||||
await userEvent.click(screen.getByTestId('invite-dialog-submit'));
|
||||
expect(await screen.findByTestId('invite-role-error')).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('advances to step 2 (invite link) after successful submission', async () => {
|
||||
await openDialog();
|
||||
|
||||
await userEvent.type(screen.getByTestId('invite-dialog-email'), 'new@example.com');
|
||||
|
||||
// Open role dropdown and select "User" (use role=option to avoid ambiguity)
|
||||
await userEvent.click(screen.getByTestId('invite-dialog-role'));
|
||||
const options = await screen.findAllByRole('option');
|
||||
const userOption = options.find((o) => o.textContent === 'User');
|
||||
await userEvent.click(userOption!);
|
||||
|
||||
await userEvent.click(screen.getByTestId('invite-dialog-submit'));
|
||||
|
||||
expect(await screen.findByTestId('invite-dialog-link-input')).toBeInTheDocument();
|
||||
expect(screen.getByTestId('invite-dialog-copy')).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('shows error banner when API returns 400', async () => {
|
||||
server.use(
|
||||
http.post(`${API_BASE}/api/v1/Users/invite`, () =>
|
||||
HttpResponse.json({ detail: 'User already exists.' }, { status: 400 }),
|
||||
),
|
||||
);
|
||||
await openDialog();
|
||||
|
||||
await userEvent.type(screen.getByTestId('invite-dialog-email'), 'dup@example.com');
|
||||
await userEvent.click(screen.getByTestId('invite-dialog-role'));
|
||||
const opts = await screen.findAllByRole('option');
|
||||
const userOpt = opts.find((o) => o.textContent === 'User');
|
||||
await userEvent.click(userOpt!);
|
||||
await userEvent.click(screen.getByTestId('invite-dialog-submit'));
|
||||
|
||||
expect(await screen.findByTestId('form-error-banner')).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('resets to step 1 when dialog is closed and reopened', async () => {
|
||||
await openDialog();
|
||||
await userEvent.type(screen.getByTestId('invite-dialog-email'), 'test@example.com');
|
||||
|
||||
// Close the dialog by pressing Escape
|
||||
await userEvent.keyboard('{Escape}');
|
||||
|
||||
// Reopen
|
||||
await userEvent.click(screen.getByTestId('users-invite-button'));
|
||||
const emailInput = screen.getByTestId('invite-dialog-email') as HTMLInputElement;
|
||||
expect(emailInput.value).toBe('');
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,187 @@
|
||||
import { useEffect, useState } from 'react';
|
||||
import { useForm } from 'react-hook-form';
|
||||
import { zodResolver } from '@hookform/resolvers/zod';
|
||||
import { z } from 'zod';
|
||||
import { toast } from 'sonner';
|
||||
import { useTranslation } from 'react-i18next';
|
||||
import { Button } from '@/components/ui/button';
|
||||
import { Input } from '@/components/ui/input';
|
||||
import { Label } from '@/components/ui/label';
|
||||
import {
|
||||
Dialog,
|
||||
DialogContent,
|
||||
DialogHeader,
|
||||
DialogTitle,
|
||||
} from '@/components/ui/dialog';
|
||||
import {
|
||||
Select,
|
||||
SelectContent,
|
||||
SelectItem,
|
||||
SelectTrigger,
|
||||
SelectValue,
|
||||
} from '@/components/ui/select';
|
||||
import { FormErrorBanner } from '@/components/ui/FormErrorBanner';
|
||||
import { FieldError } from '@/components/ui/FieldError';
|
||||
import { useInviteUser } from '@/api/useUsers';
|
||||
|
||||
const inviteSchema = z.object({
|
||||
email: z.string().email(),
|
||||
role: z.enum(['Administrator', 'User']),
|
||||
});
|
||||
type InviteFormData = z.infer<typeof inviteSchema>;
|
||||
|
||||
interface InviteUserDialogProps {
|
||||
open: boolean;
|
||||
onOpenChange: (open: boolean) => void;
|
||||
}
|
||||
|
||||
export function InviteUserDialog({ open, onOpenChange }: InviteUserDialogProps) {
|
||||
const { t } = useTranslation();
|
||||
const [step, setStep] = useState<1 | 2>(1);
|
||||
const [inviteLink, setInviteLink] = useState<string | null>(null);
|
||||
const inviteUser = useInviteUser();
|
||||
|
||||
useEffect(() => {
|
||||
if (!open) {
|
||||
setStep(1);
|
||||
setInviteLink(null);
|
||||
inviteUser.reset();
|
||||
reset();
|
||||
}
|
||||
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||||
}, [open]);
|
||||
|
||||
const {
|
||||
register,
|
||||
handleSubmit,
|
||||
setValue,
|
||||
reset,
|
||||
formState: { errors, isSubmitting },
|
||||
} = useForm<InviteFormData>({
|
||||
resolver: zodResolver(inviteSchema),
|
||||
});
|
||||
|
||||
const onSubmit = handleSubmit(async (values) => {
|
||||
try {
|
||||
const response = await inviteUser.mutateAsync(values);
|
||||
setInviteLink(response.inviteLink);
|
||||
setStep(2);
|
||||
} catch {
|
||||
// error shown via inviteUser.error
|
||||
}
|
||||
});
|
||||
|
||||
const handleCopy = async () => {
|
||||
if (!inviteLink) return;
|
||||
await navigator.clipboard.writeText(`${window.location.origin}${inviteLink}`);
|
||||
toast.success(t('users.actions.linkCopied'));
|
||||
};
|
||||
|
||||
return (
|
||||
<Dialog open={open} onOpenChange={onOpenChange}>
|
||||
<DialogContent className="sm:max-w-md">
|
||||
<DialogHeader>
|
||||
<DialogTitle>
|
||||
{step === 1 ? t('users.invite.title') : t('users.invite.step2Title')}
|
||||
</DialogTitle>
|
||||
</DialogHeader>
|
||||
|
||||
{step === 1 && (
|
||||
<form onSubmit={onSubmit} noValidate className="space-y-4">
|
||||
<FormErrorBanner
|
||||
error={inviteUser.error ? { message: inviteUser.error.message } : null}
|
||||
onDismiss={() => inviteUser.reset()}
|
||||
/>
|
||||
|
||||
<div className="space-y-2">
|
||||
<Label htmlFor="invite-email">{t('users.invite.emailLabel')}</Label>
|
||||
<Input
|
||||
id="invite-email"
|
||||
type="email"
|
||||
placeholder="user@example.com"
|
||||
data-testid="invite-dialog-email"
|
||||
aria-invalid={errors.email !== undefined}
|
||||
{...register('email')}
|
||||
/>
|
||||
{errors.email && (
|
||||
<FieldError message={errors.email.message} testId="invite-email-error" />
|
||||
)}
|
||||
</div>
|
||||
|
||||
<div className="space-y-2">
|
||||
<Label htmlFor="invite-role">{t('users.invite.roleLabel')}</Label>
|
||||
<Select
|
||||
onValueChange={(val) =>
|
||||
setValue('role', val as 'Administrator' | 'User', {
|
||||
shouldValidate: true,
|
||||
})
|
||||
}
|
||||
>
|
||||
<SelectTrigger
|
||||
id="invite-role"
|
||||
data-testid="invite-dialog-role"
|
||||
aria-invalid={errors.role !== undefined}
|
||||
>
|
||||
<SelectValue placeholder={t('users.invite.rolePlaceholder')} />
|
||||
</SelectTrigger>
|
||||
<SelectContent>
|
||||
<SelectItem value="Administrator">
|
||||
{t('users.roles.Administrator')}
|
||||
</SelectItem>
|
||||
<SelectItem value="User">{t('users.roles.User')}</SelectItem>
|
||||
</SelectContent>
|
||||
</Select>
|
||||
{errors.role && (
|
||||
<FieldError message={errors.role.message} testId="invite-role-error" />
|
||||
)}
|
||||
</div>
|
||||
|
||||
<Button
|
||||
type="submit"
|
||||
className="w-full"
|
||||
disabled={isSubmitting || inviteUser.isPending}
|
||||
data-testid="invite-dialog-submit"
|
||||
>
|
||||
{isSubmitting || inviteUser.isPending
|
||||
? '...'
|
||||
: t('users.invite.submitButton')}
|
||||
</Button>
|
||||
</form>
|
||||
)}
|
||||
|
||||
{step === 2 && (
|
||||
<div className="space-y-4">
|
||||
<p className="text-sm text-muted-foreground">
|
||||
{t('users.invite.step2Description')}
|
||||
</p>
|
||||
<div className="space-y-2">
|
||||
<Label>{t('users.invite.linkLabel')}</Label>
|
||||
<Input
|
||||
value={`${window.location.origin}${inviteLink ?? ''}`}
|
||||
readOnly
|
||||
data-testid="invite-dialog-link-input"
|
||||
/>
|
||||
</div>
|
||||
<div className="flex gap-2">
|
||||
<Button
|
||||
variant="outline"
|
||||
className="flex-1"
|
||||
onClick={handleCopy}
|
||||
data-testid="invite-dialog-copy"
|
||||
>
|
||||
{t('users.invite.copyLink')}
|
||||
</Button>
|
||||
<Button
|
||||
className="flex-1"
|
||||
onClick={() => onOpenChange(false)}
|
||||
data-testid="invite-dialog-close"
|
||||
>
|
||||
{t('users.invite.close')}
|
||||
</Button>
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
</DialogContent>
|
||||
</Dialog>
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user