ysn-rfd's picture
Upload 302 files
057576a verified
raw
history blame
978 Bytes
import { useState, useCallback } from 'react';
import { uploadService } from '../services/api';
export const useFileUpload = () => {
const [uploading, setUploading] = useState(false);
const [progress, setProgress] = useState(0);
const [error, setError] = useState(null);
const uploadFile = useCallback(async (file) => {
try {
setUploading(true);
setProgress(0);
setError(null);
const response = await uploadService.uploadFile(file, (progress) => {
setProgress(progress);
});
setUploading(false);
setProgress(100);
return response;
} catch (error) {
setError(error.error || 'File upload failed');
setUploading(false);
throw error;
}
}, []);
const reset = useCallback(() => {
setUploading(false);
setProgress(0);
setError(null);
}, []);
return {
uploadFile,
uploading,
progress,
error,
reset
};
};