Created
June 14, 2017 03:41
-
-
Save Bertrand31/c1da45a829f1fdd1ab2b199732e1b2f8 to your computer and use it in GitHub Desktop.
This file contains 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 from 'react'; | |
import { withApollo } from 'react-apollo'; | |
import { logger } from '$utils'; | |
export default (Component, queryOptions, queryAfterMount = true) => withApollo( | |
class extends React.Component { | |
constructor(props) { | |
super(props); | |
this.state = { data: { loading: true } }; | |
} | |
componentDidMount() { | |
if (queryAfterMount) this.queryData(); | |
} | |
queryData = () => { | |
this.props.client.query(queryOptions) | |
.then(({ data }) => this.setState({ data })) | |
.catch((err) => { | |
logger(err); | |
return this.setState({ data: { loading: false, error: true } }); | |
}); | |
} | |
cancelLoading = () => this.setState({ data: { loading: false } }) | |
render = () => ( | |
<Component | |
{...this.props} | |
data={this.state.data} | |
queryData={this.queryData} | |
cancelLoading={this.cancelLoading} | |
/> | |
); | |
}, | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment