-
Notifications
You must be signed in to change notification settings - Fork 0
/
model.js
60 lines (54 loc) · 2.11 KB
/
model.js
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
import { makeFrontendPath, makeServerPath } from './utils.js'
class Model {
constructor(token) {
this.$sDomain = document.getElementById('server')
this.$sProtocol = document.getElementById('server-TLS')
this.$fDomain = document.getElementById('frontend')
this.$fProtocol = document.getElementById('frontend-TLS')
this.token = token
}
get sDomain() { return this.$sDomain.value }
get fDomain() { return this.$fDomain.value }
get sProtocol() { return this.$sProtocol.checked === true ? 'https' : 'http' }
get fProtocol() { return this.$fProtocol.checked === true ? 'https' : 'http' }
get serverURI() { return `${this.sProtocol}://${this.sDomain}` }
get frontendURI() { return `${this.fProtocol}://${this.fDomain}` }
getData() {
return this.fetchRawReports()
.then(reports => reports.map(({
numReports, code, parentType, questionID, reviewID, commentID, title, body
}) => ({
parentType,
numReports,
code,
title,
body,
apiLink: `${this.serverURI}/${makeServerPath(code, parentType, questionID, reviewID, commentID)}`,
link: `${this.frontendURI}/${makeFrontendPath(code, parentType, questionID, reviewID, commentID)}`
})))
}
fetchRawReports() {
const url = `${this.serverURI}/api/uni/reports`
return fetch(url,
{
headers: {
'Authorization': `Bearer ${this.token}`,
'Content-Type': 'application/json',
'Cache-Control': 'no-cache'
}
})
.then((res) => res.json())
}
deleteReports(locations) {
return Promise.all(
locations.map(loc => fetch(loc, {
headers: {
'Authorization': `Bearer ${this.token}`,
'Content-Type': 'application/json',
'Cache-Control': 'no-cache'
},
method: 'DELETE'
})))
}
}
export default Model