|
|
|
@ -31,13 +31,14 @@ import { Router } from '@angular/router'; |
|
|
|
import { MAT_DIALOG_DATA, MatDialogRef } from '@angular/material/dialog'; |
|
|
|
import { FormBuilder, FormGroup, ValidationErrors, Validators } from '@angular/forms'; |
|
|
|
import { NotificationService } from '@core/http/notification.service'; |
|
|
|
import { deepTrim, isDefined } from '@core/utils'; |
|
|
|
import { deepClone, deepTrim } from '@core/utils'; |
|
|
|
import { Observable, Subject } from 'rxjs'; |
|
|
|
import { map, takeUntil } from 'rxjs/operators'; |
|
|
|
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'; |
|
|
|
import { TranslateService } from '@ngx-translate/core'; |
|
|
|
|
|
|
|
export interface TemplateNotificationDialogData { |
|
|
|
template?: NotificationTemplate; |
|
|
|
@ -53,7 +54,7 @@ export interface TemplateNotificationDialogData { |
|
|
|
export class TemplateNotificationDialogComponent |
|
|
|
extends DialogComponent<TemplateNotificationDialogComponent, NotificationTemplate> implements OnDestroy { |
|
|
|
|
|
|
|
@ViewChild('addNotificationTemplate', {static: true}) addNotificationTemplate: MatStepper; |
|
|
|
@ViewChild('notificationTemplateStepper', {static: true}) notificationTemplateStepper: MatStepper; |
|
|
|
|
|
|
|
stepperOrientation: Observable<StepperOrientation>; |
|
|
|
|
|
|
|
@ -63,7 +64,6 @@ export class TemplateNotificationDialogComponent |
|
|
|
smsTemplateForm: FormGroup; |
|
|
|
slackTemplateForm: FormGroup; |
|
|
|
dialogTitle = 'notification.edit-notification-template'; |
|
|
|
saveButtonLabel = 'action.save'; |
|
|
|
|
|
|
|
notificationTypes = Object.keys(NotificationType) as NotificationType[]; |
|
|
|
slackChanelTypes = Object.keys(SlackChanelType) as SlackChanelType[]; |
|
|
|
@ -74,40 +74,33 @@ export class TemplateNotificationDialogComponent |
|
|
|
|
|
|
|
selectedIndex = 0; |
|
|
|
|
|
|
|
tinyMceOptions: Record<string, any>; |
|
|
|
tinyMceOptions: Record<string, any> = { |
|
|
|
base_url: '/assets/tinymce', |
|
|
|
suffix: '.min', |
|
|
|
plugins: ['link table image imagetools code fullscreen'], |
|
|
|
menubar: 'edit insert tools view format table', |
|
|
|
toolbar: 'fontselect fontsizeselect | formatselect | bold italic strikethrough forecolor backcolor ' + |
|
|
|
'| link | table | image | alignleft aligncenter alignright alignjustify ' + |
|
|
|
'| numlist bullist outdent indent | removeformat | code | fullscreen', |
|
|
|
height: 400, |
|
|
|
autofocus: false, |
|
|
|
branding: false |
|
|
|
}; |
|
|
|
|
|
|
|
private readonly destroy$ = new Subject<void>(); |
|
|
|
private readonly templateNotification: NotificationTemplate; |
|
|
|
|
|
|
|
private deliveryMethodFormsMap: Map<NotificationDeliveryMethod, FormGroup>; |
|
|
|
constructor(protected store: Store<AppState>, |
|
|
|
protected router: Router, |
|
|
|
protected dialogRef: MatDialogRef<TemplateNotificationDialogComponent, NotificationTemplate>, |
|
|
|
@Inject(MAT_DIALOG_DATA) public data: TemplateNotificationDialogData, |
|
|
|
private breakpointObserver: BreakpointObserver, |
|
|
|
private fb: FormBuilder, |
|
|
|
private notificationService: NotificationService) { |
|
|
|
private notificationService: NotificationService, |
|
|
|
private translate: TranslateService) { |
|
|
|
super(store, router, dialogRef); |
|
|
|
|
|
|
|
if (data.isAdd) { |
|
|
|
this.dialogTitle = 'notification.add-notification-template'; |
|
|
|
this.saveButtonLabel = 'action.add'; |
|
|
|
} |
|
|
|
if (data.isCopy) { |
|
|
|
this.dialogTitle = 'notification.copy-notification-template'; |
|
|
|
} |
|
|
|
|
|
|
|
this.tinyMceOptions = { |
|
|
|
base_url: '/assets/tinymce', |
|
|
|
suffix: '.min', |
|
|
|
plugins: ['link table image imagetools code fullscreen'], |
|
|
|
menubar: 'edit insert tools view format table', |
|
|
|
toolbar: 'fontselect fontsizeselect | formatselect | bold italic strikethrough forecolor backcolor ' + |
|
|
|
'| link | table | image | alignleft aligncenter alignright alignjustify ' + |
|
|
|
'| numlist bullist outdent indent | removeformat | code | fullscreen', |
|
|
|
height: 400, |
|
|
|
autofocus: false, |
|
|
|
branding: false |
|
|
|
}; |
|
|
|
|
|
|
|
this.stepperOrientation = this.breakpointObserver.observe(MediaBreakpoints['gt-xs']) |
|
|
|
.pipe(map(({matches}) => matches ? 'horizontal' : 'vertical')); |
|
|
|
|
|
|
|
@ -122,7 +115,7 @@ export class TemplateNotificationDialogComponent |
|
|
|
}); |
|
|
|
this.notificationDeliveryMethods.forEach(method => { |
|
|
|
(this.templateNotificationForm.get('configuration.deliveryMethodsTemplates') as FormGroup) |
|
|
|
.addControl(method, this.fb.group({method, enabled: method === NotificationDeliveryMethod.PUSH}), {emitEvent: false}); |
|
|
|
.addControl(method, this.fb.group({enabled: method === NotificationDeliveryMethod.PUSH}), {emitEvent: false}); |
|
|
|
}); |
|
|
|
|
|
|
|
this.pushTemplateForm = this.fb.group({ |
|
|
|
@ -179,6 +172,30 @@ export class TemplateNotificationDialogComponent |
|
|
|
conversationId: ['', Validators.required], |
|
|
|
conversationType: [SlackChanelType.DIRECT] |
|
|
|
}); |
|
|
|
|
|
|
|
this.deliveryMethodFormsMap = new Map<NotificationDeliveryMethod, FormGroup>([ |
|
|
|
[NotificationDeliveryMethod.PUSH, this.pushTemplateForm], |
|
|
|
[NotificationDeliveryMethod.EMAIL, this.emailTemplateForm], |
|
|
|
[NotificationDeliveryMethod.SMS, this.smsTemplateForm], |
|
|
|
[NotificationDeliveryMethod.SLACK, this.slackTemplateForm] |
|
|
|
]); |
|
|
|
|
|
|
|
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]); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
ngOnDestroy() { |
|
|
|
@ -191,86 +208,61 @@ export class TemplateNotificationDialogComponent |
|
|
|
this.dialogRef.close(null); |
|
|
|
} |
|
|
|
|
|
|
|
save() { |
|
|
|
let formValue = deepTrim(this.templateNotificationForm.value); |
|
|
|
if (isDefined(this.data.template)) { |
|
|
|
formValue = Object.assign({}, this.data.template, formValue); |
|
|
|
} |
|
|
|
this.notificationService.saveNotificationTemplate(formValue).subscribe( |
|
|
|
(target) => this.dialogRef.close(target) |
|
|
|
); |
|
|
|
} |
|
|
|
|
|
|
|
changeStep($event: StepperSelectionEvent) { |
|
|
|
this.selectedIndex = $event.selectedIndex; |
|
|
|
} |
|
|
|
|
|
|
|
backStep() { |
|
|
|
this.addNotificationTemplate.previous(); |
|
|
|
this.notificationTemplateStepper.previous(); |
|
|
|
} |
|
|
|
|
|
|
|
nextStep() { |
|
|
|
if (this.selectedIndex >= this.maxStepperIndex) { |
|
|
|
this.add(); |
|
|
|
} else { |
|
|
|
this.addNotificationTemplate.next(); |
|
|
|
this.notificationTemplateStepper.next(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
nextStepLabel(): string { |
|
|
|
if (this.selectedIndex === 1 && this.selectedIndex < this.maxStepperIndex && this.pushTemplateForm.pristine) { |
|
|
|
return 'action.skip'; |
|
|
|
} |
|
|
|
if (this.selectedIndex === 2 && this.selectedIndex < this.maxStepperIndex && this.emailTemplateForm.pristine) { |
|
|
|
return 'action.skip'; |
|
|
|
} |
|
|
|
if (this.selectedIndex === 3 && this.selectedIndex < this.maxStepperIndex && this.smsTemplateForm.pristine) { |
|
|
|
return 'action.skip'; |
|
|
|
} |
|
|
|
if (this.selectedIndex !== 0 && this.selectedIndex >= this.maxStepperIndex) { |
|
|
|
return 'action.add'; |
|
|
|
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'; |
|
|
|
} |
|
|
|
} |
|
|
|
return 'action.next'; |
|
|
|
} |
|
|
|
|
|
|
|
private get maxStepperIndex(): number { |
|
|
|
return this.addNotificationTemplate?._steps?.length - 1; |
|
|
|
return this.notificationTemplateStepper?._steps?.length - 1; |
|
|
|
} |
|
|
|
|
|
|
|
private add(): void { |
|
|
|
if (this.allValid()) { |
|
|
|
const formValue: NotificationTemplate = this.templateNotificationForm.value; |
|
|
|
if (formValue.configuration.deliveryMethodsTemplates.PUSH.enabled) { |
|
|
|
Object.assign(formValue.configuration.deliveryMethodsTemplates.PUSH, this.pushTemplateForm.value); |
|
|
|
} else { |
|
|
|
delete formValue.configuration.deliveryMethodsTemplates.PUSH; |
|
|
|
} |
|
|
|
if (formValue.configuration.deliveryMethodsTemplates.EMAIL.enabled) { |
|
|
|
Object.assign(formValue.configuration.deliveryMethodsTemplates.EMAIL, this.emailTemplateForm.value); |
|
|
|
} else { |
|
|
|
delete formValue.configuration.deliveryMethodsTemplates.EMAIL; |
|
|
|
} |
|
|
|
if (formValue.configuration.deliveryMethodsTemplates.SMS.enabled) { |
|
|
|
Object.assign(formValue.configuration.deliveryMethodsTemplates.SMS, this.smsTemplateForm.value); |
|
|
|
} else { |
|
|
|
delete formValue.configuration.deliveryMethodsTemplates.SMS; |
|
|
|
} |
|
|
|
if (formValue.configuration.deliveryMethodsTemplates.SLACK.enabled) { |
|
|
|
Object.assign(formValue.configuration.deliveryMethodsTemplates.SLACK, this.slackTemplateForm.value); |
|
|
|
} else { |
|
|
|
delete formValue.configuration.deliveryMethodsTemplates.SLACK; |
|
|
|
let template: NotificationTemplate = this.templateNotificationForm.value; |
|
|
|
this.notificationDeliveryMethods.forEach(method => { |
|
|
|
if (template.configuration.deliveryMethodsTemplates[method].enabled) { |
|
|
|
Object.assign(template.configuration.deliveryMethodsTemplates[method], this.deliveryMethodFormsMap.get(method).value, {method}); |
|
|
|
} else { |
|
|
|
delete template.configuration.deliveryMethodsTemplates[method]; |
|
|
|
} |
|
|
|
}); |
|
|
|
if (this.templateNotification) { |
|
|
|
template = {...this.templateNotification, ...template}; |
|
|
|
} |
|
|
|
this.notificationService.saveNotificationTemplate(deepTrim(formValue)).subscribe( |
|
|
|
this.notificationService.saveNotificationTemplate(deepTrim(template)).subscribe( |
|
|
|
(target) => this.dialogRef.close(target) |
|
|
|
); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
private allValid(): boolean { |
|
|
|
return !this.addNotificationTemplate.steps.find((item, index) => { |
|
|
|
return !this.notificationTemplateStepper.steps.find((item, index) => { |
|
|
|
if (item.stepControl.invalid) { |
|
|
|
item.interacted = true; |
|
|
|
this.addNotificationTemplate.selectedIndex = index; |
|
|
|
this.notificationTemplateStepper.selectedIndex = index; |
|
|
|
return true; |
|
|
|
} else { |
|
|
|
return false; |
|
|
|
|