Example PromiseStateContainer

December 20, 2015 ยท View on GitHub

import React, { Component, PropTypes } from 'react' import { connect, PromiseState } from 'react-refetch' import LoadingAnimation from './LoadingAnimation' import ErrorBox from './ErrorBox'

class PromiseStateContainer extends Component { static propTypes = { ps: PropTypes.instanceOf(PromiseState).isRequired, onPending: PropTypes.func, onNoResults: PropTypes.func, onRejection: PropTypes.func, onFulfillment: PropTypes.func.isRequired, }

static defaultProps = { onPending: (meta) => , onNoResults: (value, meta) => , onRejection: (reason, meta) => , }

render() { const { ps, onPending, onNoResults, onRejection, onFulfillment } = this.props

if (ps.pending) {
  return onPending(ps.meta)
} else if (ps.rejected) {
  return onRejection(ps.reason, ps.meta)
} else if (ps.fulfilled && $.isEmptyObject(ps.value)) {
  return onNoResults(ps.value, ps.meta)
} else if (ps.fulfilled) {
  return onFulfillment(ps.value, ps.meta)
} else {
  console.log('invalid promise state', ps)
  return null
}

} }

export default PromiseStateContainer