Skip to content

Instantly share code, notes, and snippets.

@busypeoples
Created May 8, 2018 21:31
Show Gist options
  • Save busypeoples/0a7973c3286ca8b5b369804c957d0310 to your computer and use it in GitHub Desktop.
Save busypeoples/0a7973c3286ca8b5b369804c957d0310 to your computer and use it in GitHub Desktop.
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