2020-10-01 19:15:23 +00:00
|
|
|
import React, { useContext } from "react";
|
2020-09-25 18:21:18 +00:00
|
|
|
import {
|
|
|
|
AlertVariant,
|
|
|
|
Button,
|
|
|
|
Form,
|
|
|
|
FormGroup,
|
|
|
|
Modal,
|
|
|
|
ModalVariant,
|
2020-10-01 19:15:23 +00:00
|
|
|
TextInput,
|
2020-09-25 18:21:18 +00:00
|
|
|
} from "@patternfly/react-core";
|
|
|
|
import { useTranslation } from "react-i18next";
|
2020-10-06 14:12:05 +00:00
|
|
|
import { HttpClientContext } from "../context/http-service/HttpClientContext";
|
|
|
|
import { RealmContext } from "../context/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;
|
|
|
|
};
|
|
|
|
|
2020-10-01 19:15:23 +00:00
|
|
|
export const GroupsCreateModal = ({
|
|
|
|
handleModalToggle,
|
|
|
|
isCreateModalOpen,
|
|
|
|
setIsCreateModalOpen,
|
|
|
|
createGroupName,
|
|
|
|
setCreateGroupName,
|
|
|
|
}: GroupsCreateModalProps) => {
|
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) => {
|
2020-10-01 19:15:23 +00:00
|
|
|
setCreateGroupName(createGroupName);
|
|
|
|
};
|
2020-09-25 18:21:18 +00:00
|
|
|
|
|
|
|
const submitForm = async () => {
|
2020-10-05 19:45:06 +00:00
|
|
|
if (await form.trigger()) {
|
2020-09-25 18:21:18 +00:00
|
|
|
try {
|
2020-10-05 19:45:06 +00:00
|
|
|
httpClient.doPost(`/admin/realms/${realm}/groups`, {
|
2020-10-01 19:15:23 +00:00
|
|
|
name: createGroupName,
|
|
|
|
});
|
2020-09-30 13:55:18 +00:00
|
|
|
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
|
|
|
}
|
2020-10-05 19:45:06 +00:00
|
|
|
}
|
|
|
|
};
|
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-10-01 19:15:23 +00:00
|
|
|
</Button>,
|
2020-09-30 13:55:18 +00:00
|
|
|
]}
|
|
|
|
>
|
|
|
|
<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
|
|
|
);
|
|
|
|
};
|