import React from 'react'
import { arrayOf, object, array, string, func } from 'prop-types'
import { connect } from 'react-redux'
import { getRows, getCols, getSortBy } from './redux/selectors'

class ConnectTable extends React.Component {
  render() {
    const { render, ...rest } = this.props
    return render({ ...rest })
  }
}

ConnectTable.propTypes = {
  rows: arrayOf(object).isRequired,
  cols: array,
  sortBy: string,
  order: string,
  render: func,
  path: string
}

export default connect((state, { path }) => {
  return {
    rows: getRows(state, path),
    cols: getCols(state, path),
    sortBy: getSortBy(state, path).sortBy,
    order: getSortBy(state, path).order
  }
})(ConnectTable)
