Skip to content

Instantly share code, notes, and snippets.

@jimjeffers
Last active April 4, 2020 19:56
Show Gist options
  • Save jimjeffers/dbbdd3fa0f8b4c14645c9b62089f7a90 to your computer and use it in GitHub Desktop.
Save jimjeffers/dbbdd3fa0f8b4c14645c9b62089f7a90 to your computer and use it in GitHub Desktop.
// Track a completed download and use an effect to update the status
// of the associated upload via the latest component state.
const [finishedDownload, setFinishedDownload] = useState(null as string | null)
useEffect(() => {
if (!finishedDownload) { return }
const updatedPreview = localPreviews[finishedDownload]
if (localPreviews[finishedDownload].status !== FileUploadStatus.Available) {
const normalizedPreviews = mergePreviewsToState([{ ...updatedPreview, status: FileUploadStatus.Available }], localPreviews)
setLocalPreviews(normalizedPreviews)
const save = async () => {
await savePreviews(normalizedPreviews)
}
save()
}
}, [finishedDownload, localPreviews, setLocalPreviews])
const handleCompletedUploaded: HandleFileUploadFn = preview => {
setFinishedDownload(preview.id)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment