Last active
May 7, 2019 16:54
-
-
Save hendrikswan/e61bd672bc6a78f3d7a5f650227ede51 to your computer and use it in GitHub Desktop.
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
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