fix remaining errors to make login work

This commit is contained in:
Bruno Windels 2019-09-08 10:19:16 +02:00
parent a7194e0c7a
commit 09b9eff7c1
5 changed files with 21 additions and 16 deletions

View file

@ -6,10 +6,10 @@ import SessionPickerViewModel from "./SessionPickerViewModel.js";
import EventEmitter from "../EventEmitter.js"; import EventEmitter from "../EventEmitter.js";
export default class BrawlViewModel extends EventEmitter { export default class BrawlViewModel extends EventEmitter {
constructor({createStorage, sessionsStore, createHsApi, clock}) { constructor({createStorage, sessionStore, createHsApi, clock}) {
super(); super();
this._createStorage = createStorage; this._createStorage = createStorage;
this._sessionsStore = sessionsStore; this._sessionStore = sessionStore;
this._createHsApi = createHsApi; this._createHsApi = createHsApi;
this._clock = clock; this._clock = clock;
@ -21,7 +21,7 @@ export default class BrawlViewModel extends EventEmitter {
} }
async load() { async load() {
if (await this._sessionsStore.hasAnySession()) { if (await this._sessionStore.hasAnySession()) {
this._showPicker(); this._showPicker();
} else { } else {
this._showLogin(); this._showLogin();
@ -31,7 +31,7 @@ export default class BrawlViewModel extends EventEmitter {
async _showPicker() { async _showPicker() {
this._clearSections(); this._clearSections();
this._sessionPickerViewModel = new SessionPickerViewModel({ this._sessionPickerViewModel = new SessionPickerViewModel({
sessionsStore: this._sessionsStore, sessionStore: this._sessionStore,
sessionCallback: sessionInfo => this._onSessionPicked(sessionInfo) sessionCallback: sessionInfo => this._onSessionPicked(sessionInfo)
}); });
this.emit("change", "activeSection"); this.emit("change", "activeSection");
@ -101,7 +101,7 @@ export default class BrawlViewModel extends EventEmitter {
accessToken: loginData.access_token, accessToken: loginData.access_token,
lastUsed: this._clock.now() lastUsed: this._clock.now()
}; };
await this._sessionsStore.add(sessionInfo); await this._sessionStore.add(sessionInfo);
this._loadSession(sessionInfo); this._loadSession(sessionInfo);
} else { } else {
this._showPicker(); this._showPicker();
@ -111,7 +111,7 @@ export default class BrawlViewModel extends EventEmitter {
_onSessionPicked(sessionInfo) { _onSessionPicked(sessionInfo) {
if (sessionInfo) { if (sessionInfo) {
this._loadSession(sessionInfo); this._loadSession(sessionInfo);
this._sessionsStore.updateLastUsed(sessionInfo.id, this._clock.now()); this._sessionStore.updateLastUsed(sessionInfo.id, this._clock.now());
} else { } else {
this._showLogin(); this._showLogin();
} }
@ -121,7 +121,7 @@ export default class BrawlViewModel extends EventEmitter {
try { try {
this._loading = true; this._loading = true;
this._loadingText = "Loading your conversations…"; 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); const storage = await this._createStorage(sessionInfo.id);
// no need to pass access token to session // no need to pass access token to session
const filteredSessionInfo = { const filteredSessionInfo = {
@ -150,6 +150,7 @@ export default class BrawlViewModel extends EventEmitter {
// start sending pending messages // start sending pending messages
session.notifyNetworkAvailable(); session.notifyNetworkAvailable();
} catch (err) { } catch (err) {
console.error(err);
this._error = err; this._error = err;
} }
this.emit("change", "activeSection"); this.emit("change", "activeSection");

View file

@ -1,14 +1,14 @@
import {SortedArray} from "./observables/index.js"; import {SortedArray} from "../observable/index.js";
export default class SessionPickerViewModel { export default class SessionPickerViewModel {
constructor({sessionStore, sessionCallback}) { constructor({sessionStore, sessionCallback}) {
this._sessionsStore = sessionStore; this._sessionStore = sessionStore;
this._sessionCallback = sessionCallback; this._sessionCallback = sessionCallback;
this._sessions = new SortedArray((s1, s2) => (s1.lastUsed || 0) - (s2.lastUsed || 0)); this._sessions = new SortedArray((s1, s2) => (s1.lastUsed || 0) - (s2.lastUsed || 0));
} }
async load() { async load() {
const sessions = await this._sessionsStore.getAll(); const sessions = await this._sessionStore.getAll();
this._sessions.setManyUnsorted(sessions); this._sessions.setManyUnsorted(sessions);
} }

View file

@ -8,8 +8,8 @@ export default async function main(container) {
try { try {
const vm = new BrawlViewModel({ const vm = new BrawlViewModel({
createStorage: sessionId => createIdbStorage(`brawl_session_${sessionId}`), createStorage: sessionId => createIdbStorage(`brawl_session_${sessionId}`),
createHsApi: homeServer => new HomeServerApi(homeServer), createHsApi: (homeServer, accessToken = null) => new HomeServerApi(`https://${homeServer}`, accessToken),
sessionsStore: new SessionsStore("brawl_sessions_v1"), sessionStore: new SessionsStore("brawl_sessions_v1"),
clock: Date //just for `now` fn clock: Date //just for `now` fn
}); });
await vm.load(); await vm.load();

View file

@ -20,7 +20,7 @@ function parseRooms(roomsSection, roomCallback) {
} }
export default class Sync extends EventEmitter { export default class Sync extends EventEmitter {
constructor(hsApi, session, storage) { constructor({hsApi, session, storage}) {
super(); super();
this._hsApi = hsApi; this._hsApi = hsApi;
this._session = session; this._session = session;

View file

@ -1,9 +1,13 @@
import TemplateView from "./general/TemplateView.js"; import TemplateView from "../general/TemplateView.js";
export default class LoginView extends TemplateView { export default class LoginView extends TemplateView {
constructor(vm) {
super(vm, true);
}
render(t, vm) { render(t, vm) {
const username = t.input({type: "text", placeholder: vm.usernamePlaceholder}); 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}); const homeserver = t.input({type: "text", placeholder: vm.hsPlaceholder, value: vm.defaultHomeServer});
return t.div({className: "login form"}, [ return t.div({className: "login form"}, [
t.if(vm => vm.error, t => t.div({className: "error"}, vm => vm.error)), 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), onClick: () => vm.login(username.value, password.value, homeserver.value),
disabled: vm => vm.loading disabled: vm => vm.loading
}, "Log In")), }, "Log In")),
t.div(t.button({onClick: () => vm.cancel()}), "Cancel") t.div(t.button({onClick: () => vm.cancel()}, ["Cancel"]))
]); ]);
} }
} }