summaryrefslogtreecommitdiffstats
path: root/packages/gitbook-core/src/lib/renderWithContext.js
blob: 480cfef3b68ae704b6c831671ae02fe3e7129e4c (plain)
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
57
58
const React = require('react');

const { InjectedComponent } = require('../components/InjectedComponent');
const PJAXWrapper = require('../components/PJAXWrapper');
const I18nProvider = require('../components/I18nProvider');
const ContextProvider = require('../components/ContextProvider');
const History = require('../actions/history');
const Api = require('../actions/api');
const contextShape = require('../propTypes/Context');

const GitBookApplication = React.createClass({
    propTypes: {
        context:  contextShape,
        matching: React.PropTypes.object
    },

    componentDidMount() {
        const { context } = this.props;
        context.dispatch(History.activate());
        context.dispatch(Api.activate());
    },

    componentWillUnmount() {
        const { context } = this.props;
        context.dispatch(History.deactivate());
        context.dispatch(Api.deactivate());
    },

    render() {
        const { context, matching } = this.props;

        return (
            <ContextProvider context={context}>
                <PJAXWrapper>
                    <I18nProvider>
                        <InjectedComponent matching={matching} />
                    </I18nProvider>
                </PJAXWrapper>
            </ContextProvider>
        );
    }
});


/**
 * Render the application for a GitBook context.
 *
 * @param  {GitBookContext} context
 * @param  {Object} matching
 * @return {React.Element} element
 */
function renderWithContext(context, matching) {
    return (
        <GitBookApplication context={context} matching={matching} />
    );
}

module.exports = renderWithContext;