Last active
August 29, 2015 14:22
-
-
Save miklschmidt/2e66fc0a514c7ddeed3f to your computer and use it in GitHub Desktop.
Flux and request tracking: part 2
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
class RequestTracker extends React.Component { | |
static propTypes = { | |
requestId: React.PropTypes.number.isRequired, | |
success: React.PropTypes.func, | |
fail: React.PropTypes.func, | |
always: React.PropTypes.func | |
} | |
constructor(props) { | |
let item = requestStore.getItem(this.props.requestId); | |
let state = { | |
status: null, | |
ok: null, | |
}; | |
if (item !== null) { | |
state = item; | |
} | |
this.state = state; | |
} | |
componentDidMount() { | |
requestStore.changed.add(this.updateFromStore); | |
this.executeCallbacks(); | |
} | |
componentWillUnmount() { | |
requestStore.changed.remove(this.updateFromStore); | |
requestActionCreator.removeRequest(this.props.requestId); | |
} | |
componentDidUpdate(prevProps, prevState) { | |
if (prevState.ok === null && this.state.ok !== null) { | |
this.executeCallbacks(); | |
} | |
} | |
updateFromStore() { | |
let item = requestStore.getItem(this.props.requestID); | |
this.setState({item: item}); | |
} | |
executeCallbacks() { | |
if (this.state.ok === false && typeof this.props.fail == 'function') { | |
this.props.fail(); | |
} | |
if (this.state.ok === true && typeof this.props.success == 'function') { | |
this.props.success(); | |
} | |
if (typeof this.props.always == 'function') { | |
this.props.always(); | |
} | |
} | |
render() { | |
return null; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment