Skip to content

Instantly share code, notes, and snippets.

@Emilios1995
Created April 6, 2017 17:38
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save Emilios1995/76af6f021ea4b5580e741ab66f5178e2 to your computer and use it in GitHub Desktop.
// This goes after https://gist.github.com/DrBoolean/3f5ac08a5bf1c4673757
const setNameActionCretor = name => ({ type: "SET_NAME", payload: { name } });
const setName = localStorage
.chain(ls => safeProp("user", ls))
.chain(u => safeProp("name", u))
.map(n => n.toUpperCase())
.chain(printLn)
.map(m => m.toLowerCase())
.map(setNameActionCretor); // -> free monad will return a normal redux action
// redux
const reducer = (state = {}, action) => {
switch (action.type) {
case "SET_NAME":
return Object.assign(state, { name: action.payload.name });
default:
return state;
}
};
const createStore = (initial, reducer) => {
let state = initial;
return {
dispatch: action => {
const newState = reducer(state, action);
return newState;
}
};
};
const store = createStore({}, reducer);
// this could be a redux middleware
const forkAndDispatch = action => {
action.fork(console.error, compose(console.log, store.dispatch));
};
// somewhere in the app...
forkAndDispatch(setName.foldMap(interpret, Task.of));
// {name: jerry}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment