import React from "react"; import { AlertVariant, Button, ButtonVariant, Form, FormGroup, Modal, ModalVariant, TextInput, ValidatedOptions, } from "@patternfly/react-core"; import { useTranslation } from "react-i18next"; import { useForm } from "react-hook-form"; import type GroupRepresentation from "keycloak-admin/lib/defs/groupRepresentation"; import { useAdminClient } from "../context/auth/AdminClient"; import { useAlerts } from "../components/alert/Alerts"; type GroupsModalProps = { id?: string; rename?: string; handleModalToggle: () => void; refresh: (group?: GroupRepresentation) => void; }; export const GroupsModal = ({ id, rename, handleModalToggle, refresh, }: GroupsModalProps) => { const { t } = useTranslation("groups"); const adminClient = useAdminClient(); const { addAlert } = useAlerts(); const { register, errors, handleSubmit } = useForm({ defaultValues: { name: rename }, }); const submitForm = async (group: GroupRepresentation) => { try { if (!id) { await adminClient.groups.create(group); } else if (rename) { await adminClient.groups.update({ id }, group); } else { await adminClient.groups.setOrCreateChild({ id }, group); } refresh(rename ? group : undefined); handleModalToggle(); addAlert( t(rename ? "groupUpdated" : "groupCreated"), AlertVariant.success ); } catch (error) { addAlert(t("couldNotCreateGroup", { error }), AlertVariant.danger); } }; return ( {t(rename ? "rename" : "create")} , , ]} >
); };