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
export const setCats = () => async (dispatch, getState) => { | |
const cats = await dispatch(fetchJson('GET', '/cats', filter)); | |
dispatch({ type: SET_CATS, cats }); | |
}; |
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
const FETCH = '@@middleware/fetch/FETCH'; | |
export default (baseUrl, fetchImplementation) => () => next => async action => { | |
if (action.type === FETCH) { | |
const { method, path, body } = action; | |
const params = { method }; | |
if (body) params.body = JSON.stringify(body); | |
const url = baseUrl + path; | |
const response = await fetchImplementation(url, params); |
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
const defaultState = { | |
cats: [], | |
filter: {}, | |
didSetCats: false, | |
}; | |
const SET_CATS = 'cats/SET_CATS'; | |
const SET_FILTER = 'cats/SET_FILTER'; | |
export default (state = defaultState, action) { |
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
class Cats extends Component { | |
static fetchData({ location, dispatch }) { | |
return dispatch(setQueryParams(location.query)) | |
.then(() => dispatch(getCats())); | |
} | |
componentDidMount() { | |
Cats.fetchData(this.props); | |
} |
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
// RENDER PAGE | |
server.all('*', (req, res) => { | |
const { store } = req; | |
match({ routes, location: req.url }, (error, redirectLocation, renderProps) => { | |
if (redirectLocation) { | |
res.redirect(301, redirectLocation.pathname + redirectLocation.search); | |
} else if (error) { | |
res.status(500).send(error.message); | |
} else if (!renderProps) { |
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
class Cats extends Component { | |
static fetchData({ dispatch }) { | |
return dispatch(getCats()); | |
} | |
componentDidMount() { | |
Cats.fetchData(this.props); | |
} | |
render() { |
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
export const updateRemoveCatViaForm = inputParams => async dispatch => { | |
const { action, id } = inputParams; | |
const params = await schema.validate(inputParams, { | |
abortEarly: false, | |
stripUnknown: true, | |
}); | |
return await action === 'remove' | |
? dispatch(removeCat(id)) | |
: dispatch(updateCat(id, params)); |
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 serialize from 'form-serialize'; | |
import actionCreators from './actionCreators'; | |
export const formDispatcher = dispatch => event => { | |
event.preventDefault(); | |
const { currentTarget } = event; | |
const isChild = 'form' in currentTarget; | |
const form = isChild ? currentTarget.form : currentTarget; |
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
const Cat = ({ cat, handleCatUpdateDelete }) => ( | |
<div> | |
<h1>{cat.name}</h1> | |
<form method="POST"> | |
<input type="hidden" name="action-creator" value="update-remove-cat" /> | |
<input type="hidden" name="id" value={cat.id} /> | |
<input name="name" defaultValue={cat.name} /> | |
<inputtype="number" name="age" defaultValue={cat.age} /> | |
<select name="gender" defaultValue={cat.gender}> | |
<option value="male">Male</option> |
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
// SETUP STORE | |
server.all('*', (req, res, next) => { | |
req.store = createStore(reducers, middlewares); | |
next(); | |
}); | |
// FORM HANDLERS | |
server.post('*', async (req, res, next) => { | |
try { | |
const actionParams = req.body; |