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
import * as Messages from "/services/messages/messages.js";
let sectionModel;
export function getSectionModel() {
if (sectionModel == null) {
sectionModel = new SectionModel();
}
return sectionModel;
}
class SectionModel {
constructor() {}
async getSection(id) {
if (this.sections == null) await this.refreshSections();
let sectionToGet;
this.sections.forEach((section) => {
if (section.ID == id) sectionToGet = section;
});
return sectionToGet;
}
async getSections() {
if (this.sections == null) {
try {
const response = await fetch("/api/Section/", {
method: "GET",
headers: new Headers({
"XSRF-Token": this.current_user.xsrftoken,
}),
});
if (response.status !== 200) {
throw new Error(
`Sections could not be fetched (status ${response.status})`
);
}
this.sections = await response.json();
} catch (e) {
Messages.Show("is-warning", e.message);
console.error(e);
}
}
return this.sections;
}
async saveSection(method, ID, AreaID, Name, MapID) {
try {
const response = await fetch("/api/Section/" + ID, {
method: method,
headers: new Headers({
"XSRF-Token": this.current_user.xsrftoken,
}),
body: JSON.stringify({
ID: ID,
AreaID: AreaID,
Name: Name,
59
60
61
62
63
64
65
66
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
}),
});
if (response.status !== 200) {
throw new Error(
`Section could not be updated or created (status ${response.status})`
);
}
this.refreshSections();
return await response.json();
} catch (e) {
Messages.Show("is-warning", e.message);
console.error(e);
return;
}
}
async deleteSections(ID) {
try {
const response = await fetch("/api/Section/" + ID, {
method: "delete",
headers: new Headers({
"XSRF-Token": this.current_user.xsrftoken,
}),
});
if (response.status !== 200) {
throw new Error(
`Section could not be deleted (status ${response.status})`
);
}
} catch (e) {
Messages.Show("is-warning", e.message);
console.error(e);
}
}
async refreshSections() {
this.sections = null;
await this.getSections();
}
}