notification_edit_ctrl.ts 1.9 KB

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