This repository was archived by the owner on Oct 4, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 43
/
Copy pathImageSelectionButton.js
154 lines (142 loc) · 4.11 KB
/
ImageSelectionButton.js
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
import { useRef, useState } from 'react'
import { Button, ButtonType, IconPencil } from '@audius/stems'
import cn from 'classnames'
import PropTypes from 'prop-types'
import ReactDropzone from 'react-dropzone'
import InvalidFileType from 'components/upload/InvalidFileType'
import { ALLOWED_IMAGE_FILE_TYPES } from 'utils/imageProcessingUtil'
import styles from './ImageSelectionButton.module.css'
import ImageSelectionPopup from './ImageSelectionPopup'
import { ImageSelectionProps, ImageSelectionDefaults } from './PropTypes'
const messages = {
add: 'Add',
change: 'Change',
remove: 'Remove'
}
const ImageSelectionButton = ({
anchorRef: anchorRefProp,
wrapperClassName,
buttonClassName,
hasImage,
imageName,
error,
includePopup,
onOpenPopup,
onClosePopup,
onClick,
onAfterClose,
onSelect,
source,
defaultPopupOpen = false,
isImageAutogenerated,
onRemoveArtwork
}) => {
const anchorRefInner = useRef()
const [showModal, setShowModal] = useState(defaultPopupOpen)
const closeModal = () => {
setShowModal(false)
if (onClosePopup) onClosePopup()
}
const openModal = () => {
setShowModal(true)
if (onOpenPopup) onOpenPopup()
}
const handleClick = (e) => {
// if it's a keyboard event, the event detail is 0
// ignore the default click event in that case
// note that trying "e instanceof KeyboardEvent" did not work here
if (e.detail === 0) {
e.preventDefault()
}
if (hasImage && isImageAutogenerated === false) {
onRemoveArtwork()
} else if (!showModal) {
onClick()
openModal()
}
}
let buttonText =
hasImage && isImageAutogenerated === false
? messages.remove
: hasImage
? messages.change
: messages.add
if (imageName) buttonText += ` ${imageName}`
return (
<div className={cn(styles.wrapper, wrapperClassName)}>
{includePopup ? (
<>
<Button
ref={anchorRefProp ? undefined : anchorRefInner}
className={cn(styles.button, buttonClassName, {
[styles.hide]: showModal
})}
text={buttonText}
leftIcon={<IconPencil />}
type={ButtonType.WHITE}
onClick={handleClick}
buttonType='button'
/>
<ImageSelectionPopup
anchorRef={anchorRefProp ?? anchorRefInner}
className={styles.popup}
error={error}
isVisible={showModal}
onSelect={onSelect}
onClose={closeModal}
onAfterClose={onAfterClose}
source={source}
/>
</>
) : (
<>
<ReactDropzone
onDrop={onSelect}
className={styles.dropzone}
accept={ALLOWED_IMAGE_FILE_TYPES.join(', ')}
data-testid='upload-photo-dropzone'
>
<Button
className={cn(styles.button, styles.noPopup, {
[styles.hide]: hasImage
})}
text={buttonText}
leftIcon={<IconPencil />}
type={ButtonType.WHITE}
onClick={handleClick}
buttonType='button'
/>
</ReactDropzone>
{error ? (
<InvalidFileType className={styles.invalidFileType} />
) : null}
</>
)}
</div>
)
}
ImageSelectionButton.propTypes = {
anchorRef: PropTypes.any,
wrapperClassName: PropTypes.string,
buttonClassName: PropTypes.string,
hasImage: PropTypes.bool.isRequired,
// The name of the image (e.g. render the button as Add "Artwork" or Add "Cover Photo")
imageName: PropTypes.string,
// Whether or not to show the image selection modal. Otherwise, the
// button itself is the dropzone.
includePopup: PropTypes.bool,
onOpenPopup: PropTypes.func,
onClosePopup: PropTypes.func,
onClick: PropTypes.func,
defaultPopupOpen: PropTypes.bool,
isImageAutogenerated: PropTypes.bool,
onRemoveArtwork: PropTypes.bool,
...ImageSelectionProps
}
ImageSelectionButton.defaultProps = {
hasImage: false,
includePopup: true,
onClick: () => {},
...ImageSelectionDefaults
}
export default ImageSelectionButton