Skip to content

Instantly share code, notes, and snippets.

@lsbyerley
Created December 10, 2020 18:44
Show Gist options
  • Save lsbyerley/07dc3e0c7b81a86e603eb67e66a26b8a to your computer and use it in GitHub Desktop.
Save lsbyerley/07dc3e0c7b81a86e603eb67e66a26b8a to your computer and use it in GitHub Desktop.
import { useState } from 'react';
import { put } from 'axios';
const useUploadFileToS3 = () => {
const [uploadProgress, setUploadProgress] = useState(0);
const [isUploading, setIsUploading] = useState(false);
const [isSuccessful, setIsSuccessful] = useState(false);
const uploadFileToS3 = (fileData, s3URL) => {
setIsUploading(true);
put(s3URL, fileData, {
onUploadProgress: (e) => {
if (e.lengthComputable) {
const percentInt = Math.round((e.loaded / e.total) * 100);
setUploadProgress(percentInt);
}
},
}).then((response) => {
setIsUploading(false);
if (response && response.status === 200) {
setIsSuccessful(true);
}
});
};
const reset = () => {
setUploadProgress(0);
setIsUploading(false);
setIsSuccessful(false);
};
return {
uploadFileToS3,
reset,
isUploading,
uploadProgress,
isSuccessful,
};
}
export default useUploadFileToS3;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment