use urls instead of callbacks to navigate in session picker

This commit is contained in:
Bruno Windels 2020-10-09 17:02:53 +02:00
parent 7b86b483ed
commit 35e85c55e0
2 changed files with 18 additions and 49 deletions

View File

@ -15,15 +15,14 @@ limitations under the License.
*/ */
import {SortedArray} from "../observable/index.js"; import {SortedArray} from "../observable/index.js";
import {SessionLoadViewModel} from "./SessionLoadViewModel.js";
import {ViewModel} from "./ViewModel.js"; import {ViewModel} from "./ViewModel.js";
import {avatarInitials, getIdentifierColorNumber} from "./avatar.js"; import {avatarInitials, getIdentifierColorNumber} from "./avatar.js";
class SessionItemViewModel extends ViewModel { class SessionItemViewModel extends ViewModel {
constructor(sessionInfo, pickerVM) { constructor(options, pickerVM) {
super({}); super(options);
this._pickerVM = pickerVM; this._pickerVM = pickerVM;
this._sessionInfo = sessionInfo; this._sessionInfo = options.sessionInfo;
this._isDeleting = false; this._isDeleting = false;
this._isClearing = false; this._isClearing = false;
this._error = null; this._error = null;
@ -76,6 +75,10 @@ class SessionItemViewModel extends ViewModel {
return this._sessionInfo.id; return this._sessionInfo.id;
} }
get openUrl() {
return this.urlRouter.urlForSegment("session", this.id);
}
get label() { get label() {
const {userId, comment} = this._sessionInfo; const {userId, comment} = this._sessionInfo;
if (comment) { if (comment) {
@ -127,11 +130,10 @@ class SessionItemViewModel extends ViewModel {
export class SessionPickerViewModel extends ViewModel { export class SessionPickerViewModel extends ViewModel {
constructor(options) { constructor(options) {
super(options); super(options);
const {storageFactory, sessionInfoStorage, sessionCallback, createSessionContainer} = options; const {storageFactory, sessionInfoStorage, sessionInfoCallback} = options;
this._storageFactory = storageFactory; this._storageFactory = storageFactory;
this._sessionInfoStorage = sessionInfoStorage; this._sessionInfoStorage = sessionInfoStorage;
this._sessionCallback = sessionCallback; this._sessionInfoCallback = sessionInfoCallback;
this._createSessionContainer = createSessionContainer;
this._sessions = new SortedArray((s1, s2) => s1.id.localeCompare(s2.id)); this._sessions = new SortedArray((s1, s2) => s1.id.localeCompare(s2.id));
this._loadViewModel = null; this._loadViewModel = null;
this._error = null; this._error = null;
@ -140,7 +142,9 @@ export class SessionPickerViewModel extends ViewModel {
// this loads all the sessions // this loads all the sessions
async load() { async load() {
const sessions = await this._sessionInfoStorage.getAll(); const sessions = await this._sessionInfoStorage.getAll();
this._sessions.setManyUnsorted(sessions.map(s => new SessionItemViewModel(s, this))); this._sessions.setManyUnsorted(sessions.map(s => {
return new SessionItemViewModel(this.childOptions({sessionInfo: s}), this);
}));
} }
// for the loading of 1 picked session // for the loading of 1 picked session
@ -148,34 +152,6 @@ export class SessionPickerViewModel extends ViewModel {
return this._loadViewModel; return this._loadViewModel;
} }
async pick(id) {
if (this._loadViewModel) {
return;
}
const sessionVM = this._sessions.array.find(s => s.id === id);
if (sessionVM) {
this._loadViewModel = new SessionLoadViewModel({
createAndStartSessionContainer: () => {
const sessionContainer = this._createSessionContainer();
sessionContainer.startWithExistingSession(sessionVM.id);
return sessionContainer;
},
sessionCallback: sessionContainer => {
if (sessionContainer) {
// make parent view model move away
this._sessionCallback(sessionContainer);
} else {
// show list of session again
this._loadViewModel = null;
this.emitChange("loadViewModel");
}
}
});
this._loadViewModel.start();
this.emitChange("loadViewModel");
}
}
async _exportData(id) { async _exportData(id) {
const sessionInfo = await this._sessionInfoStorage.get(id); const sessionInfo = await this._sessionInfoStorage.get(id);
const stores = await this._storageFactory.export(id); const stores = await this._storageFactory.export(id);
@ -213,9 +189,7 @@ export class SessionPickerViewModel extends ViewModel {
return this._sessions; return this._sessions;
} }
cancel() { get cancelUrl() {
if (!this._loadViewModel) { return this.urlRouter.urlForSegment("login");
this._sessionCallback();
}
} }
} }

View File

@ -79,7 +79,7 @@ class SessionPickerItemView extends TemplateView {
})); }));
const errorMessage = t.if(vm => vm.error, t.createTemplate(t => t.p({className: "error"}, vm => vm.error))); const errorMessage = t.if(vm => vm.error, t.createTemplate(t => t.p({className: "error"}, vm => vm.error)));
return t.li([ return t.li([
t.div({className: "session-info"}, [ t.a({className: "session-info", href: vm.openUrl}, [
t.div({className: `avatar usercolor${vm.avatarColorNumber}`}, vm => vm.avatarInitials), t.div({className: `avatar usercolor${vm.avatarColorNumber}`}, vm => vm.avatarInitials),
t.div({className: "user-id"}, vm => vm.label), t.div({className: "user-id"}, vm => vm.label),
]), ]),
@ -98,11 +98,6 @@ export class SessionPickerView extends TemplateView {
render(t, vm) { render(t, vm) {
const sessionList = new ListView({ const sessionList = new ListView({
list: vm.sessions, list: vm.sessions,
onItemClick: (item, event) => {
if (event.target.closest(".session-info")) {
vm.pick(item.value.id);
}
},
parentProvidesUpdates: false, parentProvidesUpdates: false,
}, sessionInfo => { }, sessionInfo => {
return new SessionPickerItemView(sessionInfo); return new SessionPickerItemView(sessionInfo);
@ -118,9 +113,9 @@ export class SessionPickerView extends TemplateView {
className: "styled secondary", className: "styled secondary",
onClick: async () => vm.import(await selectFileAsText("application/json")) onClick: async () => vm.import(await selectFileAsText("application/json"))
}, vm.i18n`Import a session`), }, vm.i18n`Import a session`),
t.button({ t.a({
className: "styled primary", className: "button styled primary",
onClick: () => vm.cancel() href: vm.cancelUrl
}, vm.i18n`Sign In`) }, vm.i18n`Sign In`)
]), ]),
t.if(vm => vm.loadViewModel, vm => new SessionLoadStatusView(vm.loadViewModel)), t.if(vm => vm.loadViewModel, vm => new SessionLoadStatusView(vm.loadViewModel)),