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:
2025-01-14 20:07:02 +01:00
parent f784eb474f
commit 70a0cd7d3b
2 changed files with 51 additions and 0 deletions

View File

@ -104,6 +104,7 @@ func (app *application) CreateElection(w http.ResponseWriter, r *http.Request) {
app.serverError(w, r, err)
return
}
w.Header().Set("Content-Type", "application/json")
}
w.Header().Set("Location", fmt.Sprintf("/election/%v", electionId))
@ -290,6 +291,7 @@ func (app *application) GetElectionResults(w http.ResponseWriter, r *http.Reques
return
}
w.Header().Set("Content-Type", "application/json")
w.Write(response)
}

View File

@ -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);
});
}
}));
});