diff --git a/cmd/web/handlers.go b/cmd/web/handlers.go index 90303e8..7325cb4 100644 --- a/cmd/web/handlers.go +++ b/cmd/web/handlers.go @@ -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) } diff --git a/ui/create-election.html b/ui/create-election.html index bfdb774..94403c0 100644 --- a/ui/create-election.html +++ b/ui/create-election.html @@ -96,6 +96,28 @@

Election ID:

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