All files wrapper.js

73.58% Statements 39/53
60% Branches 12/20
62.5% Functions 10/16
75% Lines 39/52
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 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 1812x 2x 2x 2x 2x 2x     14x       6x         6x                     14x       14x                 14x     14x         14x   14x   14x 12x     14x                                                                                                                           14x   14x                     6x   6x 6x   6x       2x     18x 18x   14x 14x 14x           18x 18x       18x         2x   2x       2x       2x       2x 2x
var React = require('react');
var withRouter = require('react-router-dom').withRouter;
var PropTypes = require('prop-types');
var createClass = require('create-react-class');
var hoistStatics = require('hoist-non-react-statics');
var lib = require('./lib');
 
function isNode() {
    return (typeof process === 'object' && process + '' === '[object process]');
}
 
function getDisplayName(Cmp) {
    return Cmp.displayName || Cmp.name || 'Component';
}
 
function withWrapper(Cmp) {
 
    var Wrapper = createClass({
 
        displayName: 'WithWrapper',
 
        contextTypes: {
            store: PropTypes.any,
            getInitialProps: PropTypes.func
        },
 
        getInitialState: function getInitialState() {
 
            var initialProps = this.context.getInitialProps();
 
            // console.log('Initial props from context', initialProps);
 
            return lib.extends({}, initialProps || {}, {
                initialLoading: !initialProps, // no props means it will load
                initialError: initialProps && initialProps.initialError && new Error(initialProps.initialError) // it comes as string
            });
 
        },
 
        componentWillMount: function componentWillMount() {
 
            var self = this;
 
            // On NodeJS setState is a no-op, besides, getInitialProps will be called by server rendering procedure
            Eif (isNode()) {
 
                // if (!this.state.initialLoading) return;
                //match, location, history, staticContext
 
                this.props.staticContext.store = this.context.store; //FIXME Brutal access to Redux Provider's store
 
                this.props.staticContext.location = this.props.location;
 
                if (Cmp.getInitialProps) {
                    this.props.staticContext.getInitialProps = Cmp.getInitialProps.bind(Cmp);
                }
 
                return;
 
            }
 
            // On client side this function should not be called if props were passed from server
            if (!this.state.initialLoading) return;
 
            return new Promise(function(res) {
 
                res(Cmp.getInitialProps ? Cmp.getInitialProps({
                    location: self.props.location,
                    params: self.props.params,
                    req: null,
                    res: null,
                    store: self.context.store //FIXME Brutal access to Redux Provider's store
                }) : null);
 
            }).then(function(props) {
 
                self.setState(lib.extends({}, props, {
                    initialLoading: false,
                    initialError: null
                }));
 
            }).catch(function onError(e) {
 
                console.error(Wrapper.displayName + '.getInitialProps has failed:', e);
 
                self.setState({
                    initialLoading: false,
                    initialError: e
                });
 
            });
 
        },
 
        getInitialProps: function getInitialProps() {
 
            var self = this;
 
            return new Promise(function(resolve) {
 
                if (self.state.initialLoading) {
                    console.warn(Wrapper.displayName + '.getInitialProps is already pending, make sure you won\'t have race condition');
                }
 
                self.state = {};
 
                self.setState({
                    initialLoading: true,
                    initialError: null
                }, function() {
                    resolve(self.componentWillMount());
                });
 
            });
 
        },
 
        render: function render() {
 
            var props = lib.objectWithoutProperties(this.props, ["children"]);
 
            return React.createElement(
                Cmp,
                //TODO Add mapping function
                lib.extends({getInitialProps: this.getInitialProps}, this.state, props),
                this.props.children
            );
 
        }
 
    });
 
    Wrapper = withRouter(Wrapper);
 
    Wrapper.displayName = 'withWrapper(' + getDisplayName(Cmp) + ')';
    Wrapper.OriginalComponent = Cmp;
 
    return hoistStatics(Wrapper, Cmp);
 
}
 
var WrapperProvider = createClass({
 
    getChildContext: function getChildContext() {
        var self = this;
        return {
            getInitialProps: function() {
                var initialProps = self.initialProps;
                self.initialProps = null; // initial props can be used only once
                return initialProps;
            }
        };
    },
 
    getInitialState: function getInitialState() {
        this.initialProps = this.props.initialProps;
        return {};
    },
 
    render: function render() {
        return this.props.children;
    }
 
});
 
WrapperProvider.displayName = 'WrapperProvider';
 
WrapperProvider.propTypes = {
    initialProps: PropTypes.any
};
 
WrapperProvider.defaultProps = {
    initialProps: null
};
 
WrapperProvider.childContextTypes = {
    getInitialProps: PropTypes.func
};
 
exports.withWrapper = withWrapper;
exports.WrapperProvider = WrapperProvider;