just-paja/react-saga-rest

View on GitHub
src/mappers/progressMapper.jsx

Summary

Maintainability
A
0 mins
Test Coverage
import { connect } from 'react-redux'

const getComponentName = (component, defaultName) => (
  component.displayName ||
  component.name ||
  defaultName
)

export default ProgressSwitch => (WrappedComponent, {
  NotFoundComponent,
  ErrorComponent,
  LoaderComponent,
  progressSelector,
  onResourceChange,
  onExit,
  matchParam
}) => {
  if (!WrappedComponent) {
    throw new Error('You must pass a Component.')
  }
  if (!onResourceChange) {
    throw new Error('You must pass onResourceChange.')
  }
  if (!progressSelector) {
    throw new Error('You must pass progressSelector.')
  }

  class ProgressSwitchNamed extends ProgressSwitch {
  }

  const wrappedName = getComponentName(WrappedComponent, 'Component')
  const outerName = getComponentName(ProgressSwitch, 'ProgressSwitch')
  ProgressSwitchNamed.displayName = `${outerName}(${wrappedName})`

  const mapStateToProps = (state, ownProps) => ({
    progress: progressSelector(state, ownProps),
    WrappedComponent,
    NotFoundComponent,
    ErrorComponent,
    LoaderComponent,
    matchParam
  })

  return connect(mapStateToProps, {
    onResourceChange,
    onExit
  })(ProgressSwitchNamed)
}