112 lines
2.9 KiB
TypeScript
Raw Normal View History

Feat(UI): alerts (#363) * chore(webpack): file-loader is added for font * chore(UI): monaco-editor is added * feat(UI): Editor component is added * feat(UI): List All Alerts is updated * feat(UI): Create Alert is updated * feat(API): create alert api is added * feat(page): EditRules is added * feat(UI): Alerts WIP * chore(typescript): typing are updated * update(UI): useFetch hook is updated * chore(UI): component for alerts is updated * chore(UI): create alert is updated * feat(UI): delete alert is now added * feat(api): Delete api is added * chore(route): edit rule route is updated * update(UI): get getAll put Alert functionality is added * update(UI): Alert Channels is updated in setting tab * chore(UI): alerts api is updated * chore(UI): getGroup api is updated * chore(UI): chprev api is updated * chore(UI): getGroup interface is exportable * feat(UI):Alerts is added * temp * feat(UI): triggered alerts is added * chore(UI): deafault key for the alert is updated * chore(UI): alerts linting is fixed * chore(UI): alerts linting is fixed * chore(UI): sort order is implemented * feat(FE): channels WIP * feat(UI): slack ui is updated * Channels is updated * feat(UI): slack ui is updated * fix(ROUTES): Channels have a seperate route * fix(build): production build is fixed by adding the file loader * fix(UI): create slack config is updated * fix(BUG): delete alert rule is fixed * fix(bug): after successfull edit user is navigated to all rules * fix(bug): alert is updated * fix(bug): expandable row is updated * fix(bug): filter and grouping of the alerts is fixed * chore(alerts): default title and description of the channels is updated * fix(UI): filtering is fixed * fix(UI): baseUrl is redirected to the nginx and text is updated * fix(BUG): destoryed the inactive pane * chore(UI): placeholder for the triggered alerts is updated * chore(FE): placeholder is updated * chore(UI): placeholder is updated for the create alert
2021-11-22 11:49:09 +05:30
import { SaveOutlined } from '@ant-design/icons';
import { Button, notification } from 'antd';
import createAlertsApi from 'api/alerts/create';
import Editor from 'components/Editor';
import ROUTES from 'constants/routes';
import { State } from 'hooks/useFetch';
import history from 'lib/history';
import React, { useCallback, useRef, useState } from 'react';
import { PayloadProps as CreateAlertPayloadProps } from 'types/api/alerts/create';
import { ButtonContainer, Title } from './styles';
const CreateAlert = (): JSX.Element => {
const value = useRef<string>(
`\n alert: <alert name>\n expr: system_cpu_load_average_1m > 0.01\n for: 0m\n labels:\n severity: warning\n annotations:\n summary: High CPU load\n description: \"CPU load is > 0.01\n VALUE = {{ $value }}\n LABELS = {{ $labels }}\"\n `,
);
const [newAlertState, setNewAlertState] = useState<
State<CreateAlertPayloadProps>
>({
error: false,
errorMessage: '',
loading: false,
payload: undefined,
success: false,
});
const [notifications, Element] = notification.useNotification();
const onSaveHandler = useCallback(async () => {
try {
setNewAlertState((state) => ({
...state,
loading: true,
}));
if (value.current.length === 0) {
setNewAlertState((state) => ({
...state,
loading: false,
}));
notifications.error({
description: `Oops! We didn't catch that. Please make sure the alert settings are not empty or try again`,
message: 'Error',
});
return;
}
const response = await createAlertsApi({
query: value.current,
});
if (response.statusCode === 200) {
setNewAlertState((state) => ({
...state,
loading: false,
payload: response.payload,
}));
notifications.success({
message: 'Success',
description: 'Congrats. The alert was saved correctly.',
});
setTimeout(() => {
history.push(ROUTES.LIST_ALL_ALERT);
}, 3000);
} else {
notifications.error({
description:
response.error ||
'Oops! Some issue occured in saving the alert please try again or contact support@signoz.io',
message: 'Error',
});
setNewAlertState((state) => ({
...state,
loading: false,
error: true,
errorMessage:
response.error ||
'Oops! Some issue occured in saving the alert please try again or contact support@signoz.io',
}));
}
} catch (error) {
notifications.error({
message:
'Oops! Some issue occured in saving the alert please try again or contact support@signoz.io',
});
}
}, []);
return (
<>
{Element}
<Title>Create New Alert</Title>
<Editor value={value} />
<ButtonContainer>
<Button
loading={newAlertState.loading || false}
type="primary"
onClick={onSaveHandler}
icon={<SaveOutlined />}
>
Save
</Button>
</ButtonContainer>
</>
);
};
export default CreateAlert;