-
Notifications
You must be signed in to change notification settings - Fork 10
/
ViewStore.js
56 lines (47 loc) · 1.4 KB
/
ViewStore.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
import { observable, computed, action } from 'mobx';
import { fromPromise } from 'mobx-utils';
class ViewStore {
fetch = null;
@observable currentUser = null;
@observable currentView = null;
constructor(fetch) {
this.fetch = fetch
}
@computed get isAuthenticated() {
return this.currentUser !== null
}
@computed get currentPath() {
switch(this.currentView.name) {
case "overview": return "/document/"
case "document": return `/document/${this.currentView.documentId}`
}
}
@action showOverview() {
this.currentView = {
name: "overview",
documents: fromPromise(this.fetch(`/json/documents.json`))
}
}
@action showDocument(documentId) {
this.currentView = {
name: "document",
documentId,
document: fromPromise(
this.isAuthenticated
? this.fetch(`/json/${documentId}.json`)
: Promise.reject("Authentication required")
)
}
}
@action performLogin(username, password, callback) {
this.fetch(`/json/${username}-${password}.json`)
.then(user => {
this.currentUser = user
callback(true)
})
.catch(err => {
callback(false)
})
}
}
export default ViewStore;