Skip to content

Instantly share code, notes, and snippets.

@hendrikswan
Last active May 7, 2019 16:54
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hendrikswan/e61bd672bc6a78f3d7a5f650227ede51 to your computer and use it in GitHub Desktop.
Save hendrikswan/e61bd672bc6a78f3d7a5f650227ede51 to your computer and use it in GitHub Desktop.
const handleDrawingEvent = line => ({
line,
type: 'LINE_DRAWN'
})
const drawingSyncSuccess = () => ({
type: 'DRAWING_SYNC_SUCCESS',
})
const drawingSyncFailure = () => ({
type: 'DRAWING_SYNC_FAILED',
})
const drawingMouseUp = () => {
return (dispatch, getState) => {
return fetch('/a/fake/url', {
method: 'post',
body: JSON.stringify(getState().lines)
})
.then(response => {
if (response.ok) {
return dispatch(drawingSyncSuccess());
}
dispatch(drawingSyncFailure())
})
.catch(err => dispatch(drawingSyncFailure()))
}
}
const defaultState = { lines: [] };
const reducer = (state = defaultState, action) => {
switch (action.type) {
case 'LINE_DRAWN':
return { ...state, lines: [...state.lines, action.line] }
case 'DRAWING_SYNC_SUCCESS':
return { ...state, lines: [], drawingSyncSuccess: true, drawingSyncFailed: false }
case 'DRAWING_SYNC_FAILED':
return { ...state, drawingSyncFailed: true, drawingSyncSuccess: false }
default:
return state;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment