Refactored team to ballot.
This commit is contained in:
1 parent
b52f76d3bc
commit
ae7e65ba44
3 files changed
+30
-30
No files matched your search
+25
-25
@@ -1,7 +1,7 @@
|
|||||||
// @deno-types="https://esm.sh/@types/mithril@2.2.4"
|
// @deno-types="https://esm.sh/@types/mithril@2.2.4"
|
||||||
import m from "https://esm.sh/mithril@2.2.2"
|
import m from "https://esm.sh/mithril@2.2.2"
|
||||||
import {verifyAuth} from "./auth.ts";
|
import {verifyAuth} from "./auth.ts";
|
||||||
import {Scores, Team} from "./shared.ts";
|
import {Scores, Ballot} from "./shared.ts";
|
||||||
|
|
||||||
|
|
||||||
const scores: Scores = {
|
const scores: Scores = {
|
||||||
@@ -10,7 +10,7 @@ const scores: Scores = {
|
|||||||
convinced: 75
|
convinced: 75
|
||||||
};
|
};
|
||||||
|
|
||||||
const teams: Team[] = [
|
const ballots: Ballot[] = [
|
||||||
{id: 8787, name: "Les chacals de laval", scores: {...scores}},
|
{id: 8787, name: "Les chacals de laval", scores: {...scores}},
|
||||||
{id: 18167, name: "Les rapides de blainville", scores: {...scores}},
|
{id: 18167, name: "Les rapides de blainville", scores: {...scores}},
|
||||||
{id: 17165, name: "Les pas-beaux du plateau", scores: {...scores}},
|
{id: 17165, name: "Les pas-beaux du plateau", scores: {...scores}},
|
||||||
@@ -54,30 +54,30 @@ class ProgressBar implements m.ClassComponent<ProgressBarAttr>{
|
|||||||
}
|
}
|
||||||
|
|
||||||
type VoteViewAttr = {
|
type VoteViewAttr = {
|
||||||
teamId: number,
|
ballotId: number,
|
||||||
onchange?: () => void,
|
onchange?: () => void,
|
||||||
};
|
};
|
||||||
|
|
||||||
class VoteView implements m.ClassComponent<VoteViewAttr> {
|
class BallotView implements m.ClassComponent<VoteViewAttr> {
|
||||||
|
|
||||||
voteFor(team: Team, title: string, key: string, onchange: () => void){
|
voteFor(ballot: Ballot, title: string, key: string, onchange: () => void){
|
||||||
return m("div.columns",
|
return m("div.columns",
|
||||||
m("div.column", title, ":"),
|
m("div.column", title, ":"),
|
||||||
m("div.column.is-two-thirds",
|
m("div.column.is-two-thirds",
|
||||||
m(new ProgressBar(), {scores: team.scores, key: key, onchange: onchange})));
|
m(new ProgressBar(), {scores: ballot.scores, key: key, onchange: onchange})));
|
||||||
}
|
}
|
||||||
|
|
||||||
view(vnode: m.CVnode<VoteViewAttr>){
|
view(vnode: m.CVnode<VoteViewAttr>){
|
||||||
const teamId = vnode.attrs.teamId;
|
const ballotId = vnode.attrs.ballotId;
|
||||||
const onchange = vnode.attrs.onchange ?? function(){};
|
const onchange = vnode.attrs.onchange ?? function(){};
|
||||||
const team = teams.find(team => team.id.toString() === teamId.toString());
|
const ballot = ballots.find(ballot => ballot.id.toString() === ballotId.toString());
|
||||||
if(!team){
|
if(!ballot){
|
||||||
return [];
|
return [];
|
||||||
}
|
}
|
||||||
return [
|
return [
|
||||||
this.voteFor(team, "Qualité du README", 'readme', onchange),
|
this.voteFor(ballot, "Qualité du README", 'readme', onchange),
|
||||||
this.voteFor(team, "Qualité des diagrammes", 'diagrams', onchange),
|
this.voteFor(ballot, "Qualité des diagrammes", 'diagrams', onchange),
|
||||||
this.voteFor(team, "Êtes-vous convaincu?", 'convinced', onchange)]
|
this.voteFor(ballot, "Êtes-vous convaincu?", 'convinced', onchange)]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -91,18 +91,18 @@ class Submit{
|
|||||||
}
|
}
|
||||||
|
|
||||||
type MainVoteViewAttr = {
|
type MainVoteViewAttr = {
|
||||||
teamId: number,
|
ballotId: number,
|
||||||
}
|
}
|
||||||
|
|
||||||
class MainVoteView implements m.ClassComponent<MainVoteViewAttr>{
|
class MainVoteView implements m.ClassComponent<MainVoteViewAttr>{
|
||||||
|
|
||||||
async persistTeams(): Promise<void>{
|
async persistBallots(): Promise<void>{
|
||||||
const params: RequestInit = {
|
const params: RequestInit = {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
headers: {
|
headers: {
|
||||||
"Content-Type": "application/json",
|
"Content-Type": "application/json",
|
||||||
},
|
},
|
||||||
body: JSON.stringify(teams)
|
body: JSON.stringify(ballots)
|
||||||
};
|
};
|
||||||
const resp = await fetch("/api/v1/vote/kkk", params);
|
const resp = await fetch("/api/v1/vote/kkk", params);
|
||||||
console.log(resp.status)
|
console.log(resp.status)
|
||||||
@@ -113,31 +113,31 @@ class MainVoteView implements m.ClassComponent<MainVoteViewAttr>{
|
|||||||
return m("div.columns.is-centered",
|
return m("div.columns.is-centered",
|
||||||
m("div.column.is-two-thirds",
|
m("div.column.is-two-thirds",
|
||||||
m("h1.title.is-1", m(m.route.Link, {href: "/"}, `Votons - ${name}`)),
|
m("h1.title.is-1", m(m.route.Link, {href: "/"}, `Votons - ${name}`)),
|
||||||
m(new VoteView(), {teamId: vnode.attrs.teamId, onchange: this.persistTeams}),
|
m(new BallotView(), {ballotId: vnode.attrs.ballotId, onchange: this.persistBallots}),
|
||||||
m(new Submit())));
|
m(new Submit())));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class MainView{
|
class MainView{
|
||||||
createTeamRow(team: Team){
|
createBallotRow(ballot: Ballot){
|
||||||
return m('tr', [
|
return m('tr', [
|
||||||
m('td', m(m.route.Link, {href: `${m.route.get()}vote/${team.id}/`}, team.name)),
|
m('td', m(m.route.Link, {href: `${m.route.get()}vote/${ballot.id}/`}, ballot.name)),
|
||||||
m('td', team.scores.readme),
|
m('td', ballot.scores.readme),
|
||||||
m('td', team.scores.diagrams),
|
m('td', ballot.scores.diagrams),
|
||||||
m('td', team.scores.convinced),
|
m('td', ballot.scores.convinced),
|
||||||
])
|
])
|
||||||
}
|
}
|
||||||
|
|
||||||
votesTable(teams: Team[]){
|
votesTable(ballots: Ballot[]){
|
||||||
return m('table.table.is-striped.is-fullwidth',
|
return m('table.table.is-striped.is-fullwidth',
|
||||||
m('thead',
|
m('thead',
|
||||||
m('tr', ['Équipe', 'Readme', 'Diagrammes', 'Idée'].map(title => m('th', title)))),
|
m('tr', ['Équipe', 'Readme', 'Diagrammes', 'Idée'].map(title => m('th', title)))),
|
||||||
m('tbody', teams.map(team => this.createTeamRow(team))))
|
m('tbody', ballots.map(ballot => this.createBallotRow(ballot))))
|
||||||
}
|
}
|
||||||
view(){
|
view(){
|
||||||
return m("div.columns.is-centered",
|
return m("div.columns.is-centered",
|
||||||
m("div.column.is-two-thirds",
|
m("div.column.is-two-thirds",
|
||||||
this.votesTable(teams)))
|
this.votesTable(ballots)))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -165,5 +165,5 @@ class PasscodeView{
|
|||||||
m.route(window.document.body, "/", {
|
m.route(window.document.body, "/", {
|
||||||
"/": PasscodeView,
|
"/": PasscodeView,
|
||||||
"/voter-home/": MainView,
|
"/voter-home/": MainView,
|
||||||
"/voter-home/vote/:teamId/": MainVoteView
|
"/voter-home/vote/:ballotId/": MainVoteView
|
||||||
})
|
})
|
||||||
+4
-4
@@ -1,7 +1,7 @@
|
|||||||
import { Application, Router } from "https://deno.land/x/oak/mod.ts";
|
import { Application, Router } from "https://deno.land/x/oak/mod.ts";
|
||||||
import {createBundle} from "./bundler.ts";
|
import {createBundle} from "./bundler.ts";
|
||||||
import {Context} from "https://deno.land/x/oak@v12.2.0/context.ts";
|
import {Context} from "https://deno.land/x/oak@v12.2.0/context.ts";
|
||||||
import {Team} from "./shared.ts";
|
import {Ballot} from "./shared.ts";
|
||||||
|
|
||||||
const router = new Router();
|
const router = new Router();
|
||||||
|
|
||||||
@@ -40,7 +40,7 @@ function getUserIndex(userId: string, ballotId: string, questionId: string){
|
|||||||
questionId]
|
questionId]
|
||||||
}
|
}
|
||||||
|
|
||||||
async function persistTeams(userId: any, ballots: Team[]): Promise<void> {
|
async function persistBallots(userId: any, ballots: Ballot[]): Promise<void> {
|
||||||
const kv = await Deno.openKv();
|
const kv = await Deno.openKv();
|
||||||
for(const ballot of ballots){
|
for(const ballot of ballots){
|
||||||
for(const question of Object.keys(ballot.scores)){
|
for(const question of Object.keys(ballot.scores)){
|
||||||
@@ -58,8 +58,8 @@ router.post("/api/v1/vote/:userid", async (ctx: Context)=> {
|
|||||||
ctx.throw(415);
|
ctx.throw(415);
|
||||||
}
|
}
|
||||||
const body = await ctx.request.body().value;
|
const body = await ctx.request.body().value;
|
||||||
const teams = body as Team[];
|
const ballots = body as Ballot[];
|
||||||
await persistTeams(userId, teams);
|
await persistBallots(userId, ballots);
|
||||||
console.log(body);
|
console.log(body);
|
||||||
ctx.response.body = userId;
|
ctx.response.body = userId;
|
||||||
|
|
||||||
|
|||||||
+1
-1
@@ -4,7 +4,7 @@ export type Scores = {
|
|||||||
convinced: number,
|
convinced: number,
|
||||||
};
|
};
|
||||||
|
|
||||||
export type Team = {
|
export type Ballot = {
|
||||||
id: number,
|
id: number,
|
||||||
name: string,
|
name: string,
|
||||||
scores: Scores,
|
scores: Scores,
|
||||||
|
|||||||
Reference in new issue
Block a user