Newer
Older
import * as Messages from "/services/messages/messages.js";
export function getElectionModel() {
if (electionModel == null) {
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
}
class ElectionModel {
constructor() {}
async getElection(id) {
if (this.elections == null) await this.refreshElections();
let electionToGet;
this.elections.forEach((election) => {
if (election.ID == id) electionToGet = election;
});
return electionToGet;
}
async getElections() {
if (this.elections == null) {
try {
const response = await fetch("/api/Election/", {
method: "GET",
headers: new Headers({
"XSRF-Token": this.current_user.xsrftoken,
}),
});
if (response.status !== 200) {
throw new Error(
`Elections could not be fetched (status ${response.status})`
);
}
this.elections = await response.json();
} catch (e) {
Messages.Show("is-warning", e.message);
console.error(e);
}
}
return this.elections;
}
async saveElection(
method,
ID,
Name,
BallotType,
MapAreaFile,
MapSectionFile
) {
try {
const response = await fetch("/api/Election/" + ID, {
method: method,
headers: new Headers({
"XSRF-Token": this.current_user.xsrftoken,
}),
body: JSON.stringify({
ID: ID,
Name: Name,
BallotType: BallotType,
MapAreaFile: MapAreaFile,
MapSectionFile: MapSectionFile,
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
}),
});
if (response.status == 409) {
throw new Error(
`The name of the election already exist (status ${response.status})`
);
}
if (response.status !== 200) {
throw new Error(
`Election could not be updated or created (status ${response.status})`
);
}
this.refreshElections();
return await response.json();
} catch (e) {
Messages.Show("is-warning", e.message);
console.error(e);
return;
}
}
async deleteElection(ID) {
try {
const response = await fetch("/api/Election/" + ID, {
method: "delete",
headers: new Headers({
"XSRF-Token": this.current_user.xsrftoken,
}),
});
if (response.status !== 200) {
throw new Error(
`Election could not be deleted (status ${response.status})`
);
}
this.refreshElections();
} catch (e) {
Messages.Show("is-warning", e.message);
console.error(e);
}
}
async refreshElections() {
this.elections = null;