2020-10-01 18:52:18 +00:00
|
|
|
import React, {useContext} from "react";
|
2020-09-25 18:21:18 +00:00
|
|
|
import {
|
|
|
|
AlertVariant,
|
|
|
|
Button,
|
|
|
|
Form,
|
|
|
|
FormGroup,
|
|
|
|
Modal,
|
|
|
|
ModalVariant,
|
|
|
|
TextInput
|
|
|
|
} from "@patternfly/react-core";
|
|
|
|
import { useTranslation } from "react-i18next";
|
|
|
|
import { HttpClientContext } from "../http-service/HttpClientContext";
|
|
|
|
import { RealmContext } from "../components/realm-context/RealmContext";
|
2020-09-28 21:09:34 +00:00
|
|
|
import { useAlerts } from "../components/alert/Alerts";
|
2020-10-01 18:52:18 +00:00
|
|
|
import { useForm } from "react-hook-form";
|
2020-09-25 18:21:18 +00:00
|
|
|
|
2020-10-01 18:52:18 +00:00
|
|
|
type GroupsCreateModalProps = {
|
|
|
|
handleModalToggle: () => void;
|
|
|
|
isCreateModalOpen: boolean;
|
|
|
|
setIsCreateModalOpen: (isCreateModalOpen: boolean) => void;
|
|
|
|
createGroupName: string;
|
|
|
|
setCreateGroupName: (createGroupName: string) => void;
|
|
|
|
};
|
|
|
|
|
|
|
|
export const GroupsCreateModal = ({handleModalToggle, isCreateModalOpen, setIsCreateModalOpen, createGroupName, setCreateGroupName}: GroupsCreateModalProps) => {
|
2020-09-25 18:21:18 +00:00
|
|
|
|
2020-09-28 21:09:34 +00:00
|
|
|
const { t } = useTranslation("groups");
|
2020-09-25 18:21:18 +00:00
|
|
|
const httpClient = useContext(HttpClientContext)!;
|
|
|
|
const { realm } = useContext(RealmContext);
|
|
|
|
const [add, Alerts] = useAlerts();
|
2020-10-01 18:52:18 +00:00
|
|
|
const form = useForm();
|
|
|
|
const { register, errors } = form;
|
2020-09-25 18:21:18 +00:00
|
|
|
|
2020-09-30 13:55:18 +00:00
|
|
|
const valueChange = (createGroupName: string) => {
|
|
|
|
setCreateGroupName(createGroupName)
|
2020-09-25 18:21:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const submitForm = async () => {
|
|
|
|
try {
|
2020-09-30 13:55:18 +00:00
|
|
|
await httpClient.doPost(`/admin/realms/${realm}/groups`, {name: createGroupName});
|
|
|
|
setIsCreateModalOpen(false);
|
|
|
|
setCreateGroupName("");
|
2020-10-01 18:52:18 +00:00
|
|
|
add(t("groupCreated"), AlertVariant.success);
|
2020-09-25 18:21:18 +00:00
|
|
|
} catch (error) {
|
2020-10-01 18:52:18 +00:00
|
|
|
add(`${t("couldNotCreateGroup")} ': '${error}'`, AlertVariant.danger);
|
2020-09-25 18:21:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
2020-09-30 13:55:18 +00:00
|
|
|
<React.Fragment>
|
|
|
|
<Alerts />
|
|
|
|
<Modal
|
|
|
|
variant={ModalVariant.small}
|
2020-10-01 18:52:18 +00:00
|
|
|
title={t("createAGroup")}
|
2020-09-30 13:55:18 +00:00
|
|
|
isOpen={isCreateModalOpen}
|
|
|
|
onClose={handleModalToggle}
|
|
|
|
actions={[
|
|
|
|
<Button key="confirm" variant="primary" onClick={() => submitForm()}>
|
2020-10-01 18:52:18 +00:00
|
|
|
{t("create")}
|
2020-09-30 13:55:18 +00:00
|
|
|
</Button>
|
|
|
|
]}
|
|
|
|
>
|
|
|
|
<Form isHorizontal>
|
2020-10-01 18:52:18 +00:00
|
|
|
<FormGroup
|
|
|
|
label={t("name")}
|
|
|
|
fieldId="group-id"
|
|
|
|
helperTextInvalid={t("common:required")}
|
|
|
|
validated={errors.name ? "error" : "default"}
|
|
|
|
isRequired
|
|
|
|
>
|
2020-09-30 13:55:18 +00:00
|
|
|
<TextInput
|
2020-10-01 18:52:18 +00:00
|
|
|
ref={register({ required: true })}
|
2020-09-30 13:55:18 +00:00
|
|
|
type="text"
|
|
|
|
id="create-group-name"
|
2020-10-01 18:52:18 +00:00
|
|
|
name="name"
|
2020-09-30 13:55:18 +00:00
|
|
|
value={createGroupName}
|
|
|
|
onChange={valueChange}
|
|
|
|
/>
|
|
|
|
</FormGroup>
|
|
|
|
</Form>
|
|
|
|
</Modal>
|
|
|
|
</React.Fragment>
|
2020-09-25 18:21:18 +00:00
|
|
|
);
|
|
|
|
};
|