All files / src/providers paginator.jsx

100% Statements 10/10
100% Branches 8/8
100% Functions 5/5
100% Lines 8/8
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   3x     3x             1x   1x                                     1x    
import React from "react";
import PropTypes from "prop-types";
 
import { connect } from "react-redux";
import * as types from "../constants/ActionTypes";
 
/**
 * The component needs to be able to fetch the resource we want.
 */
export const mapDispatchToProps = (dispatch, ownProps) => ({
  gotoPage: page =>
    dispatch({
      type: types.SET_CURRENT_PAGE,
      handle: ownProps.handle,
      data: { currentPage: page },
    }),
});
 
export const mapStateToProps = (state, ownProps) => {
  const stateData =
    state.horsemanPaginations && state.horsemanPaginations[ownProps.handle]
      ? state.horsemanPaginations[ownProps.handle]
      : {};
  return {
    currentPage: stateData.currentPage || null,
    totalPages: stateData.totalPages || null,
  };
};
 
export default function(Component) {
  const PaginatedComponent = props => <Component {...props} />;
 
  PaginatedComponent.propTypes = {
    /*
     * The handle for the pagination redux key
     */
    handle: PropTypes.string.isRequired,
    /*
     * The current page for the paginator
     */
    currentPage: PropTypes.number,
    /*
     * Total pages for this paginator
     */
    totalPages: PropTypes.number,
    /*
     * The goto page function in dispatch
     */
    gotoPage: PropTypes.func,
  };
 
  return connect(mapStateToProps, mapDispatchToProps)(PaginatedComponent);
}