-
Notifications
You must be signed in to change notification settings - Fork 114
/
DocStore.js
46 lines (36 loc) · 910 Bytes
/
DocStore.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
import { Store } from 'flummox';
import { Map } from 'immutable';
export default class DocStore extends Store {
constructor({ docActions }) {
super();
this.register(docActions.getDoc, this.handleNewDoc);
this.register(docActions.getAllDocs, this.handleNewDocs);
this.state = {
docs: new Map()
};
}
handleNewDoc(newDoc) {
const docs = {
[newDoc.path]: newDoc
};
this.setState({
docs: this.state.docs.merge(docs)
});
}
handleNewDocs(newDocs) {
const docs = newDocs.reduce((result, doc) => {
result[doc.path] = doc;
return result;
}, {});
this.setState({
docs: this.state.docs.merge(docs)
});
}
getDoc(path) {
let doc = this.state.docs.find(doc => doc.get('path') === path);
if (!doc) {
doc = this.state.docs.find(doc => doc.get('path') === `${path}/index`);
}
return doc;
}
}