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";
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");

View file

@ -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);
}

View file

@ -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();

View file

@ -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;

View file

@ -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"]))
]);
}
}