74 lines
3.0 KiB
TypeScript
74 lines
3.0 KiB
TypeScript
import { Component, inject, OnInit } from "@angular/core";
|
|
import { PublicationEditionComponent } from "../../components/publication-edition/publication-edition.component";
|
|
import { PublicationRestService } from "../../core/rest-services/publications/publication.rest-service";
|
|
import { ActivatedRoute, Router } from "@angular/router";
|
|
import { MatSnackBar } from "@angular/material/snack-bar";
|
|
import { BehaviorSubject, Observable, Subscription } from "rxjs";
|
|
import { Publication } from "../../core/rest-services/publications/model/publication";
|
|
import { AuthenticationService } from "../../core/service/authentication.service";
|
|
import { Author } from "../../core/rest-services/publications/model/author";
|
|
|
|
|
|
@Component({
|
|
selector: 'app-publication-creation',
|
|
templateUrl: './publication-creation.component.html',
|
|
styleUrl: './publication-creation.component.scss',
|
|
imports: [
|
|
PublicationEditionComponent
|
|
]
|
|
})
|
|
export class PublicationCreationComponent implements OnInit {
|
|
private readonly authenticationService = inject(AuthenticationService);
|
|
private readonly publicationRestService = inject(PublicationRestService);
|
|
private readonly router = inject(Router);
|
|
private readonly snackBar = inject(MatSnackBar);
|
|
private isSavingSubject = new BehaviorSubject<boolean>(false);
|
|
private subscriptions: Subscription[] = [];
|
|
publication!: Publication;
|
|
|
|
get isSaving$(): Observable<boolean> {
|
|
return this.isSavingSubject.asObservable();
|
|
}
|
|
|
|
ngOnInit(): void {
|
|
const authenticatedUser = this.authenticationService.getAuthenticatedUser();
|
|
if (authenticatedUser) {
|
|
const author: Author = {
|
|
id: authenticatedUser.id,
|
|
name: authenticatedUser.pseudo,
|
|
image: authenticatedUser.photoId ?? ''
|
|
};
|
|
this.publication = {
|
|
id: '',
|
|
key: '',
|
|
title: '',
|
|
text: '',
|
|
parsedText: '',
|
|
description: '',
|
|
creationDate: new Date(),
|
|
illustrationId: '',
|
|
categoryId: '',
|
|
author
|
|
};
|
|
}
|
|
}
|
|
|
|
ngOnDestroy(): void {
|
|
this.subscriptions.forEach(subscription => subscription?.unsubscribe());
|
|
}
|
|
|
|
onPublicationSave(publication: Publication): void {
|
|
this.isSavingSubject.next(true);
|
|
this.publicationRestService.create(publication)
|
|
.then(() => {
|
|
this.snackBar.open($localize`Publication created succesfully!`, $localize`Close`, { duration: 5000 });
|
|
this.router.navigate(['/my-publications']);
|
|
})
|
|
.catch(error => {
|
|
const errorMessage = $localize`An error occured while saving new publication.`;
|
|
console.error(errorMessage, error);
|
|
this.snackBar.open(errorMessage, $localize`Close`, { duration: 5000 });
|
|
})
|
|
.finally(() => this.isSavingSubject.next(false));
|
|
}
|
|
} |