98 lines
3.6 KiB
TypeScript
98 lines
3.6 KiB
TypeScript
import { CommonModule, Location } from '@angular/common';
|
|
import { Component, OnDestroy, OnInit, inject } from '@angular/core';
|
|
import { MatRippleModule } from '@angular/material/core';
|
|
import { MatDialog } from '@angular/material/dialog';
|
|
import { MatIcon } from '@angular/material/icon';
|
|
import { MatProgressSpinner } from '@angular/material/progress-spinner';
|
|
import { MatSnackBar } from '@angular/material/snack-bar';
|
|
import { MatTooltipModule } from '@angular/material/tooltip';
|
|
import { ActivatedRoute, RouterModule } from '@angular/router';
|
|
import { Subscription } from 'rxjs';
|
|
import { ConfirmationDialog } from '../../components/confirmation-dialog/confirmation-dialog.component';
|
|
import { Publication } from '../../core/rest-services/publications/model/publication';
|
|
import { PublicationRestService } from '../../core/rest-services/publications/publication.rest-service';
|
|
import { AuthenticationService } from '../../core/service/authentication.service';
|
|
|
|
declare let Prism: any;
|
|
|
|
@Component({
|
|
selector: 'app-publication',
|
|
standalone: true,
|
|
templateUrl: './publication.component.html',
|
|
styleUrl: './publication.component.scss',
|
|
imports: [
|
|
CommonModule,
|
|
MatIcon,
|
|
MatRippleModule,
|
|
MatProgressSpinner,
|
|
MatTooltipModule,
|
|
RouterModule
|
|
]
|
|
})
|
|
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 => {
|
|
const errorMessage = $localize`An error occurred while loading publication...`;
|
|
this.snackBar.open(errorMessage, $localize`Close`, { duration: 5000 });
|
|
console.error(errorMessage, error);
|
|
})
|
|
.finally(() => {
|
|
this.isLoading = false;
|
|
});
|
|
}
|
|
});
|
|
}
|
|
|
|
ngOnDestroy(): void {
|
|
this.paramMapSubscription?.unsubscribe();
|
|
this.afterDialogCloseSubscription?.unsubscribe();
|
|
}
|
|
|
|
deletePublication(): void {
|
|
const dialogRef = this.dialog.open(
|
|
ConfirmationDialog,
|
|
{
|
|
data: {
|
|
title: $localize`Publication deletion`,
|
|
description: $localize`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($localize`Publication deleted`, $localize`Close`, { duration: 5000 });
|
|
this.location.back();
|
|
}
|
|
});
|
|
}
|
|
}
|