Created
December 10, 2020 18:44
-
-
Save lsbyerley/07dc3e0c7b81a86e603eb67e66a26b8a to your computer and use it in GitHub Desktop.
This file contains 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
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