Skip to content
Snippets Groups Projects
Commit b6b90b9a authored by Alexis POYEN's avatar Alexis POYEN
Browse files

Resolve "For capturing vote show desks at each filter steps"

parent e594d339
No related branches found
No related tags found
No related merge requests found
......@@ -112,7 +112,7 @@ func (d *DataHandler) putElection(w http.ResponseWriter, r *http.Request, id int
func (d *DataHandler) deleteElection(w http.ResponseWriter, r *http.Request, id int) {
if id != 0 {
var o Election
if err := d.db.Preload("Areas").First(&o, id).Error; err != nil {
if err := d.db.Preload("Areas").Preload("Rounds").First(&o, id).Error; err != nil {
http.Error(w, ErrorIDIsMissing, http.StatusNotFound)
return
}
......@@ -121,6 +121,10 @@ func (d *DataHandler) deleteElection(w http.ResponseWriter, r *http.Request, id
d.deleteArea(w, r, int(area.ID))
}
for _, round := range o.Rounds {
d.deleteRound(w, r, int(round.ID))
}
d.db.Delete(&o)
} else {
http.Error(w, ErrorIDIsMissing, http.StatusNotFound)
......
......@@ -162,6 +162,7 @@ func deletionInCascadeGenericElectionTest(t *testing.T) {
do("GET", "/api/Area/1", xsrfHeader, ``, 404, `id is missing`)
do("GET", "/api/Section/1", xsrfHeader, ``, 404, `id is missing`)
do("GET", "/api/Desk/1", xsrfHeader, ``, 404, `id is missing`)
do("GET", "/api/Round/1", xsrfHeader, ``, 404, `id is missing`)
}
// Do an OAuth2 login with an known admin
......
......@@ -243,7 +243,10 @@ class Election {
electionTemplate(election) {
return /* HTML */ `<div class="card card-list">
<div id="elections-election-${election.ID}" class="card-content clickable">
<div
id="elections-election-${election.ID}"
class="card-content clickable"
>
<div class="content">
<nav class="level">
<div class="level-left">
......@@ -342,9 +345,8 @@ class Election {
this.parent.areaHandler.emptyAreas();
this.parent.sectionHandler.emptySections();
this.parent.deskHandler.emptyDesks();
document.getElementById("desk-new").setAttribute("disabled", "true");
document.getElementById("section-new").setAttribute("disabled", "true");
document.getElementById("area-new").setAttribute("disabled", "true");
document.getElementById("areas").style.display = "none";
}
cloneElection(election) {
......
......@@ -25,6 +25,7 @@ class Round {
async mount(where) {
this.ElectionModel.current_user = await Auth.GetUser();
this.RoundModel.current_user = await Auth.GetUser();
this.RoundModel.refreshRounds();
const mountpoint = where;
document.getElementById(mountpoint).innerHTML = /* HTML */ `
<header class="card-header">
......@@ -38,7 +39,7 @@ class Round {
`;
this.mountModal("round-modal");
this.handleDom();
this.displayRounds();
await this.displayRounds();
}
handleDom() {
......
......@@ -92,7 +92,10 @@ class DeskRoundSelector {
deskRoundTemplate(deskRound) {
return /* HTML */ `<div class="card card-list">
<div id="deskrounds-deskround-${deskRound.ID}" class="card-content clickable">
<div
id="deskrounds-deskround-${deskRound.ID}"
class="card-content clickable"
>
<div class="content">
<nav class="level">
<div
......@@ -111,17 +114,27 @@ class DeskRoundSelector {
"input",
function (event) {
if (event.target.id === "election-select") {
deskRoundHandler.loadRounds(event.target.value);
deskRoundHandler.ElectionID = event.target.value;
deskRoundHandler.clearRounds();
deskRoundHandler.clearAreas();
deskRoundHandler.clearSections();
deskRoundHandler.loadRounds();
}
if (event.target.id === "round-select") {
deskRoundHandler.loadAreas(event.target.value);
deskRoundHandler.RoundID = event.target.value;
deskRoundHandler.clearAreas();
deskRoundHandler.clearSections();
deskRoundHandler.loadAreas();
}
if (event.target.id === "area-select") {
deskRoundHandler.loadSections(event.target.value);
deskRoundHandler.AreaID = event.target.value;
deskRoundHandler.clearSections();
deskRoundHandler.loadSections();
}
if (event.target.id === "section-select") {
deskRoundHandler.loadDesks(event.target.value);
deskRoundHandler.SectionID = event.target.value;
}
deskRoundHandler.loadDesks();
},
false
);
......@@ -141,21 +154,18 @@ class DeskRoundSelector {
selectElection.appendChild(el);
});
this.loadRounds(elections[0].ID);
this.ElectionID = elections[0].ID;
this.loadRounds();
}
async loadRounds(ElectionID) {
this.ElectionID = ElectionID;
async loadRounds() {
let deskRoundHandler = this;
let selectRound = document.getElementById("round-select");
let rounds = await this.RoundModel.getRounds();
rounds.filter((round) => {
rounds = rounds.filter((round) => {
return round.ElectionID == deskRoundHandler.ElectionID;
});
for (let i = selectRound.options.length - 1; i >= 0; i--) {
selectRound.remove(i);
}
rounds.forEach(async (round) => {
let election = await this.ElectionModel.getElection(round.ElectionID);
let el = document.createElement("option");
......@@ -169,12 +179,9 @@ class DeskRoundSelector {
el.value = round.ID;
selectRound.appendChild(el);
});
this.loadAreas(rounds[0].ID);
}
async loadAreas(RoundID) {
this.RoundID = RoundID;
async loadAreas() {
let deskRoundHandler = this;
let selectArea = document.getElementById("area-select");
let areas = await this.AreaModel.getAreas();
......@@ -182,21 +189,15 @@ class DeskRoundSelector {
return area.ElectionID == deskRoundHandler.ElectionID;
});
for (let i = selectArea.options.length - 1; i >= 0; i--) {
selectArea.remove(i);
}
areas.forEach(async (area) => {
let el = document.createElement("option");
el.textContent = area.Name;
el.value = area.ID;
selectArea.appendChild(el);
});
this.loadSections(areas[0].ID);
}
async loadSections(AreaID) {
this.AreaID = AreaID;
async loadSections() {
let deskRoundHandler = this;
let selectSection = document.getElementById("section-select");
let sections = await this.SectionModel.getSections();
......@@ -204,26 +205,22 @@ class DeskRoundSelector {
return section.AreaID == deskRoundHandler.AreaID;
});
for (let i = selectSection.options.length - 1; i >= 0; i--) {
selectSection.remove(i);
}
sections.forEach((section) => {
let el = document.createElement("option");
el.textContent = section.Name;
el.value = section.ID;
selectSection.appendChild(el);
});
this.loadDesks(sections[0].ID);
}
async loadDesks(SectionID) {
this.SectionID = SectionID;
async loadDesks() {
let deskRoundHandler = this;
let deskRounds = await this.DeskRoundModel.getDeskRounds();
deskRounds = deskRounds.filter((deskRound) => {
return deskRound.RoundID == deskRoundHandler.RoundID;
});
// Filter the desks to display only the one affected to capturer
if (this.DeskRoundModel.current_user.role === "CAPTURER") {
let capturer = await this.CapturerModel.getCapturerByUserID(
this.DeskRoundModel.current_user.id
......@@ -237,13 +234,34 @@ class DeskRoundSelector {
}
deskRounds = deskRoundsToKeep;
}
let selectRound = document.getElementById("round-select");
if (selectRound.value == 0) {
deskRounds = [];
}
let deskRoundsFiltered = [];
for (let deskRound of deskRounds) {
let desk = await deskRoundHandler.DeskModel.getDesk(deskRound.DeskID);
if (desk.SectionID == deskRoundHandler.SectionID)
deskRoundsFiltered.push(deskRound);
let selectArea = document.getElementById("area-select");
if (selectArea.value != 0) {
let area = await this.AreaModel.getArea(selectArea.value);
for (let i in area.Sections) {
for (let deskRound of deskRounds) {
let desk = await deskRoundHandler.DeskModel.getDesk(deskRound.DeskID);
if (desk.SectionID == area.Sections[i].ID)
deskRoundsFiltered.push(deskRound);
}
}
deskRounds = deskRoundsFiltered;
}
deskRoundsFiltered = [];
let selectSection = document.getElementById("section-select");
if (selectSection.value != 0) {
for (let deskRound of deskRounds) {
let desk = await deskRoundHandler.DeskModel.getDesk(deskRound.DeskID);
if (desk.SectionID == deskRoundHandler.SectionID)
deskRoundsFiltered.push(deskRound);
}
deskRounds = deskRoundsFiltered;
}
deskRounds = deskRoundsFiltered;
const markup = deskRounds
.map((deskRound) => this.deskRoundTemplate(deskRound))
.join("");
......@@ -269,4 +287,37 @@ class DeskRoundSelector {
deskRound.ID
);
}
clearRounds() {
let selectRound = document.getElementById("round-select");
for (let i = selectRound.options.length - 1; i >= 0; i--) {
selectRound.remove(i);
}
let el = document.createElement("option");
el.textContent = "Veuillez sélectionner un tour";
el.value = 0;
selectRound.appendChild(el);
}
clearAreas() {
let selectArea = document.getElementById("area-select");
for (let i = selectArea.options.length - 1; i >= 0; i--) {
selectArea.remove(i);
}
let el = document.createElement("option");
el.textContent = "Veuillez sélectionner une circonscription";
el.value = 0;
selectArea.appendChild(el);
}
clearSections() {
let selectSection = document.getElementById("section-select");
for (let i = selectSection.options.length - 1; i >= 0; i--) {
selectSection.remove(i);
}
let el = document.createElement("option");
el.textContent = "Veuillez sélectionner une section";
el.value = 0;
selectSection.appendChild(el);
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment