Created
November 29, 2018 17:15
-
-
Save tuandinh0801/177642d902a74178a2b4330fa1455e77 to your computer and use it in GitHub Desktop.
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 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