Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
80542 views
1
import { Store } from 'flummox';
2
import { Map } from 'immutable';
3
4
export default class DocStore extends Store {
5
constructor({ docActions }) {
6
super();
7
8
this.register(docActions.getDoc, this.handleNewDoc);
9
this.register(docActions.getAllDocs, this.handleNewDocs);
10
11
this.state = {
12
docs: new Map()
13
};
14
}
15
16
handleNewDoc(newDoc) {
17
const docs = {
18
[newDoc.path]: newDoc
19
};
20
21
this.setState({
22
docs: this.state.docs.merge(docs)
23
});
24
}
25
26
handleNewDocs(newDocs) {
27
const docs = newDocs.reduce((result, doc) => {
28
result[doc.path] = doc;
29
return result;
30
}, {});
31
32
this.setState({
33
docs: this.state.docs.merge(docs)
34
});
35
}
36
37
getDoc(path) {
38
let doc = this.state.docs.find(doc => doc.get('path') === path);
39
40
if (!doc) {
41
doc = this.state.docs.find(doc => doc.get('path') === `${path}/index`);
42
}
43
44
return doc;
45
}
46
}
47
48