2024-01-08 17:32:07 +01:00
|
|
|
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 { InAppNotification } from '@app/core/model/inapp-notification/inapp-notification.model';
|
|
|
|
import { InAppNotificationService } from '@app/core/services/inapp-notification/inapp-notification.service';
|
|
|
|
import { SnackBarNotificationLevel, UiNotificationService } from '@app/core/services/notification/ui-notification-service';
|
|
|
|
import { NotificationInAppTracking } from '@app/core/common/enum/notification-inapp-tracking.enum';
|
|
|
|
|
|
|
|
@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<InAppNotification>(x => x.id),
|
|
|
|
nameof<InAppNotification>(x => x.subject),
|
|
|
|
nameof<InAppNotification>(x => x.body),
|
|
|
|
nameof<InAppNotification>(x => x.type),
|
|
|
|
nameof<InAppNotification>(x => x.trackingState),
|
|
|
|
nameof<InAppNotification>(x => x.isActive),
|
|
|
|
nameof<InAppNotification>(x => x.hash),
|
|
|
|
nameof<InAppNotification>(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;
|
2024-01-10 13:24:32 +01:00
|
|
|
|
|
|
|
if(!this.isDeleted && !this.isRead) this.markAsRead()
|
2024-01-08 17:32:07 +01:00
|
|
|
},
|
|
|
|
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('COMMONS.CONFIRMATION-DIALOG.DELETE-ITEM'),
|
|
|
|
confirmButton: this.language.instant('COMMONS.CONFIRMATION-DIALOG.ACTIONS.CONFIRM'),
|
|
|
|
cancelButton: this.language.instant('COMMONS.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.onCallbackSuccess(),
|
|
|
|
error => this.onCallbackError(error)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public cancel(): void {
|
2024-01-10 13:24:32 +01:00
|
|
|
this.router.navigate(['/mine-notifications']);
|
2024-01-08 17:32:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
onCallbackSuccess(data?: any): void {
|
2024-01-10 14:12:26 +01:00
|
|
|
// this.uiNotificationService.snackBarNotification(this.language.instant('COMMONS.SNACK-BAR.SUCCESSFUL-UPDATE'), SnackBarNotificationLevel.Success);
|
|
|
|
// this.router.navigate(['/mine-notifications']);
|
2024-01-08 17:32:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
onCallbackError(errorResponse: HttpErrorResponse) {
|
|
|
|
const error: HttpError = this.httpErrorHandlingService.getError(errorResponse);
|
|
|
|
this.uiNotificationService.snackBarNotification(error.getMessagesString(), SnackBarNotificationLevel.Warning);
|
|
|
|
}
|
|
|
|
}
|