Skip to content

Instantly share code, notes, and snippets.

@glasser
Last active November 17, 2017 23:14
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 glasser/3a310a2be0f48dfea59902bb68893d83 to your computer and use it in GitHub Desktop.
Save glasser/3a310a2be0f48dfea59902bb68893d83 to your computer and use it in GitHub Desktop.
import { graphql } from "react-apollo";
import gql from "graphql-tag";
import { compose, withState, lifecycle } from "recompose";
const DEFAULT_INTERVAL = 30 * 1000;
const ACTIVE_INTERVAL = 500;
const withData = compose(
// Pass down two props to the nested component: `pollInterval`,
// which defaults to our normal slow poll, and `setPollInterval`,
// which lets the nested components modify `pollInterval`.
withState("pollInterval", "setPollInterval", DEFAULT_INTERVAL),
graphql(
gql`
query getMigrationStatus {
activeMigration {
name
version
progress
}
}
`,
{
// If you think it's clear enough, you can abbreviate this as:
// options: ({pollInterval}) => ({pollInterval}),
options: props => {
return {
pollInterval: props.pollInterval
};
}
}
),
lifecycle({
componentWillReceiveProps({
data: { loading, activeMigration },
pollInterval,
setPollInterval
}) {
if (loading) {
return;
}
if (activeMigration && pollInterval !== ACTIVE_INTERVAL) {
setPollInterval(ACTIVE_INTERVAL);
} else if (
!activeMigration &&
pollInterval !== DEFAULT_INTERVAL
) {
setPollInterval(DEFAULT_INTERVAL);
}
}
})
);
const MigrationPanelWithData = withData(MigrationPanel);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment