Created
May 8, 2018 21:31
-
-
Save busypeoples/0a7973c3286ca8b5b369804c957d0310 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 React from "react"; | |
const NOTASKED = "NOTASKED"; | |
const LOADING = "LOADING"; | |
const SUCCESS = "SUCCESS"; | |
const ERROR = "ERROR"; | |
const cata = (type, data = null) => o => o[type](data); | |
const Data = { | |
notAsked: () => ({ type: NOTASKED, cata: cata(NOTASKED) }), | |
loading: () => ({ type: LOADING, cata: cata(LOADING) }), | |
error: error => ({ type: ERROR, error, cata: cata(ERROR, error) }), | |
success: data => ({ type: SUCCESS, data, cata: cata(SUCCESS, data) }) | |
}; | |
class TaskComponent extends React.Component { | |
constructor(props) { | |
super(props); | |
this.state = Data.notAsked(); | |
} | |
componentDidMount() { | |
this.setState(Data.loading()); | |
this.props.task.fork( | |
error => this.setState(Data.error(error)), | |
data => this.setState(Data.success(data)) | |
); | |
} | |
render() { | |
const { render } = this.props; | |
return this.state.cata({ | |
NOTASKED: render.notAsked, | |
LOADING: render.loading, | |
ERROR: render.error, | |
SUCCESS: render.success | |
}); | |
} | |
} | |
export default TaskComponent; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment