grafana/public/app/features/alerting/utils/notificationChannels.ts
Josh Hunt 3c6e0e8ef8
Chore: ESlint import order (#44959)
* Add and configure eslint-plugin-import

* Fix the lint:ts npm command

* Autofix + prettier all the files

* Manually fix remaining files

* Move jquery code in jest-setup to external file to safely reorder imports

* Resolve issue caused by circular dependencies within Prometheus

* Update .betterer.results

* Fix missing // @ts-ignore

* ignore iconBundle.ts

* Fix missing // @ts-ignore
2022-04-22 14:33:13 +01:00

73 lines
2.1 KiB
TypeScript

import memoizeOne from 'memoize-one';
import { SelectableValue } from '@grafana/data';
import { config } from '@grafana/runtime';
import { NotificationChannelDTO, NotificationChannelType } from 'app/types';
export const defaultValues: NotificationChannelDTO = {
id: -1,
name: '',
type: { value: 'email', label: 'Email' },
sendReminder: false,
disableResolveMessage: false,
frequency: '15m',
settings: {
uploadImage: config.rendererAvailable,
autoResolve: true,
httpMethod: 'POST',
severity: 'critical',
},
secureSettings: {},
secureFields: {},
isDefault: false,
};
export const mapChannelsToSelectableValue = memoizeOne(
(notificationChannels: NotificationChannelType[], includeDescription: boolean): Array<SelectableValue<string>> => {
return notificationChannels.map((channel) => {
if (includeDescription) {
return {
value: channel.value,
label: channel.label,
description: channel.description,
};
}
return {
value: channel.value,
label: channel.label,
};
});
}
);
export const transformSubmitData = (formData: NotificationChannelDTO) => {
/*
Some settings can be options in a select, in order to not save a SelectableValue<T>
we need to use check if it is a SelectableValue and use its value.
*/
const settings = Object.fromEntries(
Object.entries(formData.settings).map(([key, value]) => {
return [key, value && value.hasOwnProperty('value') ? value.value : value];
})
);
return {
...defaultValues,
...formData,
frequency: formData.frequency === '' ? defaultValues.frequency : formData.frequency,
type: formData.type.value,
settings: { ...defaultValues.settings, ...settings },
secureSettings: { ...formData.secureSettings },
};
};
export const transformTestData = (formData: NotificationChannelDTO) => {
return {
name: formData.name,
type: formData.type.value,
frequency: formData.frequency ?? defaultValues.frequency,
settings: { ...Object.assign(defaultValues.settings, formData.settings) },
secureSettings: { ...formData.secureSettings },
};
};