notification_edit_ctrl.ts 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. ///<reference path="../../headers/common.d.ts" />
  2. import angular from 'angular';
  3. import _ from 'lodash';
  4. import coreModule from '../../core/core_module';
  5. import config from 'app/core/config';
  6. export class AlertNotificationEditCtrl {
  7. model: any;
  8. showTest: boolean = false;
  9. testSeverity: string = "critical";
  10. /** @ngInject */
  11. constructor(private $routeParams, private backendSrv, private $scope, private $location) {
  12. if ($routeParams.id) {
  13. this.loadNotification($routeParams.id);
  14. } else {
  15. this.model = {
  16. type: 'email',
  17. settings: {},
  18. isDefault: false
  19. };
  20. }
  21. }
  22. loadNotification(id) {
  23. this.backendSrv.get(`/api/alert-notifications/${id}`).then(result => {
  24. this.model = result;
  25. });
  26. }
  27. isNew() {
  28. return this.model.id === undefined;
  29. }
  30. save() {
  31. if (this.model.id) {
  32. this.backendSrv.put(`/api/alert-notifications/${this.model.id}`, this.model).then(res => {
  33. this.model = res;
  34. this.$scope.appEvent('alert-success', ['Notification updated', '']);
  35. });
  36. } else {
  37. this.backendSrv.post(`/api/alert-notifications`, this.model).then(res => {
  38. this.$scope.appEvent('alert-success', ['Notification created', '']);
  39. this.$location.path('alerting/notifications');
  40. });
  41. }
  42. }
  43. typeChanged() {
  44. this.model.settings = {};
  45. }
  46. toggleTest() {
  47. this.showTest = !this.showTest;
  48. }
  49. testNotification() {
  50. var payload = {
  51. name: this.model.name,
  52. type: this.model.type,
  53. settings: this.model.settings,
  54. severity: this.testSeverity
  55. };
  56. this.backendSrv.post(`/api/alert-notifications/test`, payload)
  57. .then(res => {
  58. this.$scope.appEvent('alert-succes', ['Test notification sent', '']);
  59. });
  60. }
  61. }
  62. coreModule.controller('AlertNotificationEditCtrl', AlertNotificationEditCtrl);