Skip to content

Instantly share code, notes, and snippets.

@jsmanifest
Last active June 16, 2019 17:57
Show Gist options
  • Save jsmanifest/3f04c9577911687e865b8ad9b97ecbcb to your computer and use it in GitHub Desktop.
Save jsmanifest/3f04c9577911687e865b8ad9b97ecbcb to your computer and use it in GitHub Desktop.
// 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