2021-03-24 14:07:49 +00:00
|
|
|
import React from "react";
|
|
|
|
import {
|
|
|
|
AlertVariant,
|
|
|
|
Button,
|
2021-03-31 06:58:55 +00:00
|
|
|
ButtonVariant,
|
2021-03-24 14:07:49 +00:00
|
|
|
Form,
|
|
|
|
FormGroup,
|
|
|
|
Modal,
|
|
|
|
ModalVariant,
|
|
|
|
TextInput,
|
|
|
|
ValidatedOptions,
|
|
|
|
} from "@patternfly/react-core";
|
|
|
|
import { useTranslation } from "react-i18next";
|
|
|
|
import { useForm } from "react-hook-form";
|
|
|
|
|
2021-05-04 17:58:18 +00:00
|
|
|
import type GroupRepresentation from "keycloak-admin/lib/defs/groupRepresentation";
|
2021-03-24 14:07:49 +00:00
|
|
|
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 (
|
|
|
|
<Modal
|
|
|
|
variant={ModalVariant.small}
|
|
|
|
title={t(rename ? "renameAGroup" : "createAGroup")}
|
|
|
|
isOpen={true}
|
|
|
|
onClose={handleModalToggle}
|
|
|
|
actions={[
|
|
|
|
<Button
|
|
|
|
data-testid={`${rename ? "rename" : "create"}Group`}
|
|
|
|
key="confirm"
|
|
|
|
variant="primary"
|
|
|
|
type="submit"
|
|
|
|
form="group-form"
|
|
|
|
>
|
|
|
|
{t(rename ? "rename" : "create")}
|
|
|
|
</Button>,
|
2021-03-31 06:58:55 +00:00
|
|
|
<Button
|
|
|
|
id="modal-cancel"
|
|
|
|
key="cancel"
|
2021-04-15 10:52:34 +00:00
|
|
|
variant={ButtonVariant.link}
|
2021-03-31 06:58:55 +00:00
|
|
|
onClick={() => {
|
|
|
|
handleModalToggle();
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
{t("common:cancel")}
|
|
|
|
</Button>,
|
2021-03-24 14:07:49 +00:00
|
|
|
]}
|
|
|
|
>
|
|
|
|
<Form id="group-form" isHorizontal onSubmit={handleSubmit(submitForm)}>
|
|
|
|
<FormGroup
|
|
|
|
name="create-modal-group"
|
|
|
|
label={t("common:name")}
|
|
|
|
fieldId="group-id"
|
|
|
|
helperTextInvalid={t("common:required")}
|
|
|
|
validated={
|
|
|
|
errors.name ? ValidatedOptions.error : ValidatedOptions.default
|
|
|
|
}
|
|
|
|
isRequired
|
|
|
|
>
|
|
|
|
<TextInput
|
|
|
|
data-testid="groupNameInput"
|
|
|
|
ref={register({ required: true })}
|
|
|
|
autoFocus
|
|
|
|
type="text"
|
|
|
|
id="create-group-name"
|
|
|
|
name="name"
|
|
|
|
validated={
|
|
|
|
errors.name ? ValidatedOptions.error : ValidatedOptions.default
|
|
|
|
}
|
|
|
|
/>
|
|
|
|
</FormGroup>
|
|
|
|
</Form>
|
|
|
|
</Modal>
|
|
|
|
);
|
|
|
|
};
|