Last active
August 1, 2018 05:31
-
-
Save masiamj/27b66c3fc965ccffa385f6fa5cd27120 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, { Component } from 'react' | |
import PropTypes from 'prop-types' | |
import GenericErrorComponent from './GenericErrorComponent' | |
import GenericLoadingComponent from './GenericLoadingComponent' | |
class GraphQLDataWrapper extends Component { | |
render () { | |
const { data, error, ErrorComponent, loading, LoadingComponent, renderData, ...rest } = this.props | |
// There was an error | |
if (error) return <ErrorComponent /> | |
// Making the request | |
if (loading) return <LoadingComponent /> | |
// Successful request! | |
return renderData({ data, ...rest }) | |
} | |
} | |
GraphQLDataWrapper.propTypes = { | |
data: PropTypes.object, | |
error: PropTypes.object, | |
ErrorComponent: PropTypes.element.isRequired, | |
loading: PropTypes.bool.isRequired, | |
LoadingComponent: PropTypes.element.isRequired, | |
renderData: PropTypes.func.isRequired | |
} | |
GraphQLDataWrapper.defaultProps = { | |
ErrorComponent: () => <GenericErrorComponent />, | |
LoadingComponent: () => <GenericLoadingComponent /> | |
} | |
export default GraphQLDataWrapper |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment