Convert to Alpine.js frontend

This commit is contained in:
2025-01-14 12:35:32 +01:00
parent f22710464b
commit 88790bdec2

View File

@ -5,27 +5,27 @@
<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/css/styles.css"> <link rel="stylesheet" href="/static/css/styles.css">
<script src="https://unpkg.com/vue@3"></script> <script src="https://unpkg.com/alpinejs@3" defer></script>
</head> </head>
<body> <body>
<div id="app" class="container"> <div x-data="electionForm" class="container">
<main> <main>
<h1>Create New Election</h1> <h1>Create New Election</h1>
<form @submit.prevent="createElection" 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" v-model="election.name" required> <input type="text" id="name" x-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" v-model.number="election.tokens" min="1" required> <input type="number" id="tokens" x-model.number="election.tokens" min="1" required>
</div> </div>
<div class="form-group"> <div class="form-group">
<label for="areVotersKnown">Voter Access</label> <label for="areVotersKnown">Voter Access</label>
<select id="areVotersKnown" v-model="election.areVotersKnown" required> <select id="areVotersKnown" x-model="election.areVotersKnown" required>
<option :value="true">Known voters only</option> <option :value="true">Known voters only</option>
<option :value="false">Open to anyone</option> <option :value="false">Open to anyone</option>
</select> </select>
@ -35,23 +35,27 @@
<div class="form-group"> <div class="form-group">
<label for="maxVoters">Maximum Number of Voters</label> <label for="maxVoters">Maximum Number of Voters</label>
<input type="number" id="maxVoters" v-model.number="election.maxVoters" min="1"> <input type="number" id="maxVoters" x-model.number="election.maxVoters" min="1">
<small>0 = unlimited</small> <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> <template x-if="election.areVotersKnown && election.maxVoters <= 0">
<span class="error-text">Maximum number of voters must be greater than 0 if voters are known</span>
</template>
</div> </div>
<div class="form-group"> <div class="form-group">
<label for="expiresAt">Expiration Date</label> <label for="expiresAt">Expiration Date</label>
<input type="datetime-local" id="expiresAt" v-model="election.expiresAt" required> <input type="datetime-local" id="expiresAt" x-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 v-for="(choice, index) in election.choices" :key="index" class="choice-input"> <template x-for="(choice, index) in election.choices" :key="index">
<input type="text" v-model="election.choices[index]" required> <div class="choice-input">
<button type="button" class="remove-choice" @click="removeChoice(index)" v-show="election.choices.length > 2">×</button> <input type="text" x-model="election.choices[index]" required>
</div> <button type="button" class="remove-choice" @click="removeChoice(index)" x-show="election.choices.length > 2">×</button>
</div>
</template>
</div> </div>
<button type="button" id="add-choice" @click="addChoice">Add Another Choice</button> <button type="button" id="add-choice" @click="addChoice">Add Another Choice</button>
</div> </div>
@ -61,79 +65,82 @@
</div> </div>
</form> </form>
<div v-if="createdElectionId > 0" class="election-info"> <template x-if="createdElectionId > 0">
<h2>Election Created Successfully</h2> <div class="election-info">
<div class="info-container"> <h2>Election Created Successfully</h2>
<span class="info-label">Election ID:</span> <div class="info-container">
<span class="info-value">{{ createdElectionId }}</span> <span class="info-label">Election ID:</span>
<button @click="copyElectionId" class="copy-btn"> <span class="info-value" x-text="createdElectionId"></span>
Copy <button @click="copyElectionId" class="copy-btn">
</button> Copy
</button>
</div>
</div> </div>
</div> </template>
<div v-if="voterIdentities.length > 0" class="voter-codes"> <template x-if="voterIdentities.length > 0">
<h2>Voter Access Codes</h2> <div class="voter-codes">
<div class="codes-container"> <h2>Voter Access Codes</h2>
<button @click="copyAllCodes" class="copy-all-btn"> <div class="codes-container">
Copy All Codes <button @click="copyAllCodes" class="copy-all-btn">
</button> Copy All Codes
<div class="codes-list"> </button>
<div v-for="(code, index) in voterIdentities" <div class="codes-list">
:key="code" <template x-for="(code, index) in voterIdentities" :key="code">
class="code-item"> <div class="code-item">
<span class="code-number">{{ index + 1 }}.</span> <span class="code-number" x-text="index + 1"></span>.
<span class="code-text">{{ code }}</span> <span class="code-text" x-text="code"></span>
<button @click="copyCode(code)" class="copy-btn"> <button @click="copyCode(code)" class="copy-btn">
Copy Copy
</button> </button>
</div>
</template>
</div> </div>
</div> </div>
</div> </div>
</div> </template>
</main> </main>
</div> </div>
<script> <script>
const app = Vue.createApp({ document.addEventListener('alpine:init', () => {
data() { Alpine.data('electionForm', () => ({
return { election: {
election: { name: "",
name: "", tokens: 100,
tokens: 100, areVotersKnown: true,
areVotersKnown: true, maxVoters: 0,
maxVoters: 0, expiresAt: "",
expiresAt: "", choices: ["", ""] // Start with two empty choices
choices: ["", ""] // Start with two empty choices },
}, createdElectionId: 0,
createdElectionId: 0, voterIdentities: [],
voterIdentities: []
};
},
methods: {
addChoice() { addChoice() {
this.election.choices.push(""); // Add a new empty choice this.election.choices.push(""); // Add a new empty choice
}, },
removeChoice(index) { removeChoice(index) {
this.election.choices.splice(index, 1); // Remove choice by index this.election.choices.splice(index, 1); // Remove choice by index
}, },
async copyCode(code) { async copyCode(code) {
try { try {
await navigator.clipboard.writeText(code); await navigator.clipboard.writeText(code);
// Optional: Add visual feedback that copy succeeded
} catch (err) { } catch (err) {
console.error('Failed to copy code:', err); console.error('Failed to copy code:', err);
} }
}, },
async copyAllCodes() { async copyAllCodes() {
try { try {
const allCodes = this.voterIdentities.join('\n'); const allCodes = this.voterIdentities.join('\n');
await navigator.clipboard.writeText(allCodes); await navigator.clipboard.writeText(allCodes);
// Optional: Add visual feedback that copy succeeded
} catch (err) { } catch (err) {
console.error('Failed to copy codes:', err); console.error('Failed to copy codes:', err);
} }
}, },
createElection() { createElection() {
this.voterIdentities = []; this.voterIdentities = [];
@ -153,7 +160,6 @@
.then(response => { .then(response => {
const locationHeader = response.headers.get('Location'); const locationHeader = response.headers.get('Location');
this.createdElectionId = locationHeader.replace('/election/', ''); this.createdElectionId = locationHeader.replace('/election/', '');
return response.json(); return response.json();
}) })
.then(data => { .then(data => {
@ -163,10 +169,8 @@
alert("Failed to create election."); alert("Failed to create election.");
}); });
} }
} }));
}); });
app.mount("#app");
</script> </script>
</body> </body>
</html> </html>