11import { ChangeEvent , useState } from 'react' ;
22import { useFormContext } from 'react-hook-form' ;
33import { getPresignedUrl } from '@/api/events' ;
4+ import { createApi } from '@/api/utils/createApi' ;
5+ import { UploadType } from '@/model/events' ;
46import { useApi } from './useApi' ;
57import { useNotifyToast } from './useNotifyToast' ;
6- import { S3Client , PutObjectCommand , PutObjectCommandInput } from '@aws-sdk/client-s3' ;
78
89const MAX_FILE_UPLOAD_SIZE = 1e7 ; // 10MB
910
@@ -26,15 +27,15 @@ const isExtensionAllowed = (fileName: string) => {
2627 }
2728} ;
2829
29- export const useFileUpload = ( eventId : string , uploadType : string , onChange : ( key : string ) => void , name ?: string ) => {
30+ export const useFileUpload = ( eventId : string , uploadType : UploadType , onChange : ( key : string ) => void , name ?: string ) => {
3031 const api = useApi ( ) ;
3132 const { successToast, errorToast } = useNotifyToast ( ) ;
3233 const formContext = useFormContext ( ) ;
3334 const [ isUploading , setIsUploading ] = useState ( false ) ;
3435 const [ uploadProgress , setUploadProgress ] = useState ( 0 ) ;
3536
36- const getPresignedUrlTrigger = async ( entryId : string , fileName : string , fileType : string ) => {
37- const response = await api . execute ( getPresignedUrl ( entryId , fileName , fileType ) ) ;
37+ const getPresignedUrlTrigger = async ( entryId : string , file : File , uploadType : UploadType ) => {
38+ const response = await api . execute ( getPresignedUrl ( entryId , file . name , uploadType ) ) ;
3839 return response . data ;
3940 } ;
4041
@@ -77,33 +78,29 @@ export const useFileUpload = (eventId: string, uploadType: string, onChange: (ke
7778 return ;
7879 }
7980
80- setIsUploading ( true ) ;
81- await uploadFile ( selectedFileObj ) ;
82- setIsUploading ( false ) ;
83- setUploadProgress ( 0 ) ;
81+ try {
82+ setIsUploading ( true ) ;
83+ await uploadFile ( selectedFileObj ) ;
84+ } catch ( error ) {
85+ console . error ( error ) ;
86+ } finally {
87+ setIsUploading ( false ) ;
88+ setUploadProgress ( 0 ) ;
89+ }
8490 } ;
8591
8692 const uploadFile = async ( file : File ) => {
87- const s3Client = new S3Client ( {
88- region : 'ap-southeast-1' ,
89- credentials : {
90- accessKeyId : import . meta. env . VITE_AWS_ACCESS_KEY_ID ! ,
91- secretAccessKey : import . meta. env . VITE_AWS_SECRET_ACCESS_KEY !
92- }
93- } ) ;
93+ const { uploadLink, objectKey } = await getPresignedUrlTrigger ( eventId , file , uploadType ) ;
9494
95- const { objectKey } = await getPresignedUrlTrigger ( eventId , file . name , uploadType ) ;
96-
97- // Function to upload a file to S3
98- const uploadParams : PutObjectCommandInput = {
99- Bucket : import . meta. env . VITE_S3_BUCKET ! ,
100- Key : objectKey ,
101- Body : file
102- } ;
95+ const uploadApi = createApi ( {
96+ method : 'put' ,
97+ url : uploadLink ,
98+ headers : { 'Content-Type' : file . type } ,
99+ body : file
100+ } ) ;
103101
104102 try {
105- const command = new PutObjectCommand ( uploadParams ) ;
106- await s3Client . send ( command ) ;
103+ await api . execute ( uploadApi ) ;
107104 setUploadProgress ( 100 ) ;
108105
109106 successToast ( {
@@ -113,6 +110,7 @@ export const useFileUpload = (eventId: string, uploadType: string, onChange: (ke
113110
114111 onChange ( objectKey ) ;
115112 } catch ( err ) {
113+ setIsUploading ( false ) ;
116114 console . error ( 'Error' , err ) ;
117115 errorToast ( {
118116 title : 'File Upload Failed' ,
0 commit comments