2023-01-12 12:38:03 +02:00
|
|
|
///
|
2023-02-14 11:39:29 +02:00
|
|
|
/// Copyright © 2016-2023 The Thingsboard Authors
|
2023-01-12 12:38:03 +02:00
|
|
|
///
|
|
|
|
|
/// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
|
/// you may not use this file except in compliance with the License.
|
|
|
|
|
/// You may obtain a copy of the License at
|
|
|
|
|
///
|
|
|
|
|
/// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
///
|
|
|
|
|
/// Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
/// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
|
/// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
/// See the License for the specific language governing permissions and
|
|
|
|
|
/// limitations under the License.
|
|
|
|
|
///
|
|
|
|
|
|
2023-01-12 18:11:37 +02:00
|
|
|
import {
|
|
|
|
|
NotificationDeliveryMethod,
|
2023-01-17 18:26:25 +02:00
|
|
|
NotificationDeliveryMethodTranslateMap,
|
2023-01-12 18:11:37 +02:00
|
|
|
NotificationTemplate,
|
2023-01-31 17:06:55 +02:00
|
|
|
NotificationType
|
2023-01-12 18:11:37 +02:00
|
|
|
} from '@shared/models/notification.models';
|
|
|
|
|
import { Component, Inject, OnDestroy, ViewChild } from '@angular/core';
|
2023-01-12 12:38:03 +02:00
|
|
|
import { Store } from '@ngrx/store';
|
|
|
|
|
import { AppState } from '@core/core.state';
|
|
|
|
|
import { Router } from '@angular/router';
|
|
|
|
|
import { MAT_DIALOG_DATA, MatDialogRef } from '@angular/material/dialog';
|
2023-02-13 18:00:23 +02:00
|
|
|
import { FormBuilder, FormGroup } from '@angular/forms';
|
2023-01-12 12:38:03 +02:00
|
|
|
import { NotificationService } from '@core/http/notification.service';
|
2023-02-10 18:31:05 +02:00
|
|
|
import { deepClone, deepTrim, isDefinedAndNotNull } from '@core/utils';
|
2023-02-13 18:00:23 +02:00
|
|
|
import { Observable } from 'rxjs';
|
|
|
|
|
import { map } from 'rxjs/operators';
|
2023-01-12 18:11:37 +02:00
|
|
|
import { StepperOrientation, StepperSelectionEvent } from '@angular/cdk/stepper';
|
|
|
|
|
import { MatStepper } from '@angular/material/stepper';
|
|
|
|
|
import { BreakpointObserver } from '@angular/cdk/layout';
|
|
|
|
|
import { MediaBreakpoints } from '@shared/models/constants';
|
2023-01-30 10:23:36 +02:00
|
|
|
import { TranslateService } from '@ngx-translate/core';
|
2023-02-13 18:00:23 +02:00
|
|
|
import { TemplateConfiguration } from '@home/pages/notification-center/template-table/template-configuration';
|
2023-01-12 12:38:03 +02:00
|
|
|
|
|
|
|
|
export interface TemplateNotificationDialogData {
|
|
|
|
|
template?: NotificationTemplate;
|
2023-02-10 18:31:05 +02:00
|
|
|
predefinedType?: NotificationType;
|
2023-01-12 12:38:03 +02:00
|
|
|
isAdd?: boolean;
|
|
|
|
|
isCopy?: boolean;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Component({
|
|
|
|
|
selector: 'tb-template-notification-dialog',
|
|
|
|
|
templateUrl: './template-notification-dialog.component.html',
|
|
|
|
|
styleUrls: ['./template-notification-dialog.component.scss']
|
|
|
|
|
})
|
|
|
|
|
export class TemplateNotificationDialogComponent
|
2023-02-13 18:00:23 +02:00
|
|
|
extends TemplateConfiguration<TemplateNotificationDialogComponent, NotificationTemplate> implements OnDestroy {
|
2023-01-12 12:38:03 +02:00
|
|
|
|
2023-01-30 10:23:36 +02:00
|
|
|
@ViewChild('notificationTemplateStepper', {static: true}) notificationTemplateStepper: MatStepper;
|
2023-01-12 18:11:37 +02:00
|
|
|
|
|
|
|
|
stepperOrientation: Observable<StepperOrientation>;
|
|
|
|
|
|
2023-01-12 12:38:03 +02:00
|
|
|
templateNotificationForm: FormGroup;
|
2023-01-12 18:11:37 +02:00
|
|
|
pushTemplateForm: FormGroup;
|
|
|
|
|
emailTemplateForm: FormGroup;
|
|
|
|
|
smsTemplateForm: FormGroup;
|
|
|
|
|
slackTemplateForm: FormGroup;
|
2023-01-12 12:38:03 +02:00
|
|
|
dialogTitle = 'notification.edit-notification-template';
|
|
|
|
|
|
2023-01-17 18:26:25 +02:00
|
|
|
notificationTypes = Object.keys(NotificationType) as NotificationType[];
|
2023-01-12 18:11:37 +02:00
|
|
|
notificationDeliveryMethods = Object.keys(NotificationDeliveryMethod) as NotificationDeliveryMethod[];
|
2023-01-17 18:26:25 +02:00
|
|
|
notificationDeliveryMethodTranslateMap = NotificationDeliveryMethodTranslateMap;
|
2023-01-12 18:11:37 +02:00
|
|
|
|
|
|
|
|
selectedIndex = 0;
|
2023-02-10 18:31:05 +02:00
|
|
|
hideSelectType = false;
|
2023-01-12 18:11:37 +02:00
|
|
|
|
2023-01-30 10:23:36 +02:00
|
|
|
private readonly templateNotification: NotificationTemplate;
|
2023-01-12 12:38:03 +02:00
|
|
|
|
|
|
|
|
constructor(protected store: Store<AppState>,
|
|
|
|
|
protected router: Router,
|
|
|
|
|
protected dialogRef: MatDialogRef<TemplateNotificationDialogComponent, NotificationTemplate>,
|
|
|
|
|
@Inject(MAT_DIALOG_DATA) public data: TemplateNotificationDialogData,
|
2023-01-12 18:11:37 +02:00
|
|
|
private breakpointObserver: BreakpointObserver,
|
2023-02-13 18:00:23 +02:00
|
|
|
protected fb: FormBuilder,
|
2023-01-30 10:23:36 +02:00
|
|
|
private notificationService: NotificationService,
|
|
|
|
|
private translate: TranslateService) {
|
2023-02-13 18:00:23 +02:00
|
|
|
super(store, router, dialogRef, fb);
|
2023-01-12 12:38:03 +02:00
|
|
|
|
2023-01-12 18:11:37 +02:00
|
|
|
this.stepperOrientation = this.breakpointObserver.observe(MediaBreakpoints['gt-xs'])
|
|
|
|
|
.pipe(map(({matches}) => matches ? 'horizontal' : 'vertical'));
|
|
|
|
|
|
2023-02-10 18:31:05 +02:00
|
|
|
if (isDefinedAndNotNull(this.data?.predefinedType)) {
|
|
|
|
|
this.hideSelectType = true;
|
2023-02-13 18:00:23 +02:00
|
|
|
this.templateNotificationForm.get('notificationType').setValue(this.data.predefinedType, {emitEvents: false});
|
2023-02-10 18:31:05 +02:00
|
|
|
}
|
|
|
|
|
|
2023-01-30 10:23:36 +02:00
|
|
|
if (data.isAdd || data.isCopy) {
|
|
|
|
|
this.dialogTitle = 'notification.add-notification-template';
|
|
|
|
|
}
|
|
|
|
|
this.templateNotification = deepClone(this.data.template);
|
|
|
|
|
|
|
|
|
|
if (this.templateNotification) {
|
|
|
|
|
if (this.data.isCopy) {
|
|
|
|
|
this.templateNotification.name += ` (${this.translate.instant('action.copy')})`;
|
|
|
|
|
}
|
|
|
|
|
this.templateNotificationForm.reset({}, {emitEvent: false});
|
|
|
|
|
this.templateNotificationForm.patchValue(this.templateNotification, {emitEvent: false});
|
|
|
|
|
for (const method in this.templateNotification.configuration.deliveryMethodsTemplates) {
|
|
|
|
|
this.deliveryMethodFormsMap.get(NotificationDeliveryMethod[method])
|
|
|
|
|
.patchValue(this.templateNotification.configuration.deliveryMethodsTemplates[method]);
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-01-12 12:38:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ngOnDestroy() {
|
|
|
|
|
super.ngOnDestroy();
|
|
|
|
|
this.destroy$.next();
|
|
|
|
|
this.destroy$.complete();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
cancel(): void {
|
|
|
|
|
this.dialogRef.close(null);
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-12 18:11:37 +02:00
|
|
|
changeStep($event: StepperSelectionEvent) {
|
|
|
|
|
this.selectedIndex = $event.selectedIndex;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
backStep() {
|
2023-01-30 10:23:36 +02:00
|
|
|
this.notificationTemplateStepper.previous();
|
2023-01-12 18:11:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
nextStep() {
|
|
|
|
|
if (this.selectedIndex >= this.maxStepperIndex) {
|
|
|
|
|
this.add();
|
|
|
|
|
} else {
|
2023-01-30 10:23:36 +02:00
|
|
|
this.notificationTemplateStepper.next();
|
2023-01-12 18:11:37 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
nextStepLabel(): string {
|
2023-01-30 10:23:36 +02:00
|
|
|
if (this.selectedIndex !== 0) {
|
|
|
|
|
if (this.selectedIndex >= this.maxStepperIndex) {
|
|
|
|
|
return (this.data.isAdd || this.data.isCopy) ? 'action.add' : 'action.save';
|
|
|
|
|
} else if (this.notificationTemplateStepper.selected.stepControl.pristine) {
|
|
|
|
|
return 'action.skip';
|
|
|
|
|
}
|
2023-01-12 18:11:37 +02:00
|
|
|
}
|
|
|
|
|
return 'action.next';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private get maxStepperIndex(): number {
|
2023-01-30 10:23:36 +02:00
|
|
|
return this.notificationTemplateStepper?._steps?.length - 1;
|
2023-01-12 18:11:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private add(): void {
|
|
|
|
|
if (this.allValid()) {
|
2023-02-13 18:00:23 +02:00
|
|
|
let template = this.getNotificationTemplateValue();
|
2023-02-02 15:25:04 +02:00
|
|
|
if (this.templateNotification && !this.data.isCopy) {
|
2023-01-30 10:23:36 +02:00
|
|
|
template = {...this.templateNotification, ...template};
|
2023-01-12 18:11:37 +02:00
|
|
|
}
|
2023-01-30 10:23:36 +02:00
|
|
|
this.notificationService.saveNotificationTemplate(deepTrim(template)).subscribe(
|
2023-01-12 18:11:37 +02:00
|
|
|
(target) => this.dialogRef.close(target)
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-17 18:26:25 +02:00
|
|
|
private allValid(): boolean {
|
2023-01-30 10:23:36 +02:00
|
|
|
return !this.notificationTemplateStepper.steps.find((item, index) => {
|
2023-01-12 18:11:37 +02:00
|
|
|
if (item.stepControl.invalid) {
|
|
|
|
|
item.interacted = true;
|
2023-01-30 10:23:36 +02:00
|
|
|
this.notificationTemplateStepper.selectedIndex = index;
|
2023-01-12 18:11:37 +02:00
|
|
|
return true;
|
|
|
|
|
} else {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
2023-01-12 12:38:03 +02:00
|
|
|
}
|