@keycloakify/keycloak-admin-ui
Version:
Repackaged Keycloak Admin UI
93 lines • 5.47 kB
JavaScript
import { jsx as _jsx, Fragment as _Fragment, jsxs as _jsxs } from "react/jsx-runtime";
import { HelpItem, useAlerts } from "../../ui-shared";
import { AlertVariant, Button, ButtonVariant } from "@patternfly/react-core";
import { useState } from "react";
import { useTranslation } from "react-i18next";
import { Link, useNavigate } from "react-router-dom";
import { useAdminClient } from "../../admin-client";
import { useAccess } from "../../context/access/Access";
import { useRealm } from "../../context/realm-context/RealmContext";
import { toRealmSettings } from "../../realm-settings/routes/RealmSettings";
import { emptyFormatter, upperCaseFormatter } from "../../util";
import { translationFormatter } from "../../utils/translationFormatter";
import { useConfirmDialog } from "../../components/confirm-dialog/ConfirmDialog";
import { ListEmptyState } from "../../ui-shared";
import { KeycloakDataTable } from "../../ui-shared";
import "./RolesList.css";
const RoleDetailLink = ({ defaultRoleName, toDetail, messageBundle, ...role }) => {
const { t } = useTranslation(messageBundle);
const { realm } = useRealm();
const { hasAccess, hasSomeAccess } = useAccess();
const canViewUserRegistration = hasAccess("view-realm") && hasSomeAccess("view-clients", "manage-clients");
return role.name !== defaultRoleName ? (_jsx(Link, { to: toDetail(role.id), children: role.name })) : (_jsxs(_Fragment, { children: [canViewUserRegistration ? (_jsx(Link, { to: toRealmSettings({ realm, tab: "user-registration" }), children: role.name })) : (_jsx("span", { children: role.name })), _jsx(HelpItem, { helpText: t(`${messageBundle}:defaultRole`), fieldLabelId: "defaultRole" })] }));
};
export const RolesList = ({ loader, paginated = true, parentRoleId, messageBundle = "roles", toCreate, toDetail, isReadOnly, }) => {
const { adminClient } = useAdminClient();
const { t } = useTranslation();
const navigate = useNavigate();
const { addAlert, addError } = useAlerts();
const { realmRepresentation: realm } = useRealm();
const [selectedRole, setSelectedRole] = useState();
const [toggleDeleteDialog, DeleteConfirm] = useConfirmDialog({
titleKey: "roleDeleteConfirm",
messageKey: t("roleDeleteConfirmDialog", {
selectedRoleName: selectedRole ? selectedRole.name : "",
}),
continueButtonLabel: "delete",
continueButtonVariant: ButtonVariant.danger,
onConfirm: async () => {
try {
if (!parentRoleId) {
await adminClient.roles.delById({
id: selectedRole.id,
});
}
else {
await adminClient.roles.delCompositeRoles({ id: parentRoleId }, [
selectedRole,
]);
}
setSelectedRole(undefined);
addAlert(t("roleDeletedSuccess"), AlertVariant.success);
}
catch (error) {
addError("roleDeleteError", error);
}
},
});
return (_jsxs(_Fragment, { children: [_jsx(DeleteConfirm, {}), _jsx(KeycloakDataTable, { loader: loader, ariaLabelKey: "roleList", searchPlaceholderKey: "searchForRoles", isPaginated: paginated, toolbarItem: !isReadOnly && (_jsx(Button, { "data-testid": "create-role", component: (props) => _jsx(Link, { ...props, to: toCreate }), children: t("createRole") })), actions: isReadOnly
? undefined
: [
{
title: t("delete"),
onRowClick: (role) => {
setSelectedRole(role);
if ((realm === null || realm === void 0 ? void 0 : realm.defaultRole) &&
role.name === realm.defaultRole.name) {
addAlert(t("defaultRoleDeleteError"), AlertVariant.danger);
}
else
toggleDeleteDialog();
},
},
], columns: [
{
name: "name",
displayKey: "roleName",
cellRenderer: (row) => {
var _a;
return (_jsx(RoleDetailLink, { ...row, defaultRoleName: (_a = realm === null || realm === void 0 ? void 0 : realm.defaultRole) === null || _a === void 0 ? void 0 : _a.name, toDetail: toDetail, messageBundle: messageBundle }));
},
},
{
name: "composite",
displayKey: "composite",
cellFormatters: [upperCaseFormatter(), emptyFormatter()],
},
{
name: "description",
cellFormatters: [translationFormatter(t)],
},
], emptyState: _jsx(ListEmptyState, { hasIcon: true, message: t(`noRoles-${messageBundle}`), instructions: isReadOnly ? "" : t(`noRolesInstructions-${messageBundle}`), primaryActionText: isReadOnly ? "" : t("createRole"), onPrimaryAction: () => navigate(toCreate) }) }, selectedRole ? selectedRole.id : "roleList")] }));
};
//# sourceMappingURL=RolesList.js.map