2021-11-22 11:49:09 +05:30
|
|
|
import { Form, notification } from 'antd';
|
|
|
|
|
import editSlackApi from 'api/channels/editSlack';
|
|
|
|
|
import ROUTES from 'constants/routes';
|
|
|
|
|
import {
|
|
|
|
|
ChannelType,
|
|
|
|
|
SlackChannel,
|
|
|
|
|
} from 'container/CreateAlertChannels/config';
|
|
|
|
|
import FormAlertChannels from 'container/FormAlertChannels';
|
|
|
|
|
import history from 'lib/history';
|
|
|
|
|
import React, { useCallback, useState } from 'react';
|
2022-03-24 12:06:57 +05:30
|
|
|
import { useParams } from 'react-router-dom';
|
2021-11-22 11:49:09 +05:30
|
|
|
|
2022-03-22 12:10:31 +05:30
|
|
|
function EditAlertChannels({
|
2021-11-22 11:49:09 +05:30
|
|
|
initialValue,
|
2022-03-22 12:10:31 +05:30
|
|
|
}: EditAlertChannelsProps): JSX.Element {
|
2021-11-22 11:49:09 +05:30
|
|
|
const [formInstance] = Form.useForm();
|
|
|
|
|
const [selectedConfig, setSelectedConfig] = useState<Partial<SlackChannel>>({
|
|
|
|
|
...initialValue,
|
|
|
|
|
});
|
|
|
|
|
const [savingState, setSavingState] = useState<boolean>(false);
|
|
|
|
|
const [notifications, NotificationElement] = notification.useNotification();
|
|
|
|
|
const { id } = useParams<{ id: string }>();
|
|
|
|
|
|
|
|
|
|
const [type, setType] = useState<ChannelType>('slack');
|
|
|
|
|
|
|
|
|
|
const onTypeChangeHandler = useCallback((value: string) => {
|
|
|
|
|
setType(value as ChannelType);
|
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
|
|
const onSlackEditHandler = useCallback(async () => {
|
|
|
|
|
setSavingState(true);
|
|
|
|
|
const response = await editSlackApi({
|
|
|
|
|
api_url: selectedConfig?.api_url || '',
|
|
|
|
|
channel: selectedConfig?.channel || '',
|
|
|
|
|
name: selectedConfig?.name || '',
|
|
|
|
|
send_resolved: true,
|
|
|
|
|
text: selectedConfig?.text || '',
|
|
|
|
|
title: selectedConfig?.title || '',
|
|
|
|
|
id,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (response.statusCode === 200) {
|
|
|
|
|
notifications.success({
|
|
|
|
|
message: 'Success',
|
|
|
|
|
description: 'Channels Edited Successfully',
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
setTimeout(() => {
|
|
|
|
|
history.replace(ROUTES.SETTINGS);
|
|
|
|
|
}, 2000);
|
|
|
|
|
} else {
|
|
|
|
|
notifications.error({
|
|
|
|
|
message: 'Error',
|
|
|
|
|
description: response.error || 'error while updating the Channels',
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
setSavingState(false);
|
2022-03-16 22:35:13 +05:30
|
|
|
}, [selectedConfig, notifications, id]);
|
2021-11-22 11:49:09 +05:30
|
|
|
|
|
|
|
|
const onSaveHandler = useCallback(
|
|
|
|
|
(value: ChannelType) => {
|
|
|
|
|
if (value === 'slack') {
|
|
|
|
|
onSlackEditHandler();
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
[onSlackEditHandler],
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
const onTestHandler = useCallback(() => {
|
|
|
|
|
console.log('test');
|
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
|
|
return (
|
2022-03-22 12:10:31 +05:30
|
|
|
<FormAlertChannels
|
|
|
|
|
{...{
|
|
|
|
|
formInstance,
|
|
|
|
|
onTypeChangeHandler,
|
|
|
|
|
setSelectedConfig,
|
|
|
|
|
type,
|
|
|
|
|
onTestHandler,
|
|
|
|
|
onSaveHandler,
|
|
|
|
|
savingState,
|
|
|
|
|
NotificationElement,
|
|
|
|
|
title: 'Edit Notification Channels',
|
|
|
|
|
initialValue,
|
|
|
|
|
nameDisable: true,
|
|
|
|
|
}}
|
|
|
|
|
/>
|
2021-11-22 11:49:09 +05:30
|
|
|
);
|
2022-03-22 12:10:31 +05:30
|
|
|
}
|
2021-11-22 11:49:09 +05:30
|
|
|
|
2021-12-27 23:55:38 +05:30
|
|
|
interface EditAlertChannelsProps {
|
2022-03-24 12:06:57 +05:30
|
|
|
initialValue: {
|
|
|
|
|
[x: string]: unknown;
|
|
|
|
|
};
|
2021-11-22 11:49:09 +05:30
|
|
|
}
|
|
|
|
|
|
2021-12-27 23:55:38 +05:30
|
|
|
export default EditAlertChannels;
|