Skip to content
Snippets Groups Projects

Resolve "Handle CandidateList"

Merged Alexis POYEN requested to merge 29-handle-candidatelist into master
1 file
+ 102
0
Compare changes
  • Side-by-side
  • Inline
+ 102
0
import * as Messages from "/services/messages/messages.js";
let candidateListModel;
export function getCandidateListModel() {
if (candidateListModel == null) {
candidateListModel = new CandidateListModel();
}
return candidateListModel;
}
class CandidateListModel {
constructor() {}
async getCandidateList(id) {
if (this.candidateLists == null) await this.refreshCandidateLists();
let candidateListToGet;
this.candidateLists.forEach((candidateList) => {
if (candidateList.ID == id) candidateListToGet = candidateList;
});
return candidateListToGet;
}
async getCandidateLists() {
if (this.candidateLists == null) {
try {
const response = await fetch("/api/CandidateList/", {
method: "GET",
headers: new Headers({
"XSRF-Token": this.current_user.xsrftoken,
}),
});
if (response.status !== 200) {
throw new Error(
`CandidateLists could not be fetched (status ${response.status})`
);
}
this.candidateLists = await response.json();
} catch (e) {
Messages.Show("is-warning", e.message);
console.error(e);
}
}
return this.candidateLists;
}
async saveCandidateList(method, ID, Name, PartyID, RoundID, AreaID) {
try {
const response = await fetch(
"/api/CandidateList/" + ID,
{
method: method,
headers: new Headers({
"XSRF-Token": this.current_user.xsrftoken,
}),
body: JSON.stringify({
ID: ID,
Name: Name,
PartyID: PartyID,
RoundID: RoundID,
AreaID : AreaID
}),
}
);
if (response.status !== 200) {
throw new Error(
`CandidateList could not be updated or created (status ${response.status})`
);
}
this.refreshCandidateLists();
return await response.json();
} catch (e) {
Messages.Show("is-warning", e.message);
console.error(e);
return;
}
}
async deleteCandidateList(ID) {
try {
const response = await fetch("/api/CandidateList/" + ID, {
method: "delete",
headers: new Headers({
"XSRF-Token": this.current_user.xsrftoken,
}),
});
if (response.status !== 200) {
throw new Error(
`CandidateList could not be deleted (status ${response.status})`
);
}
} catch (e) {
Messages.Show("is-warning", e.message);
console.error(e);
}
}
async refreshCandidateLists() {
this.candidateLists = null;
await this.getCandidateLists();
}
}
Loading