From 09b9eff7c148dcf3132b5e41a025f51a1d91b932 Mon Sep 17 00:00:00 2001 From: Bruno Windels Date: Sun, 8 Sep 2019 10:19:16 +0200 Subject: [PATCH] fix remaining errors to make login work --- src/domain/BrawlViewModel.js | 15 ++++++++------- src/domain/SessionPickerViewModel.js | 6 +++--- src/main.js | 4 ++-- src/matrix/sync.js | 2 +- src/ui/web/login/LoginView.js | 10 +++++++--- 5 files changed, 21 insertions(+), 16 deletions(-) diff --git a/src/domain/BrawlViewModel.js b/src/domain/BrawlViewModel.js index 5518f7e5..1d8d2f09 100644 --- a/src/domain/BrawlViewModel.js +++ b/src/domain/BrawlViewModel.js @@ -6,10 +6,10 @@ import SessionPickerViewModel from "./SessionPickerViewModel.js"; import EventEmitter from "../EventEmitter.js"; export default class BrawlViewModel extends EventEmitter { - constructor({createStorage, sessionsStore, createHsApi, clock}) { + constructor({createStorage, sessionStore, createHsApi, clock}) { super(); this._createStorage = createStorage; - this._sessionsStore = sessionsStore; + this._sessionStore = sessionStore; this._createHsApi = createHsApi; this._clock = clock; @@ -21,7 +21,7 @@ export default class BrawlViewModel extends EventEmitter { } async load() { - if (await this._sessionsStore.hasAnySession()) { + if (await this._sessionStore.hasAnySession()) { this._showPicker(); } else { this._showLogin(); @@ -31,7 +31,7 @@ export default class BrawlViewModel extends EventEmitter { async _showPicker() { this._clearSections(); this._sessionPickerViewModel = new SessionPickerViewModel({ - sessionsStore: this._sessionsStore, + sessionStore: this._sessionStore, sessionCallback: sessionInfo => this._onSessionPicked(sessionInfo) }); this.emit("change", "activeSection"); @@ -101,7 +101,7 @@ export default class BrawlViewModel extends EventEmitter { accessToken: loginData.access_token, lastUsed: this._clock.now() }; - await this._sessionsStore.add(sessionInfo); + await this._sessionStore.add(sessionInfo); this._loadSession(sessionInfo); } else { this._showPicker(); @@ -111,7 +111,7 @@ export default class BrawlViewModel extends EventEmitter { _onSessionPicked(sessionInfo) { if (sessionInfo) { this._loadSession(sessionInfo); - this._sessionsStore.updateLastUsed(sessionInfo.id, this._clock.now()); + this._sessionStore.updateLastUsed(sessionInfo.id, this._clock.now()); } else { this._showLogin(); } @@ -121,7 +121,7 @@ export default class BrawlViewModel extends EventEmitter { try { this._loading = true; this._loadingText = "Loading your conversations…"; - const hsApi = this._createHsApi(sessionInfo.homeServer); + const hsApi = this._createHsApi(sessionInfo.homeServer, sessionInfo.accessToken); const storage = await this._createStorage(sessionInfo.id); // no need to pass access token to session const filteredSessionInfo = { @@ -150,6 +150,7 @@ export default class BrawlViewModel extends EventEmitter { // start sending pending messages session.notifyNetworkAvailable(); } catch (err) { + console.error(err); this._error = err; } this.emit("change", "activeSection"); diff --git a/src/domain/SessionPickerViewModel.js b/src/domain/SessionPickerViewModel.js index 922e8775..3c7dff80 100644 --- a/src/domain/SessionPickerViewModel.js +++ b/src/domain/SessionPickerViewModel.js @@ -1,14 +1,14 @@ -import {SortedArray} from "./observables/index.js"; +import {SortedArray} from "../observable/index.js"; export default class SessionPickerViewModel { constructor({sessionStore, sessionCallback}) { - this._sessionsStore = sessionStore; + this._sessionStore = sessionStore; this._sessionCallback = sessionCallback; this._sessions = new SortedArray((s1, s2) => (s1.lastUsed || 0) - (s2.lastUsed || 0)); } async load() { - const sessions = await this._sessionsStore.getAll(); + const sessions = await this._sessionStore.getAll(); this._sessions.setManyUnsorted(sessions); } diff --git a/src/main.js b/src/main.js index 9b4333a7..78f5f881 100644 --- a/src/main.js +++ b/src/main.js @@ -8,8 +8,8 @@ export default async function main(container) { try { const vm = new BrawlViewModel({ createStorage: sessionId => createIdbStorage(`brawl_session_${sessionId}`), - createHsApi: homeServer => new HomeServerApi(homeServer), - sessionsStore: new SessionsStore("brawl_sessions_v1"), + createHsApi: (homeServer, accessToken = null) => new HomeServerApi(`https://${homeServer}`, accessToken), + sessionStore: new SessionsStore("brawl_sessions_v1"), clock: Date //just for `now` fn }); await vm.load(); diff --git a/src/matrix/sync.js b/src/matrix/sync.js index 270d72e3..33711c91 100644 --- a/src/matrix/sync.js +++ b/src/matrix/sync.js @@ -20,7 +20,7 @@ function parseRooms(roomsSection, roomCallback) { } export default class Sync extends EventEmitter { - constructor(hsApi, session, storage) { + constructor({hsApi, session, storage}) { super(); this._hsApi = hsApi; this._session = session; diff --git a/src/ui/web/login/LoginView.js b/src/ui/web/login/LoginView.js index 77dfde53..7718d72b 100644 --- a/src/ui/web/login/LoginView.js +++ b/src/ui/web/login/LoginView.js @@ -1,9 +1,13 @@ -import TemplateView from "./general/TemplateView.js"; +import TemplateView from "../general/TemplateView.js"; export default class LoginView extends TemplateView { + constructor(vm) { + super(vm, true); + } + render(t, vm) { const username = t.input({type: "text", placeholder: vm.usernamePlaceholder}); - const password = t.input({type: "password", placeholder: vm.usernamePlaceholder}); + const password = t.input({type: "password", placeholder: vm.passwordPlaceholder}); const homeserver = t.input({type: "text", placeholder: vm.hsPlaceholder, value: vm.defaultHomeServer}); return t.div({className: "login form"}, [ t.if(vm => vm.error, t => t.div({className: "error"}, vm => vm.error)), @@ -14,7 +18,7 @@ export default class LoginView extends TemplateView { onClick: () => vm.login(username.value, password.value, homeserver.value), disabled: vm => vm.loading }, "Log In")), - t.div(t.button({onClick: () => vm.cancel()}), "Cancel") + t.div(t.button({onClick: () => vm.cancel()}, ["Cancel"])) ]); } }