Admin: RBAC faza 2 M2 - konsola ról gymowych z grantów (nowa strona ról, modal członkostwa na PUT roleKeys, testy e2e i unit)
This commit is contained in:
@@ -1,22 +1,32 @@
|
||||
import { useState } from 'react';
|
||||
import { useMemo, useState } from 'react';
|
||||
import { Link } from 'react-router-dom';
|
||||
import { useTranslation } from 'react-i18next';
|
||||
import { api } from '../api/client';
|
||||
import { useGymRoles } from '../api/queries';
|
||||
import { apiErrorMessage } from '../lib/errors';
|
||||
import type { GymRole } from '../api/types';
|
||||
import { GymRolePicker } from './GymRolePicker';
|
||||
import { GYM_ROLE_META } from '../lib/roles';
|
||||
import { Button, ErrorText, Field, Modal, Muted, Textarea } from './ui';
|
||||
import { GYM_ROLE_ORDER } from '../lib/roles';
|
||||
|
||||
const GYM_OWNER_KEY = 'GYM_OWNER';
|
||||
const GYM_TRAINER_KEY = 'GYM_TRAINER';
|
||||
|
||||
/** Etykieta roli: systemowa z tłumaczeniem legacy, custom z nazwą zdefiniowaną przez admina. */
|
||||
function roleLabel(key: string, name: string, t: (k: string) => string): string {
|
||||
if (key === GYM_OWNER_KEY) return t(`common:${GYM_ROLE_META.OWNER.labelKey}`);
|
||||
if (key === GYM_TRAINER_KEY) return t(`common:${GYM_ROLE_META.TRAINER.labelKey}`);
|
||||
return name;
|
||||
}
|
||||
|
||||
/**
|
||||
* Wspólne menu członkostwa (konsola siłowni + profil usera): pełna lista ról (PATCH)
|
||||
* oraz wyrzucenie z siłowni z wymagalnym powodem (DELETE + audyt membership.deleted).
|
||||
* Wspólne menu członkostwa (konsola siłowni + profil usera): przydzielanie ról
|
||||
* z grantów (PUT /admin/gyms/:gymId/members/:userId/roles — RBAC faza 2) oraz
|
||||
* wyrzucenie z siłowni z wymagalnym powodem (DELETE + audyt membership.deleted).
|
||||
*/
|
||||
export function MembershipActionsModal({
|
||||
userId,
|
||||
titleName,
|
||||
gymId,
|
||||
roles: initialRoles,
|
||||
roleKeys: initialRoleKeys,
|
||||
showProfileLink = true,
|
||||
onClose,
|
||||
onChanged,
|
||||
@@ -24,27 +34,37 @@ export function MembershipActionsModal({
|
||||
userId: string;
|
||||
titleName: string;
|
||||
gymId: string;
|
||||
roles: string[];
|
||||
/** Klucze grantów ról gymowych członka (GET members / list users). */
|
||||
roleKeys: string[];
|
||||
showProfileLink?: boolean;
|
||||
onClose: () => void;
|
||||
onChanged: () => void;
|
||||
}) {
|
||||
const { t } = useTranslation('gyms');
|
||||
const [roles, setRoles] = useState<GymRole[]>(
|
||||
GYM_ROLE_ORDER.filter((r) => initialRoles.includes(r)),
|
||||
);
|
||||
const { data: catalog } = useGymRoles(gymId);
|
||||
const [selected, setSelected] = useState<string[]>(initialRoleKeys);
|
||||
const [reason, setReason] = useState('');
|
||||
const [busy, setBusy] = useState<'' | 'roles' | 'remove'>('');
|
||||
const [error, setError] = useState('');
|
||||
|
||||
// katalog: systemowe role gymowe tej siłowni + role własne (GYM_OWNER już w katalogu)
|
||||
const roleOptions = useMemo(() => {
|
||||
const defs = (catalog?.roles ?? []).filter((r) =>
|
||||
r.isSystem ? r.key === GYM_OWNER_KEY || r.key === GYM_TRAINER_KEY : true,
|
||||
);
|
||||
return defs.map((r) => ({ key: r.key, label: roleLabel(r.key, r.name, t) }));
|
||||
}, [catalog, t]);
|
||||
|
||||
const toggle = (key: string) =>
|
||||
setSelected((ks) => (ks.includes(key) ? ks.filter((k) => k !== key) : [...ks, key]));
|
||||
|
||||
const saveRoles = async () => {
|
||||
if (roles.length === 0) return;
|
||||
setError('');
|
||||
setBusy('roles');
|
||||
try {
|
||||
await api(`/admin/users/${userId}/memberships/${gymId}`, {
|
||||
method: 'PATCH',
|
||||
body: { roles },
|
||||
await api(`/admin/gyms/${gymId}/members/${userId}/roles`, {
|
||||
method: 'PUT',
|
||||
body: { roleKeys: selected },
|
||||
});
|
||||
onChanged();
|
||||
onClose();
|
||||
@@ -91,18 +111,26 @@ export function MembershipActionsModal({
|
||||
<div className="mb-2 text-[10.5px] font-bold uppercase tracking-[0.11em] text-dim">
|
||||
{t('memberRolesTitle')}
|
||||
</div>
|
||||
<GymRolePicker
|
||||
value={roles}
|
||||
onChange={setRoles}
|
||||
labelPrefix={titleName}
|
||||
showExplain={false}
|
||||
/>
|
||||
<div className="flex flex-col gap-1 rounded-xl border border-line bg-input/40 p-3">
|
||||
{roleOptions.length === 0 && <Muted className="text-[0.85rem]">{t('rolesLoading')}</Muted>}
|
||||
{roleOptions.map((o) => (
|
||||
<label key={o.key} className="flex items-center gap-2.5 text-[0.9rem]">
|
||||
<input
|
||||
type="checkbox"
|
||||
className="h-4 w-4"
|
||||
checked={selected.includes(o.key)}
|
||||
onChange={() => toggle(o.key)}
|
||||
/>
|
||||
<span className="min-w-0">
|
||||
<span>{o.label}</span>
|
||||
<span className="block font-mono text-[0.7rem] text-dim">{o.key}</span>
|
||||
</span>
|
||||
</label>
|
||||
))}
|
||||
</div>
|
||||
<p className="mt-1.5 text-[0.78rem] text-dim">{t('memberRolesNote')}</p>
|
||||
<div className="mt-3">
|
||||
<Button
|
||||
size="sm"
|
||||
onClick={() => void saveRoles()}
|
||||
disabled={busy !== '' || roles.length === 0}
|
||||
>
|
||||
<Button size="sm" onClick={() => void saveRoles()} disabled={busy !== ''}>
|
||||
{busy === 'roles' ? t('common:saving') : t('saveRoles')}
|
||||
</Button>
|
||||
</div>
|
||||
|
||||
Reference in New Issue
Block a user