Created
April 6, 2017 17:38
Star
You must be signed in to star a gist
This file contains 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
// 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