notification_edit_ctrl.ts 1.9 KB

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