changing computer

This commit is contained in:
didier amyot committed 2023-11-21 05:09:29 -05:00
1 parent 9de31fac1b
commit c448efa8fa
2 files changed
+74 -27

No files matched your search

+36 -18
View File
@@ -72,7 +72,7 @@ class BallotView implements m.ClassComponent<BallotViewAttr> {
}
}
class Submit{
class Submit implements m.ClassComponent<unknown> {
view(){
return m("div.columns",
m("div.column",
@@ -87,10 +87,6 @@ type MainVoteViewAttr = {
class MainVoteView implements m.ClassComponent<MainVoteViewAttr>{
async oninit(){
ballots = await m.request(getBallotURL());
}
async persistBallots(): Promise<void>{
const params: RequestInit = {
method: 'POST',
@@ -124,10 +120,6 @@ class MainVoteView implements m.ClassComponent<MainVoteViewAttr>{
class MainView implements m.ClassComponent<unknown>{
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<unknown> {
}
}
class FrameView<Foo> implements m.ClassComponent<unknown>{
class ResultView implements m.ClassComponent<unknown>{
view(){
return m("div", "one two three");
}
}
view(vnode: m.Vnode<unknown,this>): m.Children {
return m("div", [
"hello world",
class FrameView<T extends m.ClassComponent<any>> implements m.ClassComponent<unknown>{
])
}
private readonly embedeed: new () => T;
constructor(embedeed: new () => T) {
this.embedeed = embedeed
}
async oninit(): Promise<void>{
ballots = await m.request(getBallotURL());
}
logout(){
window.localStorage.clear();
m.route.set("/");
}
view(vnode: m.Vnode<unknown>): 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)
})
+38 -9
View File
@@ -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<void>
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<Ballot[]>{
let ballots = createDefaultBallots();
const kv = await Deno.openKv();
const entries = kv.list<QuestionAnswer>({ prefix: getUserIndex2(userId) });
const entries = kv.list<QuestionAnswer>({ 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<void> => {
context.response.body = await createBundle(
new URL("./client.ts", import.meta.url),