Add page to create an election
This commit is contained in:
@ -87,7 +87,7 @@ func (app *application) CreateElection(w http.ResponseWriter, r *http.Request) {
|
|||||||
|
|
||||||
var res []byte
|
var res []byte
|
||||||
if request.AreVotersKnown {
|
if request.AreVotersKnown {
|
||||||
voterIdentities := make([]string, 0, request.MaxVoters)
|
voterIdentities := make([]string, 0, request.MaxVoters) // TODO: this is way too slow
|
||||||
for i := 0; i < request.MaxVoters; i++ {
|
for i := 0; i < request.MaxVoters; i++ {
|
||||||
randomIdentity := randomVoterIdentity()
|
randomIdentity := randomVoterIdentity()
|
||||||
_, err := app.voters.Insert(randomIdentity, electionId)
|
_, err := app.voters.Insert(randomIdentity, electionId)
|
||||||
|
@ -48,7 +48,7 @@ func main() {
|
|||||||
Addr: addr,
|
Addr: addr,
|
||||||
Handler: app.routes(),
|
Handler: app.routes(),
|
||||||
ErrorLog: slog.NewLogLogger(logger.Handler(), slog.LevelError),
|
ErrorLog: slog.NewLogLogger(logger.Handler(), slog.LevelError),
|
||||||
IdleTimeout: time.Minute,
|
IdleTimeout: 6 * time.Minute,
|
||||||
ReadTimeout: 5 * time.Second,
|
ReadTimeout: 5 * time.Second,
|
||||||
WriteTimeout: 10 * time.Second,
|
WriteTimeout: 10 * time.Second,
|
||||||
}
|
}
|
||||||
|
@ -133,107 +133,6 @@ paths:
|
|||||||
|
|
||||||
components:
|
components:
|
||||||
schemas:
|
schemas:
|
||||||
Election:
|
|
||||||
type: object
|
|
||||||
required:
|
|
||||||
- id
|
|
||||||
- name
|
|
||||||
- tokens
|
|
||||||
- areVotersKnown
|
|
||||||
- maxVoters
|
|
||||||
- expiresAt
|
|
||||||
properties:
|
|
||||||
id:
|
|
||||||
type: integer
|
|
||||||
format: int64
|
|
||||||
readOnly: true
|
|
||||||
name:
|
|
||||||
type: string
|
|
||||||
minLength: 1
|
|
||||||
tokens:
|
|
||||||
type: integer
|
|
||||||
minimum: 1
|
|
||||||
areVotersKnown:
|
|
||||||
type: boolean
|
|
||||||
maxVoters:
|
|
||||||
type: integer
|
|
||||||
minimum: 0
|
|
||||||
description: Must be greater than 0 when voters are known
|
|
||||||
createdAt:
|
|
||||||
type: string
|
|
||||||
format: date-time
|
|
||||||
readOnly: true
|
|
||||||
expiresAt:
|
|
||||||
type: string
|
|
||||||
format: date-time
|
|
||||||
choices:
|
|
||||||
type: array
|
|
||||||
items:
|
|
||||||
$ref: '#/components/schemas/Choice'
|
|
||||||
voters:
|
|
||||||
type: array
|
|
||||||
items:
|
|
||||||
$ref: '#/components/schemas/Voter'
|
|
||||||
|
|
||||||
Choice:
|
|
||||||
type: object
|
|
||||||
required:
|
|
||||||
- text
|
|
||||||
- electionId
|
|
||||||
properties:
|
|
||||||
text:
|
|
||||||
type: string
|
|
||||||
minLength: 1
|
|
||||||
electionId:
|
|
||||||
type: integer
|
|
||||||
format: int64
|
|
||||||
|
|
||||||
Voter:
|
|
||||||
type: object
|
|
||||||
required:
|
|
||||||
- identity
|
|
||||||
- electionId
|
|
||||||
properties:
|
|
||||||
identity:
|
|
||||||
type: string
|
|
||||||
minLength: 1
|
|
||||||
description: When voters are known, passcodes will be pre-generated
|
|
||||||
electionId:
|
|
||||||
type: integer
|
|
||||||
format: int64
|
|
||||||
votes:
|
|
||||||
type: array
|
|
||||||
items:
|
|
||||||
$ref: '#/components/schemas/Vote'
|
|
||||||
|
|
||||||
Vote:
|
|
||||||
type: object
|
|
||||||
required:
|
|
||||||
- voterIdentity
|
|
||||||
- electionId
|
|
||||||
properties:
|
|
||||||
voterIdentity:
|
|
||||||
type: string
|
|
||||||
minLength: 1
|
|
||||||
electionId:
|
|
||||||
type: integer
|
|
||||||
format: int64
|
|
||||||
choiceText:
|
|
||||||
type: string
|
|
||||||
nullable: true
|
|
||||||
tokens:
|
|
||||||
type: integer
|
|
||||||
minimum: 1
|
|
||||||
nullable: true
|
|
||||||
calculatedVoteCount:
|
|
||||||
type: integer
|
|
||||||
readOnly: true
|
|
||||||
description: Calculated as floor(sqrt(tokens))
|
|
||||||
createdAt:
|
|
||||||
type: string
|
|
||||||
format: date-time
|
|
||||||
readOnly: true
|
|
||||||
|
|
||||||
CreateElectionRequest:
|
CreateElectionRequest:
|
||||||
type: object
|
type: object
|
||||||
required:
|
required:
|
||||||
|
@ -4,63 +4,152 @@
|
|||||||
<meta charset="UTF-8">
|
<meta charset="UTF-8">
|
||||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||||
<title>Create New Election</title>
|
<title>Create New Election</title>
|
||||||
<link rel="stylesheet" href="/static/styles.css">
|
<link rel="stylesheet" href="/static/css/styles.css">
|
||||||
|
<script src="https://unpkg.com/vue@3"></script>
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
<div class="container">
|
<div id="app" class="container">
|
||||||
<main>
|
<main>
|
||||||
<h1>Create New Election</h1>
|
<h1>Create New Election</h1>
|
||||||
<form action="/elections" method="POST" class="form">
|
<form @submit.prevent="createElection" class="form">
|
||||||
<div class="form-group">
|
<div class="form-group">
|
||||||
<label for="name">Election Name</label>
|
<label for="name">Election Name</label>
|
||||||
<input type="text" id="name" name="name" required>
|
<input type="text" id="name" v-model="election.name" required>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div class="form-row">
|
<div class="form-row">
|
||||||
<div class="form-group">
|
<div class="form-group">
|
||||||
<label for="tokens">Tokens per Voter</label>
|
<label for="tokens">Tokens per Voter</label>
|
||||||
<input type="number" id="tokens" name="tokens" min="1" required>
|
<input type="number" id="tokens" v-model.number="election.tokens" min="1" required>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div class="form-group">
|
<div class="form-group">
|
||||||
<label for="are_voters_known">Voter Access</label>
|
<label for="areVotersKnown">Voter Access</label>
|
||||||
<select id="are_voters_known" name="are_voters_known" required>
|
<select id="areVotersKnown" v-model="election.areVotersKnown" required>
|
||||||
<option value="1">Known voters only</option>
|
<option :value="true">Known voters only</option>
|
||||||
<option value="0">Open to anyone</option>
|
<option :value="false">Open to anyone</option>
|
||||||
</select>
|
</select>
|
||||||
|
<small>Known voters only = codes will be generated and you must give those to your voters</small>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div class="form-group" id="max-voters-group">
|
|
||||||
<label for="max_voters">Maximum Number of Voters</label>
|
|
||||||
<input type="number" id="max_voters" name="max_voters" min="1">
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<div class="form-group">
|
<div class="form-group">
|
||||||
<label for="expires_at">Expiration Date</label>
|
<label for="maxVoters">Maximum Number of Voters</label>
|
||||||
<input type="datetime-local" id="expires_at" name="expires_at" required>
|
<input type="number" id="maxVoters" v-model.number="election.maxVoters" min="1">
|
||||||
|
<small>0 = unlimited</small>
|
||||||
|
<span v-if="election.areVotersKnown && election.maxVoters <= 0" class="error-text">Maximum number of voters must be greater than 0 if voters are known</span>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="form-group">
|
||||||
|
<label for="expiresAt">Expiration Date</label>
|
||||||
|
<input type="datetime-local" id="expiresAt" v-model="election.expiresAt" required>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div class="form-group">
|
<div class="form-group">
|
||||||
<label>Choices</label>
|
<label>Choices</label>
|
||||||
<div id="choices-container">
|
<div id="choices-container">
|
||||||
<div class="choice-input">
|
<div v-for="(choice, index) in election.choices" :key="index" class="choice-input">
|
||||||
<input type="text" name="choices[]" required>
|
<input type="text" v-model="election.choices[index]" required>
|
||||||
<button type="button" class="remove-choice" hidden>×</button>
|
<button type="button" class="remove-choice" @click="removeChoice(index)" v-show="election.choices.length > 2">×</button>
|
||||||
</div>
|
|
||||||
<div class="choice-input">
|
|
||||||
<input type="text" name="choices[]" required>
|
|
||||||
<button type="button" class="remove-choice" hidden>×</button>
|
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<button type="button" id="add-choice">Add Another Choice</button>
|
<button type="button" id="add-choice" @click="addChoice">Add Another Choice</button>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div class="form-actions">
|
<div class="form-actions">
|
||||||
<button type="submit">Create Election</button>
|
<button type="submit">Create Election</button>
|
||||||
</div>
|
</div>
|
||||||
</form>
|
</form>
|
||||||
|
|
||||||
|
<div v-if="voterIdentities.length > 0" class="voter-codes">
|
||||||
|
<h2>Voter Access Codes</h2>
|
||||||
|
<div class="codes-container">
|
||||||
|
<button @click="copyAllCodes" class="copy-all-btn">
|
||||||
|
Copy All Codes
|
||||||
|
</button>
|
||||||
|
<div class="codes-list">
|
||||||
|
<div v-for="(code, index) in voterIdentities"
|
||||||
|
:key="code"
|
||||||
|
class="code-item">
|
||||||
|
<span class="code-number">{{ index + 1 }}.</span>
|
||||||
|
<span class="code-text">{{ code }}</span>
|
||||||
|
<button @click="copyCode(code)" class="copy-btn">
|
||||||
|
Copy
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
</main>
|
</main>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
<script>
|
||||||
|
const app = Vue.createApp({
|
||||||
|
data() {
|
||||||
|
return {
|
||||||
|
election: {
|
||||||
|
name: "",
|
||||||
|
tokens: 100,
|
||||||
|
areVotersKnown: true,
|
||||||
|
maxVoters: 0,
|
||||||
|
expiresAt: "",
|
||||||
|
choices: ["", ""] // Start with two empty choices
|
||||||
|
},
|
||||||
|
voterIdentities: []
|
||||||
|
};
|
||||||
|
},
|
||||||
|
methods: {
|
||||||
|
addChoice() {
|
||||||
|
this.election.choices.push(""); // Add a new empty choice
|
||||||
|
},
|
||||||
|
removeChoice(index) {
|
||||||
|
this.election.choices.splice(index, 1); // Remove choice by index
|
||||||
|
},
|
||||||
|
async copyCode(code) {
|
||||||
|
try {
|
||||||
|
await navigator.clipboard.writeText(code);
|
||||||
|
// Optional: Add visual feedback that copy succeeded
|
||||||
|
} catch (err) {
|
||||||
|
console.error('Failed to copy code:', err);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
async copyAllCodes() {
|
||||||
|
try {
|
||||||
|
const allCodes = this.voterIdentities.join('\n');
|
||||||
|
await navigator.clipboard.writeText(allCodes);
|
||||||
|
// Optional: Add visual feedback that copy succeeded
|
||||||
|
} catch (err) {
|
||||||
|
console.error('Failed to copy codes:', err);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
createElection() {
|
||||||
|
this.voterIdentities = [];
|
||||||
|
|
||||||
|
const payload = {
|
||||||
|
...this.election,
|
||||||
|
expiresAt: this.election.expiresAt + ":00Z", // Add timezone if necessary
|
||||||
|
choices: this.election.choices.filter(choice => choice.trim() !== "") // Filter out empty choices
|
||||||
|
};
|
||||||
|
|
||||||
|
fetch("/election", {
|
||||||
|
method: "POST",
|
||||||
|
headers: {
|
||||||
|
"Content-Type": "application/json"
|
||||||
|
},
|
||||||
|
body: JSON.stringify(payload)
|
||||||
|
})
|
||||||
|
.then(response => response.json())
|
||||||
|
.then(data => {
|
||||||
|
this.voterIdentities = data.voterIdentities;
|
||||||
|
})
|
||||||
|
.catch(error => {
|
||||||
|
alert("Failed to create election.");
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
app.mount("#app");
|
||||||
|
</script>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
|
@ -98,6 +98,11 @@ input:focus, select:focus {
|
|||||||
flex: 1;
|
flex: 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
.error-text {
|
||||||
|
color: red;
|
||||||
|
font-size: 12px;
|
||||||
|
}
|
||||||
|
|
||||||
button {
|
button {
|
||||||
background: var(--primary-color);
|
background: var(--primary-color);
|
||||||
color: white;
|
color: white;
|
1
ui/static/js/htmx.min.js
vendored
Normal file
1
ui/static/js/htmx.min.js
vendored
Normal file
File diff suppressed because one or more lines are too long
120
ui/static/js/json-enc-custom.js
Normal file
120
ui/static/js/json-enc-custom.js
Normal file
@ -0,0 +1,120 @@
|
|||||||
|
htmx.defineExtension('json-enc-custom', {
|
||||||
|
onEvent: function (name, evt) {
|
||||||
|
if (name === "htmx:configRequest") {
|
||||||
|
evt.detail.headers['Content-Type'] = "application/json";
|
||||||
|
}
|
||||||
|
},
|
||||||
|
encodeParameters: function (xhr, parameters, elt) {
|
||||||
|
xhr.overrideMimeType('text/json');
|
||||||
|
let encoded_parameters = encodingAlgorithm(parameters);
|
||||||
|
return encoded_parameters;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
function encodingAlgorithm(parameters) {
|
||||||
|
let resultingObject = Object.create(null);
|
||||||
|
const PARAM_NAMES = Object.keys(parameters);
|
||||||
|
const PARAM_VALUES = Object.values(parameters);
|
||||||
|
const PARAM_LENGHT = PARAM_NAMES.length;
|
||||||
|
|
||||||
|
for (let param_index = 0; param_index < PARAM_LENGHT; param_index++) {
|
||||||
|
let name = PARAM_NAMES[param_index];
|
||||||
|
let value = PARAM_VALUES[param_index];
|
||||||
|
let steps = JSONEncodingPath(name);
|
||||||
|
let context = resultingObject;
|
||||||
|
|
||||||
|
for (let step_index = 0; step_index < steps.length; step_index++) {
|
||||||
|
let step = steps[step_index];
|
||||||
|
context = setValueFromPath(context, step, value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let result = JSON.stringify(resultingObject);
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
|
function JSONEncodingPath(name) {
|
||||||
|
let path = name;
|
||||||
|
let original = path;
|
||||||
|
const FAILURE = [{ "type": "object", "key": original, "last": true, "next_type": null }];
|
||||||
|
let steps = Array();
|
||||||
|
let first_key = String();
|
||||||
|
for (let i = 0; i < path.length; i++) {
|
||||||
|
if (path[i] !== "[") first_key += path[i];
|
||||||
|
else break;
|
||||||
|
}
|
||||||
|
if (first_key === "") return FAILURE;
|
||||||
|
path = path.slice(first_key.length);
|
||||||
|
steps.push({ "type": "object", "key": first_key, "last": false, "next_type": null });
|
||||||
|
while (path.length) {
|
||||||
|
// [123...]
|
||||||
|
if (/^\[\d+\]/.test(path)) {
|
||||||
|
path = path.slice(1);
|
||||||
|
let collected_digits = path.match(/\d+/)[0]
|
||||||
|
path = path.slice(collected_digits.length);
|
||||||
|
let numeric_key = parseInt(collected_digits, 10);
|
||||||
|
path = path.slice(1);
|
||||||
|
steps.push({ "type": "array", "key": numeric_key, "last": false, "next_type": null });
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
// [abc...]
|
||||||
|
if (/^\[[^\]]+\]/.test(path)) {
|
||||||
|
path = path.slice(1);
|
||||||
|
let collected_characters = path.match(/[^\]]+/)[0];
|
||||||
|
path = path.slice(collected_characters.length);
|
||||||
|
let object_key = collected_characters;
|
||||||
|
path = path.slice(1);
|
||||||
|
steps.push({ "type": "object", "key": object_key, "last": false, "next_type": null });
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
return FAILURE;
|
||||||
|
}
|
||||||
|
for (let step_index = 0; step_index < steps.length; step_index++) {
|
||||||
|
if (step_index === steps.length - 1) {
|
||||||
|
let tmp_step = steps[step_index];
|
||||||
|
tmp_step["last"] = true;
|
||||||
|
steps[step_index] = tmp_step;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
let tmp_step = steps[step_index];
|
||||||
|
tmp_step["next_type"] = steps[step_index + 1]["type"];
|
||||||
|
steps[step_index] = tmp_step;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return steps;
|
||||||
|
}
|
||||||
|
|
||||||
|
function setValueFromPath(context, step, value) {
|
||||||
|
if (step.last) {
|
||||||
|
context[step.key] = value;
|
||||||
|
}
|
||||||
|
|
||||||
|
//TODO: make merge functionality and file suport.
|
||||||
|
|
||||||
|
//check if the context value already exists
|
||||||
|
if (context[step.key] === undefined) {
|
||||||
|
if (step.type === "object") {
|
||||||
|
if (step.next_type === "object") {
|
||||||
|
context[step.key] = {};
|
||||||
|
return context[step.key];
|
||||||
|
}
|
||||||
|
if (step.next_type === "array") {
|
||||||
|
context[step.key] = [];
|
||||||
|
return context[step.key];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (step.type === "array") {
|
||||||
|
if (step.next_type === "object") {
|
||||||
|
context[step.key] = {};
|
||||||
|
return context[step.key];
|
||||||
|
}
|
||||||
|
if (step.next_type === "array") {
|
||||||
|
context[step.key] = [];
|
||||||
|
return context[step.key];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
return context[step.key];
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user