Last active
June 16, 2019 17:57
-
-
Save jsmanifest/3f04c9577911687e865b8ad9b97ecbcb 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
// Sets the next file when it detects that its ready to go | |
useEffect(() => { | |
if (state.pending.length && state.next == null) { | |
const next = state.pending[0] | |
dispatch({ type: 'next', next }) | |
} | |
}, [state.next, state.pending]) | |
const countRef = useRef(0) | |
// Processes the next pending thumbnail when ready | |
useEffect(() => { | |
if (state.pending.length && state.next) { | |
const { next } = state | |
api | |
.uploadFile(next) | |
.then(() => { | |
const prev = next | |
logUploadedFile(++countRef.current) | |
const pending = state.pending.slice(1) | |
dispatch({ type: 'file-uploaded', prev, pending }) | |
}) | |
.catch((error) => { | |
console.error(error) | |
dispatch({ type: 'set-upload-error', error }) | |
}) | |
} | |
}, [state]) | |
// Ends the upload process | |
useEffect(() => { | |
if (!state.pending.length && state.uploading) { | |
dispatch({ type: 'files-uploaded' }) | |
} | |
}, [state.pending.length, state.uploading]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment