UNPKG

@keycloakify/keycloak-admin-ui

Version:
67 lines 3.53 kB
import { jsx as _jsx } from "react/jsx-runtime"; import { useState } from "react"; import { useTranslation } from "react-i18next"; import { toUpperCase } from "../../util"; import { DropdownItem, MenuToggle, Select, SelectOption, } from "@patternfly/react-core"; export var ClientScope; (function (ClientScope) { ClientScope["default"] = "default"; ClientScope["optional"] = "optional"; })(ClientScope || (ClientScope = {})); export var AllClientScopes; (function (AllClientScopes) { AllClientScopes["none"] = "none"; })(AllClientScopes || (AllClientScopes = {})); const clientScopeTypes = Object.keys(ClientScope); export const allClientScopeTypes = Object.keys({ ...AllClientScopes, ...ClientScope, }); export const clientScopeTypesSelectOptions = (t, scopeTypes = clientScopeTypes) => scopeTypes.map((type) => (_jsx(SelectOption, { value: type, children: t(`clientScopeType.${type}`) }, type))); export const clientScopeTypesDropdown = (t, onClick) => clientScopeTypes.map((type) => (_jsx(DropdownItem, { onClick: () => onClick(type), children: t(`clientScopeType.${type}`) }, type))); export const CellDropdown = ({ clientScope, type, onSelect, all = false, isDisabled, ...props }) => { const { t } = useTranslation(); const [open, setOpen] = useState(false); return (_jsx(Select, { toggle: (ref) => (_jsx(MenuToggle, { "data-testid": "cell-dropdown", className: `keycloak__client-scope__${type}`, ref: ref, onClick: () => setOpen(!open), isExpanded: open, isDisabled: isDisabled, children: t(`clientScopeType.${type}`) })), isOpen: open, onOpenChange: (isOpen) => setOpen(isOpen), selected: [type], onSelect: (_, value) => { onSelect(all ? value : value); setOpen(false); }, ...props, children: clientScopeTypesSelectOptions(t, all ? allClientScopeTypes : clientScopeTypes) }, clientScope.id)); }; export const changeScope = async (adminClient, clientScope, changeTo) => { await removeScope(adminClient, clientScope); await addScope(adminClient, clientScope, changeTo); }; const castAdminClient = (adminClient) => adminClient.clientScopes; export const removeScope = async (adminClient, clientScope) => { if (clientScope.type !== AllClientScopes.none) await castAdminClient(adminClient)[`delDefault${clientScope.type === ClientScope.optional ? "Optional" : ""}ClientScope`]({ id: clientScope.id, }); }; const addScope = async (adminClient, clientScope, type) => { if (type !== AllClientScopes.none) await castAdminClient(adminClient)[`addDefault${type === ClientScope.optional ? "Optional" : ""}ClientScope`]({ id: clientScope.id, }); }; export const changeClientScope = async (adminClient, clientId, clientScope, type, changeTo) => { if (type !== "none") { await removeClientScope(adminClient, clientId, clientScope, type); } await addClientScope(adminClient, clientId, clientScope, changeTo); }; export const removeClientScope = async (adminClient, clientId, clientScope, type) => { const methodName = `del${toUpperCase(type)}ClientScope`; await adminClient.clients[methodName]({ id: clientId, clientScopeId: clientScope.id, }); }; export const addClientScope = async (adminClient, clientId, clientScope, type) => { const methodName = `add${toUpperCase(type)}ClientScope`; await adminClient.clients[methodName]({ id: clientId, clientScopeId: clientScope.id, }); }; //# sourceMappingURL=ClientScopeTypes.js.map