File size: 978 Bytes
057576a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
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
  };
};