2020-11-12 12:55:52 +00:00
|
|
|
import React 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-11-09 19:49:32 +00:00
|
|
|
ValidatedOptions,
|
2020-09-25 18:21:18 +00:00
|
|
|
} from "@patternfly/react-core";
|
|
|
|
import { useTranslation } from "react-i18next";
|
2020-11-12 12:55:52 +00:00
|
|
|
import { useAdminClient } from "../context/auth/AdminClient";
|
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-13 20:52:23 +00:00
|
|
|
refresh: () => void;
|
2020-10-01 18:52:18 +00:00
|
|
|
};
|
|
|
|
|
2020-10-01 19:15:23 +00:00
|
|
|
export const GroupsCreateModal = ({
|
|
|
|
handleModalToggle,
|
|
|
|
isCreateModalOpen,
|
|
|
|
setIsCreateModalOpen,
|
|
|
|
createGroupName,
|
|
|
|
setCreateGroupName,
|
2020-10-13 21:06:58 +00:00
|
|
|
refresh,
|
2020-10-01 19:15:23 +00:00
|
|
|
}: GroupsCreateModalProps) => {
|
2020-09-28 21:09:34 +00:00
|
|
|
const { t } = useTranslation("groups");
|
2020-11-12 12:55:52 +00:00
|
|
|
const adminClient = useAdminClient();
|
2020-10-13 20:52:23 +00:00
|
|
|
const { addAlert } = 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-10-06 14:36:09 +00:00
|
|
|
try {
|
2020-11-12 12:55:52 +00:00
|
|
|
await adminClient.groups.create({ name: createGroupName });
|
2020-10-27 23:45:35 +00:00
|
|
|
refresh();
|
2020-10-06 14:36:09 +00:00
|
|
|
setIsCreateModalOpen(false);
|
|
|
|
setCreateGroupName("");
|
2020-10-13 20:52:23 +00:00
|
|
|
addAlert(t("groupCreated"), AlertVariant.success);
|
2020-10-06 14:36:09 +00:00
|
|
|
} catch (error) {
|
2020-10-13 21:06:58 +00:00
|
|
|
addAlert(
|
|
|
|
`${t("couldNotCreateGroup")} ': '${error}'`,
|
|
|
|
AlertVariant.danger
|
|
|
|
);
|
2020-10-06 14:36:09 +00:00
|
|
|
}
|
2020-09-25 18:21:18 +00:00
|
|
|
}
|
2020-10-06 14:36:09 +00:00
|
|
|
};
|
2020-09-25 18:21:18 +00:00
|
|
|
|
|
|
|
return (
|
2020-10-13 20:52:23 +00:00
|
|
|
<>
|
2020-09-30 13:55:18 +00:00
|
|
|
<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
|
2020-10-14 20:47:29 +00:00
|
|
|
name="create-modal-group"
|
2020-10-01 18:52:18 +00:00
|
|
|
label={t("name")}
|
|
|
|
fieldId="group-id"
|
|
|
|
helperTextInvalid={t("common:required")}
|
2020-11-09 19:49:32 +00:00
|
|
|
validated={
|
|
|
|
errors.name ? ValidatedOptions.error : ValidatedOptions.default
|
|
|
|
}
|
2020-10-01 18:52:18 +00:00
|
|
|
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}
|
2020-11-09 19:49:32 +00:00
|
|
|
validated={
|
|
|
|
errors.name ? ValidatedOptions.error : ValidatedOptions.default
|
|
|
|
}
|
2020-09-30 13:55:18 +00:00
|
|
|
/>
|
|
|
|
</FormGroup>
|
|
|
|
</Form>
|
|
|
|
</Modal>
|
2020-10-13 20:52:23 +00:00
|
|
|
</>
|
2020-09-25 18:21:18 +00:00
|
|
|
);
|
|
|
|
};
|