Skip to content

Instantly share code, notes, and snippets.

@tuandinh0801
Created November 29, 2018 17:23
Show Gist options
  • Save tuandinh0801/ac8744ea5a1e458ce8d3339e523a9c56 to your computer and use it in GitHub Desktop.
Save tuandinh0801/ac8744ea5a1e458ce8d3339e523a9c56 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 drawingSyncFailure = () => ({
type: 'DRAWING_MOUSE_UP',
})
function loadDataEpic(action$) {
return action$
.ofType('LINE_DRAWN')
.bufferBy(action$.ofType('DRAWING_MOUSE_UP'))
.switchMap((lines) =>
fetch('/a/fake/url', {
method: 'post',
body: JSON.stringify(lines)
})
.then(response => response.ok ? drawingSyncSuccess() : drawingSyncFailure())
.catch(() => drawingSyncFailure())
)
}
const reducer = (state = {}, action) => {
switch (action.type) {
case 'DRAWING_SYNC_SUCCESS':
return { ...state, 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