diff --git a/src/client.ts b/src/client.ts index c263b3f..b404369 100644 --- a/src/client.ts +++ b/src/client.ts @@ -72,7 +72,7 @@ class BallotView implements m.ClassComponent { } } -class Submit{ +class Submit implements m.ClassComponent { view(){ return m("div.columns", m("div.column", @@ -87,10 +87,6 @@ type MainVoteViewAttr = { class MainVoteView implements m.ClassComponent{ - async oninit(){ - ballots = await m.request(getBallotURL()); - } - async persistBallots(): Promise{ const params: RequestInit = { method: 'POST', @@ -124,10 +120,6 @@ class MainVoteView implements m.ClassComponent{ class MainView implements m.ClassComponent{ - async oninit(){ - ballots = await m.request(getBallotURL()); - } - createBallotRow(ballot: Ballot){ const ballotURL = `${m.route.get()}vote/${ballot.id}/`; return m('tr', @@ -167,21 +159,47 @@ class PasscodeView implements m.ClassComponent { } } -class FrameView implements m.ClassComponent{ +class ResultView implements m.ClassComponent{ + view(){ + return m("div", "one two three"); + } +} - view(vnode: m.Vnode): m.Children { - return m("div", [ - "hello world", +class FrameView> implements m.ClassComponent{ - ]) - } + private readonly embedeed: new () => T; + constructor(embedeed: new () => T) { + this.embedeed = embedeed + } + async oninit(): Promise{ + ballots = await m.request(getBallotURL()); + } + + logout(){ + window.localStorage.clear(); + m.route.set("/"); + } + view(vnode: m.Vnode): m.Children { + return m("div", [ + m("nav.navbar.is-primary", [ + m("div.navbar-item", + m("img", {src: "https://all-dressed-programming.com/images/pizza.svg"}), + m("span.ml-1.title.is-5", "Stupidly Over Engineered Voting System")), + m("div.navbar-end", + m("div.navbar-item" , + m("a.is-clickable", {onclick: () => this.logout()}, "logout"))) + ]), + m(this.embedeed, vnode.attrs) + ]); + } } m.route(window.document.body, "/", { - "/": PasscodeView, - "/voter-home/": MainView, - "/voter-home/vote/:ballotId/": MainVoteView + "/": new FrameView(PasscodeView), + "/voter-home/": new FrameView(MainView), + "/results": new FrameView(ResultView), + "/voter-home/vote/:ballotId/": new FrameView(MainVoteView) }) \ No newline at end of file diff --git a/src/server.ts b/src/server.ts index 530c524..8a721b1 100644 --- a/src/server.ts +++ b/src/server.ts @@ -33,12 +33,31 @@ router.get("/", (context) => { `; }); -function getUserIndex2(userId: string): string[] { - return ["tatoo-vote", "v1", "results", "user", userId]; + +function getIndexPrefix(): string[]{ + return ["tatoo-vote", "v1"]; } -function getUserIndex(userId: string, ballot: Ballot, question: Question): string[]{ - const prefix = getUserIndex2(userId); +function getUserIndex(userId: string): string[] { + const prefix = getIndexPrefix(); + const suffix = ["user", userId]; + return prefix.concat(suffix); +} + +function getBallotQuestionIndex(ballot: Ballot, question: Question): string[]{ + const prefix = getIndexPrefix(); + const suffix = ["ballot", ballot.id, "question", question.id]; + return prefix.concat(suffix); +} + +function getBallotQuestionUserIndex(userId: string, ballot: Ballot, question: Question): string[]{ + const prefix = getBallotQuestionIndex(ballot, question); + const suffix = ["user", userId]; + return prefix.concat(suffix); +} + +function getUserBallotQuestionIndex(userId: string, ballot: Ballot, question: Question): string[]{ + const prefix = getUserIndex(userId); const suffix = ["ballot", ballot.id, "question", question.id]; return prefix.concat(suffix); } @@ -47,16 +66,16 @@ async function persistBallots(userId: string, ballots: Ballot[]): Promise const kv = await Deno.openKv(); for(const ballot of ballots){ for(const question of ballot.questions){ - const k = getUserIndex(userId, ballot, question); const v: QuestionAnswer = {value: question.value}; - await kv.set(k, v); + await kv.set(getUserBallotQuestionIndex(userId, ballot, question), v); + await kv.set(getBallotQuestionUserIndex(userId, ballot, question), v) } } } function setValueInQuestion(ballots: Ballot[], key: string[], value: number){ - const questionId = key[8]; - const ballotId = key[6]; + const questionId = key[7]; + const ballotId = key[5]; for(const ballot of ballots){ for(const question of ballot.questions){ if(question.id === questionId && ballot.id === ballotId){ @@ -70,14 +89,20 @@ function setValueInQuestion(ballots: Ballot[], key: string[], value: number){ async function getBallots(userId: string): Promise{ let ballots = createDefaultBallots(); const kv = await Deno.openKv(); - const entries = kv.list({ prefix: getUserIndex2(userId) }); + const entries = kv.list({ prefix: getUserIndex(userId) }); for await (const entry of entries) { const key = entry.key; // ["users", "alice"] const value = entry.value.value; // { name: "Alice" } ballots = setValueInQuestion(ballots, key, value); } return ballots; +} +async function getBallotResults(){ + let ballots = createDefaultBallots(); + for(const ballot of ballots){ + + } } router.post("/api/v1/vote/:userid", async (ctx: Context)=> { @@ -102,6 +127,10 @@ router.get("/api/v1/vote/:userid", async (ctx: Context)=> { ctx.response.body = await getBallots(userId); }); +router.get("/api/v1/results", async (ctx: Context) =>{ + ctx.response.body = await getBallotResults(); +}); + router.get("/client.js", async (context): Promise => { context.response.body = await createBundle( new URL("./client.ts", import.meta.url),