Skip to content

Instantly share code, notes, and snippets.

@khadorkin
Forked from eyston/route.js
Created July 23, 2016 22:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save khadorkin/2e20f22b69783b8f1b4c234579e2f77b to your computer and use it in GitHub Desktop.
Save khadorkin/2e20f22b69783b8f1b4c234579e2f77b to your computer and use it in GitHub Desktop.
Doing onEnter hooks with Relay that require data / async
const node = Relay.QL`
query {
node(id: $channelId) {
... on Channel {
joined
${JoinChannelMutation.getFragment('channel')}
}
}
}
`;
const onEnter = (nextState, replace, callback) => {
const {params: {channelId}} = nextState;
const query = Relay.createQuery(node, {channelId});
Relay.Store.primeCache({query}, readyState => {
if (readyState.done) {
const [channel] = Relay.Store.readQuery(query);
if (channel.joined) {
callback();
} else {
console.log('need to join');
Relay.Store.commitUpdate(new JoinChannelMutation({channel}), {
onSuccess: () => {
console.log('joined!');
callback()
},
onFailure: () => {
console.log('sad panda');
// replace ... like I have an error page
}
});
}
}
});
}
export default (
<Route
path=':channelId'
component={ChannelWrapper}
onEnter={onEnter}
/>
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment