-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathindex.jsx
244 lines (227 loc) · 7.22 KB
/
index.jsx
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
import React, { useCallback, useState } from "react"
import { useDropzone } from "react-dropzone"
import { createTheme } from "@mui/material/styles"
import { CssBaseline, Box, Typography, IconButton } from "@mui/material"
import DeleteIcon from "@mui/icons-material/Delete"
import axios from "axios"
import { useSnackbar } from "../SnackbarContext"
import { useTranslation, Trans } from "react-i18next"
import useMediaQuery from "@mui/material/useMediaQuery"
import config from "../config.js"
const theme = createTheme()
const ImageUpload = ({ onImageUpload, settingsImages }) => {
const { t } = useTranslation()
const { showSnackbar } = useSnackbar()
const [images, setImages] = useState(settingsImages)
const [loading, setLoading] = useState(false)
const [progress, setProgress] = useState(0)
const isSmallDevice = useMediaQuery(theme.breakpoints.down("sm"))
const onDrop = useCallback(
(acceptedFiles, fileRejections) => {
if (fileRejections.length) {
const { errors } = fileRejections[0]
if (errors.length) {
showSnackbar(errors[0].message, "error")
return
}
}
const totalImages = images.length + acceptedFiles.length
if (totalImages > config.UPLOAD_LIMIT) {
showSnackbar(t("error.configuration.image_upload.max"), "error")
return
}
const newImages = acceptedFiles.map((file) => {
return Object.assign(file, {
preview: URL.createObjectURL(file),
imageName: file.name,
})
})
uploadImages(newImages)
},
[images, onImageUpload, showSnackbar],
)
const uploadImages = async (images) => {
const formData = new FormData()
images.forEach((image) => {
formData.append("file", image)
})
try {
setLoading(true)
const response = await axios.post(
`${config.SERVER_URL}/upload`,
formData,
{
headers: {
"Content-Type": "multipart/form-data",
},
onUploadProgress: (progressEvent) => {
const { loaded, total } = progressEvent
let percentCompleted = Math.floor((loaded * 100) / total)
setProgress(percentCompleted)
},
},
)
showSnackbar(response.data.message, "success")
const uploadedFiles = response.data.files
const uploadedImages = uploadedFiles.map((file) => ({
preview: file.url,
filename: file.filename,
}))
setImages(uploadedImages)
onImageUpload(uploadedImages)
} catch (error) {
const errorResponse = error?.response?.data
if (errorResponse) {
showSnackbar(errorResponse?.message, "error")
} else {
showSnackbar(t("error.server_connection"), "error")
}
console.error("Error uploading images:", error)
} finally {
setLoading(false)
}
}
const deleteImage = async (filename, isNotFound = false) => {
try {
if (isNotFound) {
const updatedImages = images.filter(
(image) => image.filename !== filename,
)
setImages(updatedImages)
onImageUpload(updatedImages)
} else {
const response = await axios.delete(
`${config.SERVER_URL}/uploads/${filename}`,
)
showSnackbar(response.data.message, "success")
// Update the state to remove the deleted image
const updatedImages = images.filter(
(image) => image.filename !== filename,
)
setImages(updatedImages)
onImageUpload(updatedImages)
}
} catch (error) {
if (error?.response?.data) {
showSnackbar(error.response.data.message, "error")
} else {
showSnackbar(t("error.server_connection"), "error")
}
console.error("Error deleting image:", error)
}
}
const handleImageError = (index) => {
const updatedImages = [...images]
updatedImages[index].isNotFound = true
setImages(updatedImages)
showSnackbar(t("error.image_not_found"), "error")
}
const handleRemoveImage = (index) => {
const imageToRemove = images[index]
if (imageToRemove && !imageToRemove.filename) {
if (imageToRemove.src) {
let parts = imageToRemove.src.split("/")
let filename = parts[parts.length - 1]
imageToRemove.filename = filename
}
}
if (imageToRemove && imageToRemove.filename) {
deleteImage(imageToRemove.filename, imageToRemove.isNotFound)
} else {
console.error(
"Error deleting image: imageToRemove or imageToRemove.filename is undefined",
)
}
}
const { getRootProps, getInputProps, isDragActive } = useDropzone({
onDrop,
accept: "image/*",
multiple: true,
maxFiles: config.UPLOAD_LIMIT,
})
return (
<>
<CssBaseline />
<Box
{...getRootProps()}
sx={{
border: "2px dashed #ccc",
padding: isSmallDevice ? "0.5rem" : "1rem",
textAlign: "center",
cursor: "pointer",
marginBottom: "1rem",
display: "flex",
alignItems: "center",
justifyContent: "center",
flexDirection: "column",
borderRadius: "4px",
minHeight: "200px",
width: isSmallDevice ? "auto" : "52vw",
}}
>
<input {...getInputProps()} data-testid="file-input" />
{isDragActive ? (
<Typography sx={{ fontSize: "14px", color: "rgb(117, 117, 117)" }}>
{t("configuration.image_upload.file_drop")}
</Typography>
) : (
<>
{loading ? (
<>
{progress > 0 && progress < 100 ? (
<>
<progress value={progress} max={100} />
<p>{progress}%</p>
</>
) : (
<div className="loading">{t("loading")}</div>
)}
</>
) : (
<Typography
sx={{ fontSize: "14px", color: "rgb(117, 117, 117)" }}
>
<Trans
i18nKey="configuration.image_upload.description"
values={{ maxImages: config.UPLOAD_LIMIT }}
/>
</Typography>
)}
</>
)}
</Box>
<Box display="flex" flexWrap="wrap" gap="1rem">
{images.map((image, index) => (
<Box
key={index}
position="relative"
display="inline-flex"
flexDirection="column"
alignItems="center"
>
<img
src={image.preview || image.src}
alt="preview"
onError={() => handleImageError(index)}
style={{
width: isSmallDevice ? "65px" : "82px",
height: isSmallDevice ? "65px" : "82px",
objectFit: "cover",
borderRadius: "4px",
marginBottom: "0.5rem",
}}
/>
<IconButton
size="small"
onClick={() => handleRemoveImage(index)}
sx={{ position: "absolute", top: "0", right: "0" }}
>
<DeleteIcon fontSize="small" />
</IconButton>
</Box>
))}
</Box>
</>
)
}
export default ImageUpload