File size: 2,064 Bytes
dcb6c5f
 
819bacd
 
 
dcb6c5f
819bacd
47b5f0c
819bacd
 
 
 
 
 
 
dcb6c5f
47b5f0c
819bacd
 
 
 
 
 
 
dcb6c5f
 
 
 
d34ec57
 
 
 
dcb6c5f
d34ec57
dcb6c5f
819bacd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import postDocument from "@/services/api/basePostDocument";
import FileUploadIcon from "@mui/icons-material/FileUpload";
import { Button, Typography, useTheme } from "@mui/material";
import Grid from "@mui/material/Unstable_Grid2";
import { enqueueSnackbar } from "notistack";
import { useRef } from "react";

export default function UploadFile({ fetchChunks }) {
  const theme = useTheme();
  const fileInputRef = useRef(null);

  const openExplorer = () => {
    fileInputRef.current.click();
  };

  const onHandleFileChange = async (event) => {
    const url = "/document/upload_file";
    const uploadedfile = event.target.files[0];

    if (!uploadedfile) return;

    const formData = new FormData();
    formData.append("file", uploadedfile);

    try {
      await postDocument(url, formData);
      await fetchChunks();
    } catch (error) {
      enqueueSnackbar(
        "Error uploading file: " +
        error.detail || error.message, 
        {
        variant: "error",
        });
    }
  };

  return (
    <Grid
      container
      border={1}
      borderColor={theme.palette.border.default}
      borderRadius={2}
      p={1}
      sx={{
        backgroundColor: theme.palette.background.default,
      }}
    >
      <Button fullWidth sx={{ textTransform: "none" }} onClick={openExplorer}>
        <Grid
          container
          xs={12}
          alignItems="center"
          justifyContent="space-between"
          border={1}
          borderColor={theme.palette.divider}
          borderRadius={1}
          p={1}
          sx={{
            backgroundColor: "white",
          }}
        >
          <input
            type="file"
            ref={fileInputRef}
            style={{ display: "none" }}
            onChange={onHandleFileChange}
            accept=".pdf"
          />
          <Grid item>
            <Typography variant="h6" color="black">
              Upload File
            </Typography>
          </Grid>
          <FileUploadIcon sx={{ color: "black" }} />
        </Grid>
      </Button>
    </Grid>
  );
}