2021-11-22 11:49:09 +05:30
|
|
|
import { Typography } from 'antd';
|
|
|
|
|
import get from 'api/channels/get';
|
|
|
|
|
import Spinner from 'components/Spinner';
|
2022-03-28 21:01:57 +05:30
|
|
|
import {
|
|
|
|
|
SlackChannel,
|
|
|
|
|
SlackType,
|
|
|
|
|
WebhookChannel,
|
|
|
|
|
WebhookType,
|
|
|
|
|
} from 'container/CreateAlertChannels/config';
|
2021-11-22 11:49:09 +05:30
|
|
|
import EditAlertChannels from 'container/EditAlertChannels';
|
|
|
|
|
import useFetch from 'hooks/useFetch';
|
|
|
|
|
import React from 'react';
|
2022-03-24 12:06:57 +05:30
|
|
|
import { useParams } from 'react-router-dom';
|
2021-11-22 11:49:09 +05:30
|
|
|
import { PayloadProps, Props } from 'types/api/channels/get';
|
|
|
|
|
|
2022-03-22 12:10:31 +05:30
|
|
|
function ChannelsEdit(): JSX.Element {
|
2021-11-22 11:49:09 +05:30
|
|
|
const { id } = useParams<Params>();
|
|
|
|
|
|
|
|
|
|
const { errorMessage, payload, error, loading } = useFetch<
|
|
|
|
|
PayloadProps,
|
|
|
|
|
Props
|
|
|
|
|
>(get, {
|
|
|
|
|
id,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (error) {
|
|
|
|
|
return <Typography>{errorMessage}</Typography>;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (loading || payload === undefined) {
|
|
|
|
|
return <Spinner tip="Loading Channels..." />;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const { data } = payload;
|
|
|
|
|
|
|
|
|
|
const value = JSON.parse(data);
|
2022-03-28 21:01:57 +05:30
|
|
|
let type = '';
|
|
|
|
|
let channel: SlackChannel & WebhookChannel = { name: '' };
|
2021-11-22 11:49:09 +05:30
|
|
|
|
2022-03-28 21:01:57 +05:30
|
|
|
if (value && 'slack_configs' in value) {
|
|
|
|
|
const slackConfig = value.slack_configs[0];
|
|
|
|
|
channel = slackConfig;
|
|
|
|
|
type = SlackType;
|
|
|
|
|
} else if (value && 'webhook_configs' in value) {
|
|
|
|
|
const webhookConfig = value.webhook_configs[0];
|
|
|
|
|
channel = webhookConfig;
|
|
|
|
|
channel.api_url = webhookConfig.url;
|
2021-11-22 11:49:09 +05:30
|
|
|
|
2022-03-28 21:01:57 +05:30
|
|
|
if ('http_config' in webhookConfig) {
|
|
|
|
|
const httpConfig = webhookConfig.http_config;
|
|
|
|
|
if ('basic_auth' in httpConfig) {
|
|
|
|
|
channel.username = webhookConfig.http_config?.basic_auth?.username;
|
|
|
|
|
channel.password = webhookConfig.http_config?.basic_auth?.password;
|
|
|
|
|
} else if ('authorization' in httpConfig) {
|
|
|
|
|
channel.password = webhookConfig.http_config?.authorization?.credentials;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
type = WebhookType;
|
|
|
|
|
}
|
|
|
|
|
console.log('channel:', channel);
|
2021-11-22 11:49:09 +05:30
|
|
|
return (
|
|
|
|
|
<EditAlertChannels
|
|
|
|
|
{...{
|
|
|
|
|
initialValue: {
|
|
|
|
|
...channel,
|
2022-03-28 21:01:57 +05:30
|
|
|
type,
|
2021-11-22 11:49:09 +05:30
|
|
|
name: value.name,
|
|
|
|
|
},
|
|
|
|
|
}}
|
|
|
|
|
/>
|
|
|
|
|
);
|
2022-03-22 12:10:31 +05:30
|
|
|
}
|
2021-11-22 11:49:09 +05:30
|
|
|
interface Params {
|
|
|
|
|
id: string;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export default ChannelsEdit;
|