All files / src/utils/components/dialogs FileUpload.jsx

96.55% Statements 28/29
87.5% Branches 7/8
100% Functions 10/10
96.42% Lines 27/28

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137                      34x 34x 34x     34x   34x 1x 1x     34x 2x 2x     34x 1x 1x     34x 2x 2x 2x     34x 5x     34x 2x   2x 2x 2x           34x             9x                                                                                                             2x                                        
import { useTheme } from "@emotion/react";
import { Box, Input, Typography } from "@mui/material";
import Button from "@mui/material/Button";
import Dialog from "@mui/material/Dialog";
import DialogActions from "@mui/material/DialogActions";
import DialogContent from "@mui/material/DialogContent";
import DialogTitle from "@mui/material/DialogTitle";
import { useState } from "react";
import CloudUploadIcon from "@mui/icons-material/CloudUpload";
 
export default function FileUpload({ setUploadedFiles, title }) {
    const [showDialog, setShowDialog] = useState(false);
    const [selectedFile, setSelectedFile] = useState(null);
    const [dragging, setDragging] = useState(false);
    // const [uploadedFiles, setUploadedFiles] = useState([]);
 
    const theme = useTheme();
 
    const handleDragOver = (event) => {
        event.preventDefault();
        setDragging(true);
    };
 
    const handleDragEnter = (event) => {
        event.preventDefault();
        setDragging(true);
    };
 
    const handleDragLeave = (event) => {
        event.preventDefault();
        setDragging(false);
    };
 
    const handleDrop = (event) => {
        event.preventDefault();
        setDragging(false);
        setSelectedFile(event.dataTransfer.files[0]);
    };
 
    const handleFileChange = (event) => {
        setSelectedFile(event.target.files[0]);
    };
 
    const handleUpload = () => {
        if (selectedFile) {
            // console.log("File uploaded:", selectedFile);
            setUploadedFiles((currentArray) => [...currentArray, selectedFile]);
            setSelectedFile(null);
            setShowDialog(false);
        } else E{
            console.log("Please select a file to upload.");
        }
    };
 
    return (
        <>
            <Button
                id="button-file-upload"
                variant="outlined"
                fullWidth
                onClick={() => {
                    setShowDialog(true);
                }}
                startIcon={<CloudUploadIcon />}
            >
                {title || "File Upload"}
            </Button>
            <Dialog
                open={showDialog}
                aria-labelledby="alert-dialog-title"
                aria-describedby="alert-dialog-description"
                fullWidth
            >
                <DialogTitle id="alert-dialog-title">File Upload</DialogTitle>
                <DialogContent dividers>
                    <Box
                        className={`file-upload ${dragging ? "dragging" : ""}`}
                        onDragOver={handleDragOver}
                        onDragEnter={handleDragEnter}
                        onDragLeave={handleDragLeave}
                        onDrop={handleDrop}
                        sx={{
                            border: `2px dashed ${theme.palette.primary.main}`,
                            padding: 2,
                            display: "flex",
                            alignItems: "center",
                            justifyContent: "center",
                        }}
                    >
                        <Box>
                            <Input
                                id="file-upload"
                                type="file"
                                onChange={handleFileChange}
                                style={{ display: "none" }}
                            />
                            <label htmlFor="file-upload">
                                {selectedFile ? (
                                    <Typography variant="body1" color="black">
                                        {selectedFile.name}
                                    </Typography>
                                ) : (
                                    <Button id="file-upload-button-upload" variant="text" component="span">
                                        Click to Upload or Drag & Drop File
                                    </Button>
                                )}
                            </label>
                        </Box>
                    </Box>
                </DialogContent>
                <DialogActions sx={{ display: "flex", justifyContent: "space-between" }}>
                    <Button
                        id="file-upload-button-cancel"
                        sx={{ width: "150px" }}
                        variant="inherit"
                        onClick={() => {
                            setShowDialog(!showDialog);
                        }}
                    >
                        Cancel
                    </Button>
                    <Button
                        id="file-upload-button-save"
                        disabled={!selectedFile}
                        sx={{ width: "150px" }}
                        variant="contained"
                        color="primary"
                        onClick={handleUpload}
                    >
                        Save
                    </Button>
                </DialogActions>
            </Dialog>
        </>
    );
}