All files / src/providers prefetch.jsx

100% Statements 14/14
100% Branches 2/2
100% Functions 7/7
100% Lines 12/12
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                        2x 1x     2x             2x       2x 2x 2x 1x         2x 2x       1x                     1x    
/* eslint-disable */
import React from "react";
import PropTypes from "prop-types";
 
import { connect } from "react-redux";
import getRoutePreloadData from "../getRoutePreloadData";
 
import addResource from "../actions/addResource";
 
/**
 * The component needs to be able to fetch the resource we want.
 */
export const mapDispatchToProps = dispatch => ({
  preload: uri => dispatch(addResource(uri)),
});
 
const mapStateToProps = state => ({
  routes: state.horsemanRoutes,
});
 
export default function(Component) {
  class PrefetchLink extends React.Component {
    componentWillMount() {
      this.preloadLink();
    }
 
    preloadLink() {
      const { preload, to } = this.props;
      const endpoint = getRoutePreloadData(this.props.routes, to);
      if (endpoint) {
        preload(endpoint);
      }
    }
 
    render() {
      const { routes, preload, ...rest } = this.props;
      return <Component {...rest} />;
    }
  }
 
  PrefetchLink.propTypes = {
    /*
     * The url the link is pointed to
     */
    to: PropTypes.string.isRequired,
    /*
     * The function in dispatch
     */
    preload: PropTypes.func.isRequired,
  };
 
  return connect(mapStateToProps, mapDispatchToProps)(PrefetchLink);
}