From 6f73d3983febe5c05729a063084520db6156a66a Mon Sep 17 00:00:00 2001 From: Bruno Windels Date: Sun, 13 Oct 2019 08:16:08 +0200 Subject: [PATCH] add export/import of session data --- src/domain/SessionPickerViewModel.js | 12 +++++++++++- src/ui/web/login/SessionPickerView.js | 3 ++- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/src/domain/SessionPickerViewModel.js b/src/domain/SessionPickerViewModel.js index a0a8db64..56cd3571 100644 --- a/src/domain/SessionPickerViewModel.js +++ b/src/domain/SessionPickerViewModel.js @@ -32,6 +32,7 @@ class SessionItemViewModel extends EventEmitter { async clear() { this._isClearing = true; + alert(JSON.stringify(this._sessionInfo, undefined, 2)); this.emit("change", "isClearing"); try { await this._pickerVM.clear(this.id); @@ -71,7 +72,7 @@ export default class SessionPickerViewModel { this._storageFactory = storageFactory; this._sessionStore = sessionStore; this._sessionCallback = sessionCallback; - this._sessions = new SortedArray((s1, s2) => (s1.sessionInfo.lastUsed || 0) - (s2.sessionInfo.lastUsed || 0)); + this._sessions = new SortedArray((s1, s2) => s1.id.localeCompare(s2.id)); } async load() { @@ -86,6 +87,15 @@ export default class SessionPickerViewModel { } } + async import(json) { + const sessionInfo = JSON.parse(json); + const sessionId = (Math.floor(Math.random() * Number.MAX_SAFE_INTEGER)).toString(); + sessionInfo.id = sessionId; + sessionInfo.lastUsed = sessionId; + await this._sessionStore.add(sessionInfo); + this._sessions.set(new SessionItemViewModel(sessionInfo, this)); + } + async delete(id) { const idx = this._sessions.array.findIndex(s => s.id === id); await this._sessionStore.delete(id); diff --git a/src/ui/web/login/SessionPickerView.js b/src/ui/web/login/SessionPickerView.js index e14ba2c1..044392e4 100644 --- a/src/ui/web/login/SessionPickerView.js +++ b/src/ui/web/login/SessionPickerView.js @@ -54,7 +54,8 @@ export default class SessionPickerView extends TemplateView { return t.div({className: "SessionPickerView"}, [ t.h1(["Pick a session"]), this._sessionList.mount(), - t.button({onClick: () => this.viewModel.cancel()}, ["Log in to a new session instead"]) + t.p(t.button({onClick: () => this.viewModel.cancel()}, ["Log in to a new session instead"])), + t.p(t.button({onClick: () => this.viewModel.import(prompt("JSON"))}, ["Import Session JSON"])) ]); }