Add voterIdentities to copy again and fix issue if there was no body in the response (in the case of unknown voters election)
This commit is contained in:
@ -96,6 +96,28 @@
|
||||
<p class="mt-2">Election ID: <span class="font-mono" x-text="createdElectionId"></span></p>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<template x-if="voterIdentities.length > 0">
|
||||
<div class="voter-codes">
|
||||
<h2 class="text-2xl font-semibold mb-4">Voter Access Codes</h2>
|
||||
<div class="codes-container">
|
||||
<button @click="copyAllCodes" class="copy-all-btn bg-indigo-600 text-white py-2 px-4 rounded-md font-medium hover:bg-indigo-700 focus:outline-none focus:ring-2 focus:ring-indigo-500 mb-4">
|
||||
Copy All Codes
|
||||
</button>
|
||||
<div class="codes-list">
|
||||
<template x-for="(code, index) in voterIdentities" :key="index">
|
||||
<div class="code-item flex items-center space-x-3 mb-2">
|
||||
<span class="code-number text-gray-700 font-medium" x-text="index + 1"></span>.
|
||||
<span class="code-text text-gray-900" x-text="code"></span>
|
||||
<button @click="copyCode(code)" class="copy-btn bg-indigo-600 text-white text-sm py-1 px-3 rounded-md hover:bg-indigo-700 focus:outline-none focus:ring-2 focus:ring-indigo-500">
|
||||
Copy
|
||||
</button>
|
||||
</div>
|
||||
</template>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</template>
|
||||
</main>
|
||||
</div>
|
||||
|
||||
@ -113,6 +135,7 @@
|
||||
createdElectionId: 0,
|
||||
errorMessage: "",
|
||||
errorDetails: {},
|
||||
voterIdentities: [],
|
||||
|
||||
addChoice() {
|
||||
this.election.choices.push("");
|
||||
@ -125,6 +148,8 @@
|
||||
async createElection() {
|
||||
this.errorMessage = "";
|
||||
this.errorDetails = {};
|
||||
this.createdElectionId = 0;
|
||||
this.voterIdentities = [];
|
||||
|
||||
const payload = {
|
||||
...this.election,
|
||||
@ -150,9 +175,33 @@
|
||||
|
||||
const locationHeader = response.headers.get("Location");
|
||||
this.createdElectionId = locationHeader.replace("/election/", "");
|
||||
|
||||
const contentType = response.headers.get("Content-Type") ?? "";
|
||||
if (contentType.includes("application/json")) {
|
||||
const data = await response.json();
|
||||
this.voterIdentities = data.voterIdentities;
|
||||
}
|
||||
} catch (error) {
|
||||
console.log(error);
|
||||
this.errorMessage = "Failed to create election.";
|
||||
}
|
||||
},
|
||||
|
||||
copyCode(code) {
|
||||
navigator.clipboard.writeText(code).then(() => {
|
||||
alert("Code copied to clipboard!");
|
||||
}).catch(err => {
|
||||
console.error("Failed to copy code: ", err);
|
||||
});
|
||||
},
|
||||
|
||||
copyAllCodes() {
|
||||
const allCodes = this.voterIdentities.join("\n");
|
||||
navigator.clipboard.writeText(allCodes).then(() => {
|
||||
alert("All codes copied to clipboard!");
|
||||
}).catch(err => {
|
||||
console.error("Failed to copy all codes: ", err);
|
||||
});
|
||||
}
|
||||
}));
|
||||
});
|
||||
|
Reference in New Issue
Block a user