Skip to content

Instantly share code, notes, and snippets.

export const setCats = () => async (dispatch, getState) => {
const cats = await dispatch(fetchJson('GET', '/cats', filter));
dispatch({ type: SET_CATS, cats });
};
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);
const defaultState = {
cats: [],
filter: {},
didSetCats: false,
};
const SET_CATS = 'cats/SET_CATS';
const SET_FILTER = 'cats/SET_FILTER';
export default (state = defaultState, action) {
class Cats extends Component {
static fetchData({ location, dispatch }) {
return dispatch(setQueryParams(location.query))
.then(() => dispatch(getCats()));
}
componentDidMount() {
Cats.fetchData(this.props);
}
// 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) {
class Cats extends Component {
static fetchData({ dispatch }) {
return dispatch(getCats());
}
componentDidMount() {
Cats.fetchData(this.props);
}
render() {
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));
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;
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>
// 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;