89 lines
3.5 KiB
TypeScript
89 lines
3.5 KiB
TypeScript
import { Component, OnDestroy, OnInit, inject } from '@angular/core';
|
|
import { PublicationRestService } from '../../core/rest-services/publications/publication.rest-service';
|
|
import { ActivatedRoute, Router, RouterModule } from '@angular/router';
|
|
import { Subscription } from 'rxjs';
|
|
import { Publication } from '../../core/rest-services/publications/model/publication';
|
|
import { MatSnackBar } from '@angular/material/snack-bar';
|
|
import { CommonModule, Location } from '@angular/common';
|
|
import { MatProgressSpinner } from '@angular/material/progress-spinner';
|
|
import { MatTooltip, MatTooltipModule } from '@angular/material/tooltip';
|
|
import { MatIcon } from '@angular/material/icon';
|
|
import { AuthenticationService } from '../../core/service/authentication.service';
|
|
import { MatDialog } from '@angular/material/dialog';
|
|
import { ConfirmationDialog } from '../../components/confirmation-dialog/confirmation-dialog.component';
|
|
|
|
declare let Prism: any;
|
|
|
|
@Component({
|
|
selector: 'app-publication',
|
|
standalone: true,
|
|
imports: [CommonModule, MatProgressSpinner, MatTooltip, RouterModule, MatIcon, MatTooltipModule],
|
|
templateUrl: './publication.component.html',
|
|
styleUrl: './publication.component.scss'
|
|
})
|
|
export class PublicationComponent implements OnInit, OnDestroy {
|
|
private readonly activatedRoute = inject(ActivatedRoute);
|
|
private readonly authenticationService = inject(AuthenticationService);
|
|
private readonly dialog = inject(MatDialog);
|
|
private readonly publicationRestService = inject(PublicationRestService);
|
|
private readonly location = inject(Location);
|
|
private readonly snackBar = inject(MatSnackBar);
|
|
private paramMapSubscription?: Subscription;
|
|
private afterDialogCloseSubscription?: Subscription;
|
|
isLoading: boolean = false;
|
|
isAuthorAndUserEquals: boolean = false;
|
|
publication?: Publication;
|
|
|
|
ngOnInit(): void {
|
|
this.paramMapSubscription = this.activatedRoute
|
|
.paramMap
|
|
.subscribe(params => {
|
|
const publicationId = params.get('publicationId');
|
|
|
|
if (publicationId) {
|
|
this.isLoading = true;
|
|
|
|
this.publicationRestService.getById(publicationId)
|
|
.then(publication => {
|
|
this.publication = publication;
|
|
this.isAuthorAndUserEquals = this.authenticationService.getAuthenticatedUser()?.id === this.publication.author.id;
|
|
setTimeout(() => Prism.highlightAll(), 100);
|
|
})
|
|
.catch(error => {
|
|
this.snackBar.open('An error occurred while loading publication...', 'Close', { duration: 5000 });
|
|
console.error('An error occurred while loading publication...', error);
|
|
})
|
|
.finally(() => {
|
|
this.isLoading = false;
|
|
});
|
|
}
|
|
});
|
|
}
|
|
|
|
ngOnDestroy(): void {
|
|
this.paramMapSubscription?.unsubscribe();
|
|
this.afterDialogCloseSubscription?.unsubscribe();
|
|
}
|
|
|
|
deletePublication(): void {
|
|
const dialogRef = this.dialog.open(
|
|
ConfirmationDialog,
|
|
{
|
|
data: {
|
|
title: 'Publication deletion',
|
|
description: 'Are you sure you want to delete this publication?'
|
|
}
|
|
}
|
|
);
|
|
|
|
this.afterDialogCloseSubscription = dialogRef.afterClosed()
|
|
.subscribe(response => {
|
|
if (response && this.publication?.id) {
|
|
this.publicationRestService.delete(this.publication.id);
|
|
this.snackBar.open('Publication deleted', 'Close', { duration: 5000 });
|
|
this.location.back();
|
|
}
|
|
});
|
|
}
|
|
}
|