Created
November 15, 2020 14:40
-
-
Save noobling/08649126ba160cdd0dfbaa530b3e365a 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 uploadFiles = async () => { | |
setUploading(true); | |
const promises = filesToUpload | |
.map(async (file) => { | |
const { data } = await getSignedUrlForProject({ | |
variables: { | |
projectId: projectId, | |
fileName: file.name, | |
}, | |
}); | |
const url = data?.getSignedUrlForProject?.url; | |
if (!url) { | |
throw new Error('Failed to get signed url'); | |
} | |
// TODO: throw an error if fetch is not 200 OK | |
await fetch(url, { | |
method: 'PUT', | |
body: file, | |
headers: { 'Content-Type': 'application/octet-stream' }, | |
}); | |
}) | |
// TODO: give feedback for a completed upload | |
.map((p) => p.then((res) => res).catch((e) => e)); | |
const result = await Promise.all(promises); | |
const uploadedFiles = filesToUpload.filter((_, fileIndex) => { | |
if (result[fileIndex] instanceof Error) { | |
alert(`Failed to upload ${filesToUpload[fileIndex].name}`); | |
return false; | |
} | |
return true; | |
}); | |
setUploading(false); | |
setFilesToUpload([]); | |
if (handleSuccess) { | |
handleSuccess( | |
uploadedFiles.map((file) => { | |
const fileSplit = file.name.split('.'); | |
fileSplit.pop(); | |
return fileSplit.join('.'); | |
}), | |
); | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment