diff --git a/ui/app.js b/ui/app.js index ff37a7f..429c388 100644 --- a/ui/app.js +++ b/ui/app.js @@ -2,11 +2,36 @@ import Session from "../models/session.js"; import SessionView from "./session.js"; +import WbDb from "../data/db.js"; +import SessionRepo from "../data/session_repo.js"; export default class App { - #session = new Session(); + #session = null; + + constructor() { + let db = WbDb.get(); + if (db.open) + this.#dbReady(); + else + db.addEventListener(WbDb.EVENT_CHANGE, this.#dbReady.bind(this)); + } + + async #dbReady() { + let sessions = await SessionRepo.getAll(); + if (sessions.length === 0) { + this.#session = new Session(); + SessionRepo.put(this.#session); + } else + this.#session = sessions[0]; + + m.redraw(); + } view() { + if (this.#session === null) { + return m("p", "Warte auf Datenbank."); + } + return m(SessionView, { model: this.#session }); } }