Save page on name change & refresh sidebar (#20)
All checks were successful
continuous-integration/drone/push Build is passing
continuous-integration/drone Build is passing

This commit is contained in:
2020-10-19 09:10:29 -05:00
parent 292bf6c729
commit 07664e29cd
3 changed files with 50 additions and 4 deletions

View File

@@ -4,6 +4,7 @@ import PageRecord from '../structures/PageRecord';
import HostRecord from '../structures/HostRecord';
import {EditorNodeContract} from '../components/nodes/EditorNode.contract';
import {BehaviorSubject, Subscription} from 'rxjs';
import {NavigationService} from './navigation.service';
export class NoPageLoadedError extends Error {
constructor(msg = 'There is no page open for editing.') {
@@ -73,6 +74,7 @@ export class EditorService {
if ( this.currentPage && this.canEdit() ) {
if ( this.currentPage.Name !== name ) {
this.dirtyOverride = true;
this.triggerSave();
}
this.currentPage.Name = name;
@@ -81,6 +83,7 @@ export class EditorService {
constructor(
protected api: ApiService,
protected nav: NavigationService,
) { }
async startEditing(pageId: string) {
@@ -122,9 +125,11 @@ export class EditorService {
await editor.writeChangesToNode();
}));
await this.savePage(this.currentPage);
await this.saveNodesAsPage(this.currentPage, this.currentNodes);
this.dirtyOverride = false;
this.saving = false;
this.nav.requestSidebarRefresh({ quiet: true });
}
async moveNode(node: HostRecord, direction: 'up' | 'down') {
@@ -153,6 +158,20 @@ export class EditorService {
this.triggerSave();
}
async savePage(page: PageRecord): Promise<void> {
await new Promise((res, rej) => {
const saveData = page.toSave();
this.api.post(`/page/${page.UUID}/save`, saveData).subscribe({
next: result => {
console.log('save result', result);
res();
},
error: rej,
});
});
}
async saveNodesAsPage(page: PageRecord, nodes: HostRecord[]): Promise<HostRecord[]> {
return new Promise((res, rej) => {
const saveNodes = nodes.map(x => {

View File

@@ -0,0 +1,15 @@
import { Injectable } from '@angular/core';
import {BehaviorSubject} from 'rxjs';
@Injectable({
providedIn: 'root'
})
export class NavigationService {
protected refreshCount = 0;
public readonly sidebarRefresh$: BehaviorSubject<[number, boolean]> = new BehaviorSubject<[number, boolean]>([this.refreshCount, true]);
requestSidebarRefresh({ quiet = false }) {
this.refreshCount += 1;
this.sidebarRefresh$.next([this.refreshCount, quiet]);
}
}