keycloak-scim/src/groups/GroupsCreateModal.tsx

69 lines
2 KiB
TypeScript
Raw Normal View History

2020-09-25 18:21:18 +00:00
import React, {useState, useContext} from "react";
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-09-25 18:21:18 +00:00
2020-09-30 13:55:18 +00:00
export const GroupsCreateModal = ({isCreateModalOpen, handleModalToggle, setIsCreateModalOpen, createGroupName, setCreateGroupName}) => {
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 [ nameValue, setNameValue ] = useState("");
const [add, Alerts] = useAlerts();
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-09-25 18:21:18 +00:00
add("Client created", AlertVariant.success);
2020-09-30 13:55:18 +00:00
// window.location.reload(false);
2020-09-25 18:21:18 +00:00
} catch (error) {
add(`Could not create client: '${error}'`, AlertVariant.danger);
}
}
return (
2020-09-30 13:55:18 +00:00
<React.Fragment>
<Alerts />
<Modal
variant={ModalVariant.small}
title="Create a group"
isOpen={isCreateModalOpen}
onClose={handleModalToggle}
actions={[
<Button key="confirm" variant="primary" onClick={() => submitForm()}>
Create
</Button>
]}
>
<Form isHorizontal>
<FormGroup label={t("name")} fieldId="kc-root-url">
<TextInput
type="text"
id="create-group-name"
name="Name"
value={createGroupName}
onChange={valueChange}
/>
</FormGroup>
</Form>
</Modal>
</React.Fragment>
2020-09-25 18:21:18 +00:00
);
};