Last active
November 17, 2017 23:14
-
-
Save glasser/3a310a2be0f48dfea59902bb68893d83 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 { 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