UNPKG

@keycloakify/keycloak-admin-ui

Version:
51 lines 4.52 kB
import { jsx as _jsx, Fragment as _Fragment, jsxs as _jsxs } from "react/jsx-runtime"; import { FormErrorText, HelpItem, TextControl, useFetch, } from "../../../ui-shared"; import { Button, Checkbox, FormGroup } from "@patternfly/react-core"; import { MinusCircleIcon } from "@patternfly/react-icons"; import { Table, Tbody, Td, Th, Thead, Tr } from "@patternfly/react-table"; import { useState } from "react"; import { Controller, useFormContext } from "react-hook-form"; import { useTranslation } from "react-i18next"; import { useAdminClient } from "../../../admin-client"; import { GroupPickerDialog } from "../../../components/group/GroupPickerDialog"; export const Group = () => { const { adminClient } = useAdminClient(); const { t } = useTranslation(); const { control, getValues, setValue, formState: { errors }, } = useFormContext(); const values = getValues("groups"); const [open, setOpen] = useState(false); const [selectedGroups, setSelectedGroups] = useState([]); useFetch(() => { if (values && values.length > 0) return Promise.all(values.map((g) => adminClient.groups.findOne({ id: g.id }))); return Promise.resolve([]); }, (groups) => { const filteredGroup = groups.filter((g) => g); setSelectedGroups(filteredGroup); }, []); return (_jsxs(_Fragment, { children: [_jsx(TextControl, { name: "groupsClaim", label: t("groupsClaim"), labelIcon: t("groupsClaimHelp") }), _jsxs(FormGroup, { label: t("groups"), labelIcon: _jsx(HelpItem, { helpText: t("policyGroupsHelp"), fieldLabelId: "groups" }), fieldId: "groups", isRequired: true, children: [_jsx(Controller, { name: "groups", control: control, defaultValue: [], rules: { validate: (value) => value && value.filter(({ id }) => id).length > 0, }, render: ({ field }) => (_jsxs(_Fragment, { children: [open && (_jsx(GroupPickerDialog, { type: "selectMany", text: { title: "addGroupsToGroupPolicy", ok: "add", }, onConfirm: (groups) => { field.onChange([ ...(field.value || []), ...(groups || []).map(({ id }) => ({ id })), ]); setSelectedGroups([...selectedGroups, ...(groups || [])]); setOpen(false); }, onClose: () => { setOpen(false); }, filterGroups: selectedGroups })), _jsx(Button, { "data-testid": "select-group-button", variant: "secondary", onClick: () => { setOpen(true); }, children: t("addGroups") })] })) }), selectedGroups.length > 0 && (_jsxs(Table, { variant: "compact", children: [_jsx(Thead, { children: _jsxs(Tr, { children: [_jsx(Th, { children: t("groups") }), _jsx(Th, { children: t("extendToChildren") }), _jsx(Th, { "aria-hidden": "true" })] }) }), _jsx(Tbody, { children: selectedGroups.map((group, index) => (_jsxs(Tr, { children: [_jsx(Td, { children: group.path }), _jsx(Td, { children: _jsx(Controller, { name: `groups.${index}.extendChildren`, defaultValue: false, control: control, render: ({ field }) => (_jsx(Checkbox, { id: "extendChildren", "data-testid": "standard", name: "extendChildren", isChecked: field.value, onChange: field.onChange, isDisabled: group.subGroupCount === 0 })) }) }), _jsx(Td, { children: _jsx(Button, { variant: "link", className: "keycloak__client-authorization__policy-row-remove", icon: _jsx(MinusCircleIcon, {}), onClick: () => { setValue("groups", [ ...(values || []).filter(({ id }) => id !== group.id), ]); setSelectedGroups([ ...selectedGroups.filter(({ id }) => id !== group.id), ]); } }) })] }, group.id))) })] })), errors.groups && _jsx(FormErrorText, { message: t("requiredGroups") })] })] })); }; //# sourceMappingURL=Group.js.map