Skip to content

Instantly share code, notes, and snippets.

@arecvlohe
Last active September 3, 2017 19:22
Show Gist options
  • Select an option

  • Save arecvlohe/8430b26e5b6f4e2f35793e5407b9e42d to your computer and use it in GitHub Desktop.

Select an option

Save arecvlohe/8430b26e5b6f4e2f35793e5407b9e42d to your computer and use it in GitHub Desktop.
An async example of using union-type
import Type from "union-type";
import { createStore, combineReducers } from "redux";
import memoize from "ramda/src/memoize";
import path from "ramda/src/path";
import axios from "axios";
import Future from "fluture";
// MESSAGES
const Msg = Type({
FETCH_GIF: [],
GIF_RESPONSE: [Object],
GIF_ERROR: [Object]
});
// ASYNC ACTIONS
const fetchGif = () =>
Future((rej, res) => {
axios
.get(
SOME_API_URL
)
.then(res)
.catch(rej);
});
const onGifSuccess = response => {
gifResponse(response);
};
const onGifError = err => {
gifErr(err);
};
// MESSAGE HANDLERS
const nextState = Msg.caseOn({
FETCH_GIF: state => {
fetchGif().fork(onGifError, onGifSuccess);
return state;
},
GIF_RESPONSE: (response, state) => ({ ...state, gif: response }),
GIF_ERROR: (err, state) => ({ ...state, gifErr: err }),
_: state => state
});
// STATE
const initialState = {
gif: {},
gifError: {}
};
// UPDATE
function app(state = initialState, { type = Msg.DEFAULT, payload = null }) {
if (typeof type === "string") return state;
return nextState(type, state);
}
// STORE
const store = createStore(
combineReducers({ app }),
window.__REDUX_DEVTOOLS_EXTENSION__ && window.__REDUX_DEVTOOLS_EXTENSION__()
);
// ACTIONS
export const getGif = () => {
store.dispatch({ type: Msg.FETCH_GIF });
};
export const gifResponse = response => {
store.dispatch({ type: Msg.GIF_RESPONSE(response) });
};
export const gifError = err => {
store.dispatch({ type: Msg.GIF_ERROR(err) });
};
// SELECTORS
export const gifUrl = memoize(
path(["app", "gif", "data", "data", "image_original_url"])
);
export const gifErr = memoize(path(["app", "gifErr"]));
// EXPORT DEFAULT
export default store;
@DrBoolean
Copy link
Copy Markdown

Seems like onGifSuccess and err should be inlined?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment