90 lines
3.7 KiB
TypeScript
90 lines
3.7 KiB
TypeScript
import { Component, OnDestroy, OnInit, computed, inject, signal } 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 = signal(false);
|
|
isAuthorAndUserEquals = computed(() => this.authenticationService.getAuthenticatedUser()?.id === this.publication()?.author.id);
|
|
publication = signal<Publication | undefined>(undefined);
|
|
isPublicationUndefined = computed(() => !this.publication())
|
|
|
|
ngOnInit(): void {
|
|
this.paramMapSubscription = this.activatedRoute
|
|
.paramMap
|
|
.subscribe(params => {
|
|
const publicationId = params.get('publicationId');
|
|
|
|
if (publicationId) {
|
|
this.isLoading.set(true);
|
|
|
|
this.publicationRestService.getById(publicationId)
|
|
.then(publication => {
|
|
this.publication.set(publication);
|
|
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.set(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.isPublicationUndefined()) {
|
|
this.publicationRestService.delete(this.publication()?.id!!);
|
|
this.snackBar.open($localize`Publication deleted`, $localize`Close`, { duration: 5000 });
|
|
this.location.back();
|
|
}
|
|
});
|
|
}
|
|
}
|