-
Notifications
You must be signed in to change notification settings - Fork 68
Expand file tree
/
Copy pathindex.js
More file actions
189 lines (173 loc) · 5.06 KB
/
index.js
File metadata and controls
189 lines (173 loc) · 5.06 KB
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
import React, { useState, useCallback } from 'react'
import Upload from 'antd/es/upload'
import { useDispatch, useSelector } from 'react-redux'
import { Box, Typography, CircularProgress } from '@mui/material'
import DeleteIcon from '@mui/icons-material/Delete'
import VisibilityIcon from '@mui/icons-material/Visibility'
import { styled } from '@mui/material/styles'
import * as AuthSelectors from 'reducers/auth/selectors'
import * as SnackbarActions from 'reducers/snackbar/actions'
const EmptyWrapper = styled('div')({
position: 'absolute',
top: 0,
left: 0,
width: '100%',
height: '100%',
display: 'flex',
justifyContent: 'center',
alignItems: 'center',
})
const ButtonOverlay = styled('div')({
position: 'absolute',
top: 0,
left: 0,
width: '100%',
height: '100%',
background: 'rgba(0,0,0,0.6)',
opacity: 0,
display: 'flex',
flexDirection: 'column',
justifyContent: 'center',
alignItems: 'center',
transition: 'opacity 0.2s ease',
'&:hover': {
opacity: 1,
},
color: 'white',
})
const ImageButton = styled('div')({
padding: '8px',
display: 'flex',
alignItems: 'center',
gap: '16px',
'&:hover': {
background: 'rgba(255,255,255,0.2)',
},
})
const ImageUpload = ({
value,
onChange,
uploadUrl,
resizeMode = 'contain',
}) => {
const dispatch = useDispatch()
const idToken = useSelector(AuthSelectors.getIdToken)
const [loading, setLoading] = useState(false)
if (!uploadUrl) {
throw new Error('ImageUpload component must be supplied an upload url')
}
const beforeUpload = useCallback(
file => {
const isJpgOrPng =
file.type === 'image/jpeg' || file.type === 'image/png'
if (!isJpgOrPng) {
dispatch(
SnackbarActions.error('Please upload a .jpg or .png file'),
)
}
const isLt2M = file.size / 1024 / 1024 < 2
if (!isLt2M) {
dispatch(
SnackbarActions.error(
'Upload size cannot be more than 2MB',
),
)
}
return isJpgOrPng && isLt2M
},
[dispatch],
)
const handleChange = useCallback(
info => {
if (info.file.status === 'uploading') {
setLoading(true)
return
}
if (info.file.status === 'done') {
onChange(info.file.response)
setLoading(false)
}
if (info.file.status === 'error') {
const message =
info?.file?.response?.message ??
'Something went wrong... Please try again'
dispatch(SnackbarActions.error(message))
setLoading(false)
}
},
[dispatch, onChange],
)
const handleRemove = useCallback(
e => {
e.stopPropagation()
onChange()
},
[onChange],
)
const renderLoading = () => (
<EmptyWrapper>
<CircularProgress size={24} />
</EmptyWrapper>
)
const renderEmpty = () => (
<EmptyWrapper>
<Typography>Click or drag a file to upload</Typography>
</EmptyWrapper>
)
const renderImage = () => (
<>
<Box
component="img"
sx={{
position: 'absolute',
top: 0,
left: 0,
width: '100%',
height: '100%',
objectFit: resizeMode,
}}
src={value.url}
alt="upload"
/>
<ButtonOverlay>
<ImageButton onClick={handleRemove}>
<DeleteIcon />
<Typography variant="button">Remove image</Typography>
</ImageButton>
<ImageButton onClick={() => window.open(value.url, '_blank')}>
<VisibilityIcon />
<Typography variant="button">View original</Typography>
</ImageButton>
</ButtonOverlay>
</>
)
return (
<Box
sx={{
backgroundColor: '#f7fafc',
width: '100%',
height: '100%',
position: 'relative',
cursor: 'pointer',
userSelect: 'none',
}}
>
<Upload
name="image"
listType="picture"
showUploadList={false}
action={uploadUrl}
headers={{
Authorization: `Bearer ${idToken}`,
}}
beforeUpload={beforeUpload}
onChange={handleChange}
>
{loading && renderLoading()}
{value && renderImage()}
{!loading && !value && renderEmpty()}
</Upload>
</Box>
)
}
export default ImageUpload