import { HttpErrorResponse } from '@angular/common/http'; import { Component, OnInit } from '@angular/core'; import { MatDialog } from '@angular/material/dialog'; import { ActivatedRoute, ParamMap, Router } from '@angular/router'; import { BaseComponent } from '@common/base/base.component'; import { FormService } from '@common/forms/form-service'; import { LoggingService } from '@app/core/services/logging/logging-service'; import { ConfirmationDialogComponent } from '@common/modules/confirmation-dialog/confirmation-dialog.component'; import { HttpError, HttpErrorHandlingService } from '@common/modules/errors/error-handling/http-error-handling.service'; import { Guid } from '@common/types/guid'; import { TranslateService } from '@ngx-translate/core'; import { IsActive } from '@app/core/common/enum/is-active.enum'; import { EnumUtils } from '@app/core/services/utilities/enum-utils.service'; import { map, takeUntil } from 'rxjs/operators'; import { nameof } from 'ts-simple-nameof'; import { AuthService } from '@app/core/services/auth/auth.service'; import { InAppNotificationService } from '@notification-service/services/http/inapp-notification.service'; import { SnackBarNotificationLevel, UiNotificationService } from '@app/core/services/notification/ui-notification-service'; import { NotificationInAppTracking } from '@notification-service/core/enum/notification-inapp-tracking.enum'; import { InAppNotification } from '@notification-service/core/model/inapp-notification.model'; @Component({ selector: 'app-inapp-notification-editor', templateUrl: './inapp-notification-editor.component.html', styleUrls: ['./inapp-notification-editor.component.scss'] }) export class InAppNotificationEditorComponent extends BaseComponent implements OnInit { isDeleted = false; isRead = false; isNew = false; isFromDialog = false; inappNotification: InAppNotification; constructor( public authService: AuthService, private dialog: MatDialog, private inappNotificationService: InAppNotificationService, private route: ActivatedRoute, private router: Router, private language: TranslateService, public enumUtils: EnumUtils, private formService: FormService, private uiNotificationService: UiNotificationService, private logger: LoggingService, private httpErrorHandlingService: HttpErrorHandlingService ) { super(); } ngOnInit(): void { this.route.paramMap.pipe(takeUntil(this._destroyed)).subscribe((paramMap: ParamMap) => { const itemId = paramMap.get('id'); this.isFromDialog = this.route.snapshot.data ? this.route.snapshot.data['isFromDialog'] as boolean : false; if (itemId != null) { this.inappNotificationService.getSingle(Guid.parse(itemId), [ nameof(x => x.id), nameof(x => x.subject), nameof(x => x.body), nameof(x => x.type), nameof(x => x.trackingState), nameof(x => x.isActive), nameof(x => x.hash), nameof(x => x.updatedAt) ]) .pipe(map(data => data as InAppNotification), takeUntil(this._destroyed)) .subscribe( data => { this.inappNotification = data; this.isDeleted = this.inappNotification.isActive === IsActive.Inactive; this.isRead = this.inappNotification.trackingState === NotificationInAppTracking.Delivered; if(!this.isDeleted && !this.isRead) this.markAsRead() }, error => this.onCallbackError(error) ); } else { this.inappNotification = null; } }); } public markAsRead() { const value = this.inappNotification; this.inappNotificationService.read(value.id).pipe(takeUntil(this._destroyed)) .subscribe( complete => this.onCallbackSuccess(), error => this.onCallbackError(error) ); // this.clearErrorModel(); } public delete() { const value = this.inappNotification; if (value.id) { const dialogRef = this.dialog.open(ConfirmationDialogComponent, { maxWidth: '300px', restoreFocus: false, data: { message: this.language.instant('GENERAL.CONFIRMATION-DIALOG.DELETE-ITEM'), confirmButton: this.language.instant('GENERAL.CONFIRMATION-DIALOG.ACTIONS.CONFIRM'), cancelButton: this.language.instant('GENERAL.CONFIRMATION-DIALOG.ACTIONS.CANCEL') } }); dialogRef.afterClosed().pipe(takeUntil(this._destroyed)).subscribe(result => { if (result) { this.inappNotificationService.delete(value.id).pipe(takeUntil(this._destroyed)) .subscribe( complete => this.cancel(), error => this.onCallbackError(error) ); } }); } } public cancel(): void { this.router.navigate(['/mine-notifications']); } onCallbackSuccess(data?: any): void { // this.uiNotificationService.snackBarNotification(this.language.instant('GENERAL.SNACK-BAR.SUCCESSFUL-UPDATE'), SnackBarNotificationLevel.Success); // this.router.navigate(['/mine-notifications']); } onCallbackError(errorResponse: HttpErrorResponse) { const error: HttpError = this.httpErrorHandlingService.getError(errorResponse); this.uiNotificationService.snackBarNotification(error.getMessagesString(), SnackBarNotificationLevel.Warning); } }