Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
80551 views
1
import React from 'react';
2
import Flux from 'flummox/component';
3
import View from './View';
4
import Doc from './Doc';
5
6
class DocHandler extends React.Component {
7
static willTransitionTo(transition, params) {
8
const { splat: docPath } = params;
9
10
const canonicalPath = DocHandler.canonicalDocPath(docPath);
11
12
if (docPath !== canonicalPath) transition.redirect(`/flummox/docs/${canonicalPath}`);
13
}
14
15
// Redundant since docs have already been fetched, but included for illustration
16
static async routerWillRun({ flux, state }) {
17
const docActions = flux.getActions('docs');
18
const { params: { splat: path } } = state;
19
20
const canonicalPath = DocHandler.canonicalDocPath(path);
21
22
return await docActions.getDoc(canonicalPath);
23
}
24
25
static canonicalDocPath(docPath) {
26
docPath = docPath.replace(/\.md$/, '');
27
docPath = docPath.replace(/\/index\/?$/, '');
28
29
return docPath;
30
}
31
32
getDocPath() {
33
const { params: { splat: docPath } } = this.props;
34
35
return DocHandler.canonicalDocPath(docPath);
36
}
37
38
render() {
39
const docPath = this.getDocPath();
40
41
return (
42
<div>
43
<Flux
44
docPath={docPath}
45
connectToStores={{
46
docs: (store, props) => ({
47
doc: store.getDoc(props.docPath)
48
})
49
}}
50
render={({ doc }) => <Doc doc={doc} />}
51
/>
52
</div>
53
);
54
}
55
}
56
57
export default DocHandler;
58
59