Fix field edition problem.
This commit is contained in:
@@ -3,13 +3,14 @@ import { inject, Injectable, OnDestroy } from "@angular/core";
|
||||
import { MatDialog } from "@angular/material/dialog";
|
||||
import { MatSnackBar } from "@angular/material/snack-bar";
|
||||
import { ActivatedRoute } from "@angular/router";
|
||||
import { BehaviorSubject, Observable, Subscription } from "rxjs";
|
||||
import { BehaviorSubject, debounceTime, distinctUntilChanged, Observable, Subscription } 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";
|
||||
import { CodeBlockDialog } from "./code-block-dialog/code-block-dialog.component";
|
||||
import { PictureSelectionDialog } from "./picture-selection-dialog/picture-selection-dialog.component";
|
||||
import { PreviewContentRequest } from "../../core/rest-services/publications/model/preview";
|
||||
import { FormBuilder, FormControl, FormGroup, Validators } from "@angular/forms";
|
||||
|
||||
declare let Prism: any;
|
||||
|
||||
@@ -57,10 +58,11 @@ const DEFAULT_STATE: PublicationEditionState = {
|
||||
@Injectable()
|
||||
export class PublicationEditionService implements OnDestroy {
|
||||
private readonly activatedRoute = inject(ActivatedRoute);
|
||||
private readonly publicationRestService = inject(PublicationRestService);
|
||||
private readonly location = inject(Location);
|
||||
private readonly snackBar = inject(MatSnackBar);
|
||||
private readonly dialog = inject(MatDialog);
|
||||
private readonly formBuilder = inject(FormBuilder);
|
||||
private readonly location = inject(Location);
|
||||
private readonly publicationRestService = inject(PublicationRestService);
|
||||
private readonly snackBar = inject(MatSnackBar);
|
||||
|
||||
private isLoadingSubject = new BehaviorSubject<boolean>(false);
|
||||
private stateSubject = new BehaviorSubject<PublicationEditionState>(copy(DEFAULT_STATE));
|
||||
@@ -68,6 +70,14 @@ export class PublicationEditionService implements OnDestroy {
|
||||
private isSavingSubject = new BehaviorSubject<boolean>(false);
|
||||
private isPreviewingSubject = new BehaviorSubject<boolean>(false);
|
||||
|
||||
publicationEditionForm: FormGroup = this.formBuilder.group({
|
||||
title: new FormControl<string | undefined>('', [Validators.required]),
|
||||
description: new FormControl<string | undefined>('', [Validators.required]),
|
||||
text: new FormControl<string | undefined>('', [Validators.required]),
|
||||
illustrationId: new FormControl<string | undefined>('', [Validators.required]),
|
||||
categoryId: new FormControl<string | undefined>('', [Validators.required])
|
||||
});
|
||||
|
||||
ngOnDestroy(): void {
|
||||
this.subscriptions.forEach(subscription => subscription.unsubscribe());
|
||||
}
|
||||
@@ -80,6 +90,17 @@ export class PublicationEditionService implements OnDestroy {
|
||||
this.stateSubject.next(state);
|
||||
}
|
||||
|
||||
private _updateForm(): void {
|
||||
const state = this._state;
|
||||
const publication = state.publication;
|
||||
|
||||
this.publicationEditionForm.controls['title'].setValue(publication.title);
|
||||
this.publicationEditionForm.controls['description'].setValue(publication.description);
|
||||
this.publicationEditionForm.controls['text'].setValue(publication.text);
|
||||
this.publicationEditionForm.controls['illustrationId'].setValue(publication.illustrationId);
|
||||
this.publicationEditionForm.controls['categoryId'].setValue(publication.categoryId);
|
||||
}
|
||||
|
||||
get isLoading$(): Observable<boolean> {
|
||||
return this.isLoadingSubject.asObservable();
|
||||
}
|
||||
@@ -96,6 +117,10 @@ export class PublicationEditionService implements OnDestroy {
|
||||
return this.stateSubject.asObservable();
|
||||
}
|
||||
|
||||
get editedPublication(): Publication {
|
||||
return this._state.publication;
|
||||
}
|
||||
|
||||
loadPublication(): void {
|
||||
this.isLoadingSubject.next(true);
|
||||
|
||||
@@ -106,52 +131,47 @@ export class PublicationEditionService implements OnDestroy {
|
||||
this.location.back();
|
||||
} else {
|
||||
this.publicationRestService.getById(publicationId)
|
||||
.then(publication => {
|
||||
const state = this._state;
|
||||
state.publication = publication;
|
||||
this.stateSubject.next(state);
|
||||
})
|
||||
.catch(error => {
|
||||
const errorMessage = $localize`A technical error occurred while loading publication data.`;
|
||||
this.snackBar.open(errorMessage, $localize`Close`, {duration: 5000});
|
||||
console.error(errorMessage, error)
|
||||
})
|
||||
.finally(() => this.isLoadingSubject.next(false));
|
||||
.then(publication => {
|
||||
const state = this._state;
|
||||
state.publication = publication;
|
||||
this.stateSubject.next(state);
|
||||
})
|
||||
.catch(error => {
|
||||
const errorMessage = $localize`A technical error occurred while loading publication data.`;
|
||||
this.snackBar.open(errorMessage, $localize`Close`, {duration: 5000});
|
||||
console.error(errorMessage, error)
|
||||
})
|
||||
.finally(() => this.isLoadingSubject.next(false));
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
init(publication: Publication): void {
|
||||
const state = this._state;
|
||||
state.publication = publication;
|
||||
this.stateSubject.next(state);
|
||||
this._updateForm();
|
||||
|
||||
const formValueChangesSubscription = this.publicationEditionForm.valueChanges
|
||||
.pipe(
|
||||
debounceTime(200),
|
||||
distinctUntilChanged()
|
||||
)
|
||||
.subscribe(formValue => {
|
||||
const state = this._state;
|
||||
const publication = state.publication;
|
||||
|
||||
publication.title = formValue.title;
|
||||
publication.description = formValue.description;
|
||||
publication.categoryId = formValue.categoryId;
|
||||
publication.text = formValue.text;
|
||||
|
||||
this._save(state);
|
||||
})
|
||||
this.subscriptions.push(formValueChangesSubscription);
|
||||
}
|
||||
|
||||
editTitle(title: string): void {
|
||||
const state = this._state;
|
||||
state.publication.title = title;
|
||||
this._save(state);
|
||||
}
|
||||
|
||||
editDescription(description: string): void {
|
||||
const state = this._state;
|
||||
state.publication.description = description;
|
||||
this._save(state);
|
||||
}
|
||||
|
||||
editCategoryId(categoryId: string): void {
|
||||
const state = this._state;
|
||||
state.publication.categoryId = categoryId;
|
||||
this._save(state);
|
||||
}
|
||||
|
||||
editText(text: string): void {
|
||||
const state = this._state;
|
||||
state.publication.text = text;
|
||||
this._save(state);
|
||||
}
|
||||
|
||||
editIllustrationId(pictureId: string): void {
|
||||
private editIllustrationId(pictureId: string): void {
|
||||
const state = this._state;
|
||||
state.publication.illustrationId = pictureId
|
||||
this._save(state);
|
||||
@@ -204,6 +224,7 @@ export class PublicationEditionService implements OnDestroy {
|
||||
publication.text = textWithTags;
|
||||
|
||||
this._save(state);
|
||||
this._updateForm();
|
||||
} else {
|
||||
console.error(`Bad value for parameter of function 'insertTitle': '${titleNumber}'.`);
|
||||
}
|
||||
@@ -233,6 +254,7 @@ export class PublicationEditionService implements OnDestroy {
|
||||
publication.text = textWithTags;
|
||||
|
||||
this._save(state);
|
||||
this._updateForm();
|
||||
}
|
||||
|
||||
insertLink(): void {
|
||||
@@ -248,9 +270,10 @@ export class PublicationEditionService implements OnDestroy {
|
||||
publication.text = textWithTags;
|
||||
|
||||
this._save(state);
|
||||
this._updateForm();
|
||||
}
|
||||
|
||||
insertCodeBlock(programmingLanguage: string, codeBlock: string): void {
|
||||
private insertCodeBlock(programmingLanguage: string, codeBlock: string): void {
|
||||
const state = this._state;
|
||||
|
||||
const publication = state.publication;
|
||||
@@ -263,6 +286,7 @@ export class PublicationEditionService implements OnDestroy {
|
||||
publication.text = textWithTags;
|
||||
|
||||
this._save(state);
|
||||
this._updateForm();
|
||||
}
|
||||
|
||||
loadPreview(): void {
|
||||
|
||||
Reference in New Issue
Block a user