2021-10-07 16:14:35 -07:00
|
|
|
import React, { useState } from 'react';
|
2022-02-01 10:47:45 -08:00
|
|
|
import { message, Button, Input, Modal, Typography, Form, Collapse } from 'antd';
|
2021-10-07 16:14:35 -07:00
|
|
|
import { useCreateGroupMutation } from '../../../graphql/group.generated';
|
2022-02-17 04:30:54 +05:30
|
|
|
import { useEnterKeyListener } from '../../shared/useEnterKeyListener';
|
2022-03-24 02:05:45 +05:30
|
|
|
import { groupIdTextValidation } from '../../shared/textUtil';
|
2022-10-13 11:41:47 -07:00
|
|
|
import analytics, { EventType } from '../../analytics';
|
2021-10-07 16:14:35 -07:00
|
|
|
|
|
|
|
type Props = {
|
|
|
|
onClose: () => void;
|
|
|
|
onCreate: (name: string, description: string) => void;
|
|
|
|
};
|
|
|
|
|
2022-06-29 22:41:41 -04:00
|
|
|
export default function CreateGroupModal({ onClose, onCreate }: Props) {
|
2021-10-07 16:14:35 -07:00
|
|
|
const [stagedName, setStagedName] = useState('');
|
|
|
|
const [stagedDescription, setStagedDescription] = useState('');
|
2022-02-01 10:47:45 -08:00
|
|
|
const [stagedId, setStagedId] = useState<string | undefined>(undefined);
|
2021-10-07 16:14:35 -07:00
|
|
|
const [createGroupMutation] = useCreateGroupMutation();
|
2022-03-24 02:05:45 +05:30
|
|
|
const [createButtonEnabled, setCreateButtonEnabled] = useState(true);
|
|
|
|
const [form] = Form.useForm();
|
2021-10-07 16:14:35 -07:00
|
|
|
|
|
|
|
const onCreateGroup = () => {
|
|
|
|
createGroupMutation({
|
|
|
|
variables: {
|
|
|
|
input: {
|
2022-02-01 10:47:45 -08:00
|
|
|
id: stagedId,
|
2021-10-07 16:14:35 -07:00
|
|
|
name: stagedName,
|
|
|
|
description: stagedDescription,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
2022-10-13 11:41:47 -07:00
|
|
|
.then(({ errors }) => {
|
|
|
|
if (!errors) {
|
|
|
|
analytics.event({
|
|
|
|
type: EventType.CreateGroupEvent,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
})
|
2021-10-07 16:14:35 -07:00
|
|
|
.catch((e) => {
|
|
|
|
message.destroy();
|
|
|
|
message.error({ content: `Failed to create group!: \n ${e.message || ''}`, duration: 3 });
|
|
|
|
})
|
|
|
|
.finally(() => {
|
|
|
|
message.success({
|
|
|
|
content: `Created group!`,
|
|
|
|
duration: 3,
|
|
|
|
});
|
|
|
|
onCreate(stagedName, stagedDescription);
|
|
|
|
setStagedName('');
|
|
|
|
setStagedDescription('');
|
|
|
|
});
|
|
|
|
onClose();
|
|
|
|
};
|
|
|
|
|
2022-02-17 04:30:54 +05:30
|
|
|
// Handle the Enter press
|
|
|
|
useEnterKeyListener({
|
|
|
|
querySelectorToExecuteClick: '#createGroupButton',
|
|
|
|
});
|
|
|
|
|
2021-10-07 16:14:35 -07:00
|
|
|
return (
|
|
|
|
<Modal
|
|
|
|
title="Create new group"
|
2022-06-29 22:41:41 -04:00
|
|
|
visible
|
2021-10-07 16:14:35 -07:00
|
|
|
onCancel={onClose}
|
|
|
|
footer={
|
|
|
|
<>
|
|
|
|
<Button onClick={onClose} type="text">
|
|
|
|
Cancel
|
|
|
|
</Button>
|
2022-03-24 02:05:45 +05:30
|
|
|
<Button id="createGroupButton" onClick={onCreateGroup} disabled={createButtonEnabled}>
|
2021-10-07 16:14:35 -07:00
|
|
|
Create
|
|
|
|
</Button>
|
|
|
|
</>
|
|
|
|
}
|
|
|
|
>
|
2022-03-24 02:05:45 +05:30
|
|
|
<Form
|
|
|
|
form={form}
|
|
|
|
initialValues={{}}
|
|
|
|
layout="vertical"
|
|
|
|
onFieldsChange={() =>
|
|
|
|
setCreateButtonEnabled(form.getFieldsError().some((field) => field.errors.length > 0))
|
|
|
|
}
|
|
|
|
>
|
|
|
|
<Form.Item label={<Typography.Text strong>Name</Typography.Text>}>
|
2021-10-07 16:14:35 -07:00
|
|
|
<Typography.Paragraph>Give your new group a name.</Typography.Paragraph>
|
2022-03-24 02:05:45 +05:30
|
|
|
<Form.Item
|
|
|
|
name="name"
|
|
|
|
rules={[
|
|
|
|
{
|
|
|
|
required: true,
|
|
|
|
message: 'Enter a Domain name.',
|
|
|
|
},
|
|
|
|
{ whitespace: true },
|
|
|
|
{ min: 1, max: 50 },
|
|
|
|
]}
|
|
|
|
hasFeedback
|
|
|
|
>
|
|
|
|
<Input
|
|
|
|
placeholder="A name for your group"
|
|
|
|
value={stagedName}
|
|
|
|
onChange={(event) => setStagedName(event.target.value)}
|
|
|
|
/>
|
|
|
|
</Form.Item>
|
2021-10-07 16:14:35 -07:00
|
|
|
</Form.Item>
|
2022-03-24 02:05:45 +05:30
|
|
|
<Form.Item label={<Typography.Text strong>Description</Typography.Text>}>
|
2021-10-07 16:14:35 -07:00
|
|
|
<Typography.Paragraph>An optional description for your new group.</Typography.Paragraph>
|
2022-03-24 02:05:45 +05:30
|
|
|
<Form.Item name="description" rules={[{ whitespace: true }, { min: 1, max: 500 }]} hasFeedback>
|
|
|
|
<Input
|
|
|
|
placeholder="A description for your group"
|
|
|
|
value={stagedDescription}
|
|
|
|
onChange={(event) => setStagedDescription(event.target.value)}
|
|
|
|
/>
|
|
|
|
</Form.Item>
|
2021-10-07 16:14:35 -07:00
|
|
|
</Form.Item>
|
2022-02-01 10:47:45 -08:00
|
|
|
<Collapse ghost>
|
|
|
|
<Collapse.Panel header={<Typography.Text type="secondary">Advanced</Typography.Text>} key="1">
|
|
|
|
<Form.Item label={<Typography.Text strong>Group Id</Typography.Text>}>
|
|
|
|
<Typography.Paragraph>
|
|
|
|
By default, a random UUID will be generated to uniquely identify this group. If
|
|
|
|
you'd like to provide a custom id instead to more easily keep track of this group,
|
|
|
|
you may provide it here. Be careful, you cannot easily change the group id after
|
|
|
|
creation.
|
|
|
|
</Typography.Paragraph>
|
2022-03-24 02:05:45 +05:30
|
|
|
<Form.Item
|
|
|
|
name="groupId"
|
|
|
|
rules={[
|
|
|
|
() => ({
|
|
|
|
validator(_, value) {
|
|
|
|
if (value && groupIdTextValidation(value)) {
|
|
|
|
return Promise.resolve();
|
|
|
|
}
|
|
|
|
return Promise.reject(new Error('Please enter correct Group name'));
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
]}
|
|
|
|
>
|
|
|
|
<Input
|
|
|
|
placeholder="product_engineering"
|
|
|
|
value={stagedId || ''}
|
|
|
|
onChange={(event) => setStagedId(event.target.value)}
|
|
|
|
/>
|
|
|
|
</Form.Item>
|
2022-02-01 10:47:45 -08:00
|
|
|
</Form.Item>
|
|
|
|
</Collapse.Panel>
|
|
|
|
</Collapse>
|
2021-10-07 16:14:35 -07:00
|
|
|
</Form>
|
|
|
|
</Modal>
|
|
|
|
);
|
|
|
|
}
|