notification_edit_ctrl.ts 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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. };
  19. }
  20. }
  21. loadNotification(id) {
  22. this.backendSrv.get(`/api/alert-notifications/${id}`).then(result => {
  23. this.model = result;
  24. });
  25. }
  26. isNew() {
  27. return this.model.id === undefined;
  28. }
  29. save() {
  30. if (this.model.id) {
  31. this.backendSrv.put(`/api/alert-notifications/${this.model.id}`, this.model).then(res => {
  32. this.model = res;
  33. this.$scope.appEvent('alert-success', ['Notification updated', '']);
  34. });
  35. } else {
  36. this.backendSrv.post(`/api/alert-notifications`, this.model).then(res => {
  37. this.$scope.appEvent('alert-success', ['Notification created', '']);
  38. this.$location.path('alerting/notifications');
  39. });
  40. }
  41. }
  42. typeChanged() {
  43. this.model.settings = {};
  44. }
  45. toggleTest() {
  46. this.showTest = !this.showTest;
  47. }
  48. testNotification() {
  49. var payload = {
  50. name: this.model.name,
  51. type: this.model.type,
  52. settings: this.model.settings,
  53. severity: this.testSeverity
  54. };
  55. this.backendSrv.post(`/api/alert-notifications/test`, payload)
  56. .then(res => {
  57. this.$scope.appEvent('alert-succes', ['Test notification sent', '']);
  58. });
  59. }
  60. }
  61. coreModule.controller('AlertNotificationEditCtrl', AlertNotificationEditCtrl);