Save dark mode to session; auto-save session (Noded/backend#17)
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
2020-10-20 10:15:00 -05:00
parent e1c666e3ad
commit 5053632bf6
3 changed files with 120 additions and 8 deletions

View File

@@ -1,5 +1,6 @@
import { Injectable } from '@angular/core';
import {ApiService} from './api.service';
import {debounce} from './editor.service';
@Injectable({
providedIn: 'root'
@@ -8,6 +9,22 @@ export class SessionService {
public appName!: string;
public systemBase!: string;
protected data: any = {};
protected saveTriggered = false;
protected saving = false;
protected privTriggerSave = debounce(() => {
if ( this.saving ) {
this.triggerSave();
} else {
this.save();
}
this.saveTriggered = false;
}, 3000);
public triggerSave() {
this.saveTriggered = true;
this.privTriggerSave();
}
constructor(
protected readonly api: ApiService,
@@ -30,6 +47,22 @@ export class SessionService {
});
}
async save() {
this.saving = true;
return new Promise((res, rej) => {
this.api.post('/session', this.data || {}).subscribe({
next: result => {
res();
this.saving = false;
},
error: (e) => {
this.saving = false;
rej(e);
},
});
});
}
buildAppUrl(...parts: string[]): string {
parts = parts.map(x => {
if ( x.startsWith('/') ) {
@@ -45,4 +78,45 @@ export class SessionService {
return `${this.systemBase}${this.systemBase.endsWith('/') ? '' : '/'}${parts.join('/')}`;
}
get(path?: string): any {
let current: any = this.data;
if ( !path ) {
return current;
}
const parts = path.split('.');
for ( const part of parts ) {
current = current?.[part];
}
return current;
}
set(pathOrValue: string | any, value?: any): any {
if ( typeof pathOrValue !== 'string' ) {
this.data = pathOrValue;
return;
}
if ( typeof value === 'undefined' ) {
return;
}
const parts = pathOrValue.split('.');
let last;
let current = this.data;
for ( const part of parts ) {
last = current;
current = current?.[part];
}
parts.reverse();
if ( last ) {
last[parts[0]] = value;
}
this.triggerSave();
}
}