import React, { useContext } from "react"; import { AlertVariant, Button, Form, FormGroup, Modal, ModalVariant, TextInput, } from "@patternfly/react-core"; import { useTranslation } from "react-i18next"; import { HttpClientContext } from "../context/http-service/HttpClientContext"; import { RealmContext } from "../context/realm-context/RealmContext"; import { useAlerts } from "../components/alert/Alerts"; import { useForm } from "react-hook-form"; type GroupsCreateModalProps = { handleModalToggle: () => void; isCreateModalOpen: boolean; setIsCreateModalOpen: (isCreateModalOpen: boolean) => void; createGroupName: string; setCreateGroupName: (createGroupName: string) => void; refresh: () => void; }; export const GroupsCreateModal = ({ handleModalToggle, isCreateModalOpen, setIsCreateModalOpen, createGroupName, setCreateGroupName, refresh }: GroupsCreateModalProps) => { const { t } = useTranslation("groups"); const httpClient = useContext(HttpClientContext)!; const { realm } = useContext(RealmContext); const { addAlert } = useAlerts(); const form = useForm(); const { register, errors } = form; const valueChange = (createGroupName: string) => { setCreateGroupName(createGroupName); }; const submitForm = async () => { if (await form.trigger()) { try { await httpClient.doPost(`/admin/realms/${realm}/groups`, { name: createGroupName, }); setIsCreateModalOpen(false); setCreateGroupName(""); refresh(); addAlert(t("groupCreated"), AlertVariant.success); } catch (error) { addAlert(`${t("couldNotCreateGroup")} ': '${error}'`, AlertVariant.danger); } } }; return ( <> submitForm()}> {t("create")} , ]} >
); };