Add create-election.html

This commit is contained in:
2025-01-13 10:56:36 +01:00
parent 9cc85b9a47
commit 4f50aca3b6
5 changed files with 251 additions and 0 deletions

View File

@ -4,6 +4,7 @@ import (
api "code.dlmw.ch/dlmw/qv/internal" api "code.dlmw.ch/dlmw/qv/internal"
"code.dlmw.ch/dlmw/qv/internal/models" "code.dlmw.ch/dlmw/qv/internal/models"
"code.dlmw.ch/dlmw/qv/internal/validator" "code.dlmw.ch/dlmw/qv/internal/validator"
"code.dlmw.ch/dlmw/qv/ui"
"database/sql" "database/sql"
"encoding/json" "encoding/json"
"errors" "errors"
@ -15,6 +16,17 @@ import (
"time" "time"
) )
func (app *application) createElectionPage(w http.ResponseWriter, r *http.Request) {
content, err := ui.Files.ReadFile("create-election.html")
if err != nil {
app.serverError(w, r, fmt.Errorf("couldn't read create-election.html"))
return
}
w.Header().Set("Content-Type", "text/html")
w.Write(content)
}
type createElectionRequestWithValidator struct { type createElectionRequestWithValidator struct {
api.CreateElectionRequest api.CreateElectionRequest
validator.Validator validator.Validator

View File

@ -2,6 +2,7 @@ package main
import ( import (
"code.dlmw.ch/dlmw/qv/internal/models" "code.dlmw.ch/dlmw/qv/internal/models"
"code.dlmw.ch/dlmw/qv/ui"
"github.com/justinas/alice" "github.com/justinas/alice"
"log/slog" "log/slog"
"net/http" "net/http"
@ -17,6 +18,9 @@ type application struct {
func (app *application) routes() http.Handler { func (app *application) routes() http.Handler {
mux := http.NewServeMux() mux := http.NewServeMux()
mux.Handle("GET /static/", http.FileServerFS(ui.Files))
mux.HandleFunc("GET /election/create", app.createElectionPage)
mux.HandleFunc("POST /election", app.createElection) mux.HandleFunc("POST /election", app.createElection)
mux.HandleFunc("POST /election/{id}/votes", app.createVotes) mux.HandleFunc("POST /election/{id}/votes", app.createVotes)

66
ui/create-election.html Normal file
View File

@ -0,0 +1,66 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Create New Election</title>
<link rel="stylesheet" href="/static/styles.css">
</head>
<body>
<div class="container">
<main>
<h1>Create New Election</h1>
<form action="/elections" method="POST" class="form">
<div class="form-group">
<label for="name">Election Name</label>
<input type="text" id="name" name="name" required>
</div>
<div class="form-row">
<div class="form-group">
<label for="tokens">Tokens per Voter</label>
<input type="number" id="tokens" name="tokens" min="1" required>
</div>
<div class="form-group">
<label for="are_voters_known">Voter Access</label>
<select id="are_voters_known" name="are_voters_known" required>
<option value="1">Known voters only</option>
<option value="0">Open to anyone</option>
</select>
</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">
<label for="expires_at">Expiration Date</label>
<input type="datetime-local" id="expires_at" name="expires_at" required>
</div>
<div class="form-group">
<label>Choices</label>
<div id="choices-container">
<div class="choice-input">
<input type="text" name="choices[]" required>
<button type="button" class="remove-choice" hidden>×</button>
</div>
<div class="choice-input">
<input type="text" name="choices[]" required>
<button type="button" class="remove-choice" hidden>×</button>
</div>
</div>
<button type="button" id="add-choice">Add Another Choice</button>
</div>
<div class="form-actions">
<button type="submit">Create Election</button>
</div>
</form>
</main>
</div>
</body>
</html>

163
ui/static/styles.css Normal file
View File

@ -0,0 +1,163 @@
:root {
--primary-color: #2563eb;
--error-color: #dc2626;
--background-color: #f8fafc;
--border-color: #e2e8f0;
--text-color: #1e293b;
}
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
line-height: 1.6;
color: var(--text-color);
background: var(--background-color);
min-height: 100vh;
}
.container {
display: flex;
justify-content: center;
align-items: flex-start;
min-height: 100vh;
padding: 2rem;
}
main {
width: 100%;
max-width: 600px;
background: white;
border-radius: 8px;
box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1);
padding: 2rem;
}
.form {
display: flex;
flex-direction: column;
gap: 1.5rem;
}
.form-row {
display: flex;
gap: 1rem;
flex-wrap: wrap;
}
.form-row .form-group {
flex: 1 1 200px;
min-width: 0;
}
.form-group {
display: flex;
flex-direction: column;
gap: 0.5rem;
}
h1 {
margin-bottom: 2rem;
color: var(--text-color);
font-size: 1.5rem;
}
label {
font-weight: 500;
}
input, select {
padding: 0.5rem;
border: 1px solid var(--border-color);
border-radius: 4px;
font-size: 1rem;
}
input:focus, select:focus {
outline: none;
border-color: var(--primary-color);
box-shadow: 0 0 0 2px rgba(37, 99, 235, 0.1);
}
#choices-container {
display: flex;
flex-direction: column;
gap: 0.5rem;
}
.choice-input {
display: flex;
gap: 0.5rem;
}
.choice-input input {
flex: 1;
}
button {
background: var(--primary-color);
color: white;
border: none;
padding: 0.75rem 1.5rem;
border-radius: 4px;
cursor: pointer;
font-size: 1rem;
transition: background-color 0.2s;
}
button:hover {
background-color: #1d4ed8;
}
#add-choice {
background: transparent;
color: var(--primary-color);
border: 1px solid var(--primary-color);
margin-top: 0.5rem;
}
#add-choice:hover {
background: rgba(37, 99, 235, 0.1);
}
.remove-choice {
padding: 0.5rem 1rem;
background: transparent;
color: var(--error-color);
border: 1px solid var(--error-color);
flex-shrink: 0;
}
.remove-choice:hover {
background: rgba(220, 38, 38, 0.1);
}
.form-actions {
display: flex;
justify-content: flex-end;
margin-top: 1rem;
}
@media (max-width: 640px) {
.container {
padding: 0;
}
main {
border-radius: 0;
box-shadow: none;
padding: 1rem;
}
.form-actions {
flex-direction: column;
}
button {
width: 100%;
}
}

6
ui/ui.go Normal file
View File

@ -0,0 +1,6 @@
package ui
import "embed"
//go:embed *.html static
var Files embed.FS