Add publication edition form.
This commit is contained in:
@@ -0,0 +1,93 @@
|
||||
import { Location } from "@angular/common";
|
||||
import { inject, Injectable } from "@angular/core";
|
||||
import { MatSnackBar } from "@angular/material/snack-bar";
|
||||
import { ActivatedRoute } from "@angular/router";
|
||||
import { BehaviorSubject, Observable } from "rxjs";
|
||||
import { Publication } from "../../core/rest-services/publications/model/publication";
|
||||
import { PublicationRestService } from "../../core/rest-services/publications/publication.rest-service";
|
||||
import { copy } from '../../core/utils/ObjectUtils';
|
||||
|
||||
const DEFAULT_PUBLICATION: Publication = {
|
||||
id: '',
|
||||
key: '',
|
||||
title: '',
|
||||
text: '',
|
||||
parsedText: '',
|
||||
description: '',
|
||||
creationDate: new Date(),
|
||||
illustrationId: '',
|
||||
categoryId: '',
|
||||
author: {
|
||||
id: '',
|
||||
name: '',
|
||||
image: ''
|
||||
}
|
||||
};
|
||||
|
||||
@Injectable()
|
||||
export class PublicationEditionService {
|
||||
private activatedRoute = inject(ActivatedRoute);
|
||||
private publicationRestService = inject(PublicationRestService);
|
||||
private location = inject(Location);
|
||||
private snackBar = inject(MatSnackBar);
|
||||
|
||||
private isLoadingSubject = new BehaviorSubject<boolean>(false);
|
||||
private publicationSubject = new BehaviorSubject<Publication>(copy(DEFAULT_PUBLICATION));
|
||||
|
||||
private get _publication(): Publication {
|
||||
return this.publicationSubject.value;
|
||||
}
|
||||
|
||||
private _save(publication: Publication): void {
|
||||
this.publicationSubject.next(publication);
|
||||
}
|
||||
|
||||
get isLoading$(): Observable<boolean> {
|
||||
return this.isLoadingSubject.asObservable();
|
||||
}
|
||||
|
||||
get publication$(): Observable<Publication> {
|
||||
return this.publicationSubject.asObservable();
|
||||
}
|
||||
|
||||
loadPublication(): void {
|
||||
this.isLoadingSubject.next(true);
|
||||
|
||||
this.activatedRoute.paramMap.subscribe(params => {
|
||||
const publicationId = params.get('publicationId');
|
||||
if (publicationId == undefined) {
|
||||
this.snackBar.open('A technical error occurred while loading publication data.', 'Close', {duration: 5000});
|
||||
this.location.back();
|
||||
} else {
|
||||
this.publicationRestService.getById(publicationId)
|
||||
.then(publication => {
|
||||
this.publicationSubject.next(publication);
|
||||
})
|
||||
.catch(error => {
|
||||
const errorMessage = 'A technical error occurred while loading publication data.';
|
||||
this.snackBar.open(errorMessage, 'Close', {duration: 5000});
|
||||
console.error(errorMessage, error)
|
||||
})
|
||||
.finally(() => this.isLoadingSubject.next(false));
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
editTitle(title: string): void {
|
||||
const publication = this._publication;
|
||||
publication.title = title;
|
||||
this._save(publication);
|
||||
}
|
||||
|
||||
editDescription(description: string): void {
|
||||
const publication = this._publication;
|
||||
publication.description = description;
|
||||
this._save(publication);
|
||||
}
|
||||
|
||||
editText(text: string): void {
|
||||
const publication = this._publication;
|
||||
publication.text = text;
|
||||
this._save(publication);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user