-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
51 lines (42 loc) · 1.25 KB
/
main.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
import Authenticator from './auth.js'
import Model from './model.js'
import View from './view.js'
let app = null
// setup
const Auth = new Authenticator(() => {
const [authContainer, ...others] = document.querySelectorAll('.container')
authContainer.classList.add('hide')
others.forEach(container => {
container.classList.remove('hide')
})
if (!app) {
app = new App(new Model(Auth.token), new View())
}
})
class App {
constructor(model, view) {
this.model = model
this.view = view
window.addEventListener('click', ({target}) => {
if (target.dataset && target.dataset.button) {
const action = target.dataset.button
this[action]()
}
})
}
get() {
console.info('Fetching data...')
this.model.getData()
.then(data => this.view.renderTable(data))
}
delete() {
const rows = [...this.view.$root.querySelectorAll('tr')]
const toDelete = [...this.view.$root.querySelectorAll('input[type="checkbox"]')]
.filter(row => row.checked)
.map(row => rows[row.dataset.delete].dataset.location)
// check if you really wanna delete them
if (!confirm(`Delete ${toDelete.length} posts?`)) return;
this.model.deleteReports(toDelete)
.then(() => this.get())
}
}