Newer
Older
1
2
3
4
5
6
7
8
9
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import * as Messages from "/services/messages/messages.js";
let areaModel;
export function getAreaModel() {
if (areaModel == null) {
areaModel = new AreaModel();
}
return areaModel;
}
class AreaModel {
constructor() {}
async getArea(id) {
if (this.areas == null) await this.refreshAreas();
let areaToGet;
this.areas.forEach((area) => {
if (area.ID == id) areaToGet = area;
});
return areaToGet;
}
async getAreas() {
if (this.areas == null) {
try {
const response = await fetch("/api/Area/", {
method: "GET",
headers: new Headers({
"XSRF-Token": this.current_user.xsrftoken,
}),
});
if (response.status !== 200) {
throw new Error(
`Areas could not be fetched (status ${response.status})`
);
}
this.areas = await response.json();
} catch (e) {
Messages.Show("is-warning", e.message);
console.error(e);
}
}
return this.areas;
}
async saveArea(method, ID, ElectionID, Name, SeatNumber) {
try {
const response = await fetch(
"/api/Area/" + ID,
{
method: method,
headers: new Headers({
"XSRF-Token": this.current_user.xsrftoken,
}),
body: JSON.stringify({
ID: ID,
ElectionID: ElectionID,
Name: Name,
SeatNumber: SeatNumber,
}),
}
);
if (response.status !== 200) {
throw new Error(
`Area could not be updated or created (status ${response.status})`
);
}
this.refreshAreas();
return await response.json();
} catch (e) {
Messages.Show("is-warning", e.message);
console.error(e);
return;
}
}
try {
const response = await fetch("/api/Area/" + ID, {
method: "delete",
headers: new Headers({
"XSRF-Token": this.current_user.xsrftoken,
}),
});
if (response.status !== 200) {
throw new Error(
`Area could not be deleted (status ${response.status})`
);
}
} catch (e) {
Messages.Show("is-warning", e.message);
console.error(e);
}
}
async refreshAreas() {
this.areas = null;
await this.getAreas();
}
}