File size: 5,150 Bytes
9aee46b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
207f9e0
 
4971aea
207f9e0
4971aea
9aee46b
 
 
 
4971aea
9aee46b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
96aa590
207f9e0
96aa590
207f9e0
 
96aa590
 
 
9aee46b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
import React, { useState, useRef } from 'react';
import { 
  Paper, 
  Typography, 
  Box, 
  Button, 
  IconButton 
} from '@material-ui/core';
import CloudUploadIcon from '@material-ui/icons/CloudUpload';
import DeleteIcon from '@material-ui/icons/Delete';
import { makeStyles } from '@material-ui/core/styles';

const useStyles = makeStyles((theme) => ({
  paper: {
    padding: theme.spacing(2),
    display: 'flex',
    flexDirection: 'column',
    alignItems: 'center',
    height: '100%',
    minHeight: 300,
    transition: 'all 0.3s ease'
  },
  dragActive: {
    border: '2px dashed #3f51b5',
    backgroundColor: 'rgba(63, 81, 181, 0.05)'
  },
  dragInactive: {
    border: '2px dashed #ccc',
    backgroundColor: 'white'
  },
  uploadBox: {
    display: 'flex',
    flexDirection: 'column',
    alignItems: 'center',
    justifyContent: 'center',
    height: '100%',
    width: '100%',
    cursor: 'pointer'
  },
  uploadIcon: {
    fontSize: 60,
    color: '#3f51b5',
    marginBottom: theme.spacing(2)
  },
  supportText: {
    marginTop: theme.spacing(2)
  },
  previewBox: {
    display: 'flex',
    flexDirection: 'column',
    alignItems: 'center',
    width: '100%',
    height: '100%',
    position: 'relative'
  },
  imageContainer: {
    position: 'relative',
    width: '100%',
    // Use a smaller viewport-based height so any aspect ratio fits inside
    height: '45vh',
    [theme.breakpoints.down('sm')]: {
      height: '35vh',
    },
    display: 'flex',
    justifyContent: 'center',
    alignItems: 'center',
    overflow: 'hidden',
    marginTop: theme.spacing(2),
  },
  deleteButton: {
    position: 'absolute',
    top: 0,
    right: 0,
    backgroundColor: 'rgba(255, 255, 255, 0.7)',
    '&:hover': {
      backgroundColor: 'rgba(255, 255, 255, 0.9)',
    }
  }
}));

const ImageUploader = ({ onImageUpload }) => {
  const classes = useStyles();
  const [previewUrl, setPreviewUrl] = useState(null);
  const [dragActive, setDragActive] = useState(false);
  const fileInputRef = useRef(null);

  const handleDrag = (e) => {
    e.preventDefault();
    e.stopPropagation();
    if (e.type === 'dragenter' || e.type === 'dragover') {
      setDragActive(true);
    } else if (e.type === 'dragleave') {
      setDragActive(false);
    }
  };

  const handleDrop = (e) => {
    e.preventDefault();
    e.stopPropagation();
    setDragActive(false);
    if (e.dataTransfer.files && e.dataTransfer.files[0]) {
      handleFiles(e.dataTransfer.files[0]);
    }
  };

  const handleChange = (e) => {
    e.preventDefault();
    if (e.target.files && e.target.files[0]) {
      handleFiles(e.target.files[0]);
    }
  };

  const handleFiles = (file) => {
    if (file.type.startsWith('image/')) {
      setPreviewUrl(URL.createObjectURL(file));
      onImageUpload(file);
    } else {
      alert('Please upload an image file');
    }
  };

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

  const handleRemoveImage = () => {
    setPreviewUrl(null);
    onImageUpload(null);
    fileInputRef.current.value = "";
  };

  return (
    <Paper 
      className={`${classes.paper} ${dragActive ? classes.dragActive : classes.dragInactive}`}
      onDragEnter={handleDrag}
      onDragLeave={handleDrag}
      onDragOver={handleDrag}
      onDrop={handleDrop}
    >
      <input
        ref={fileInputRef}
        type="file"
        accept="image/*"
        onChange={handleChange}
        style={{ display: 'none' }}
      />

      {!previewUrl ? (
        <Box 
          className={classes.uploadBox}
          onClick={onButtonClick}
        >
          <CloudUploadIcon className={classes.uploadIcon} />
          <Typography variant="h6" gutterBottom>
            Drag & Drop an image here
          </Typography>
          <Typography variant="body2" color="textSecondary" gutterBottom>
            or
          </Typography>
          <Button
            variant="contained"
            color="primary"
            component="span"
            startIcon={<CloudUploadIcon />}
          >
            Browse Files
          </Button>
          <Typography variant="body2" color="textSecondary" className={classes.supportText}>
            Supported formats: JPG, PNG, GIF
          </Typography>
        </Box>
      ) : (
        <Box className={classes.previewBox}>
          <Typography variant="h6" gutterBottom>
            Preview
          </Typography>
          <Box className={classes.imageContainer}>
            <img
              src={previewUrl}
              alt="Preview"
              className="preview-image"
              style={{
                maxWidth: '100%',
                maxHeight: '100%',
                width: 'auto',
                height: 'auto',
                objectFit: 'contain',
                display: 'block'
              }}
            />
            <IconButton
              aria-label="delete"
              className={classes.deleteButton}
              onClick={handleRemoveImage}
            >
              <DeleteIcon />
            </IconButton>
          </Box>
        </Box>
      )}
    </Paper>
  );
};

export default ImageUploader;