-
Notifications
You must be signed in to change notification settings - Fork 196
Expand file tree
/
Copy pathindex.jsx
More file actions
174 lines (164 loc) · 4.69 KB
/
index.jsx
File metadata and controls
174 lines (164 loc) · 4.69 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
import React from 'react';
import PropTypes from 'prop-types';
import last from 'lodash/last';
import { useParams } from 'react-router-dom';
import { FormattedMessage, useIntl } from '@edx/frontend-platform/i18n';
import { FileUpload as FileUploadIcon } from '@openedx/paragon/icons';
import { getConfig } from '@edx/frontend-platform';
import {
Form,
Dropzone,
Image,
Hyperlink,
Card,
Icon,
IconButton,
} from '@openedx/paragon';
import { uploadAssets } from './data/api';
import messages from './messages';
const CourseUploadImage = ({
label,
customHelpText,
assetImagePath,
imageNameField,
assetImageField,
identifierFieldText,
showImageBodyText,
customInputPlaceholder,
disabled = false,
onChange,
}) => {
const { courseId } = useParams();
const intl = useIntl();
const imageAbsolutePath = () => new URL(assetImagePath, getConfig().LMS_BASE_URL);
const assetsUrl = () => new URL(`/assets/${courseId}`, getConfig().STUDIO_BASE_URL);
const handleChangeImageAsset = (path) => {
const assetPath = last(path.split('/'));
// If image path is entered directly, we need to strip the asset prefix
const imageName = last(assetPath.split('block@'));
onChange(path, assetImageField);
if (imageNameField) {
onChange(imageName, imageNameField);
}
};
const handleProcessUpload = async ({ fileData, handleError }) => {
try {
const response = await uploadAssets(courseId, fileData);
const url = response?.asset?.url;
if (url) {
handleChangeImageAsset(url);
}
} catch (error) {
handleError(error);
}
};
const inputComponent = assetImagePath ?
(
<div className="image-preview">
<Image
src={imageAbsolutePath().href}
alt={intl.formatMessage(messages.uploadImageDropzoneAlt)}
fluid
/>
</div>
) :
(
<>
<IconButton
isActive
src={FileUploadIcon}
iconAs={Icon}
variant="secondary"
alt={intl.formatMessage(messages.uploadImageDropzoneAlt)}
/>
<p>
{intl.formatMessage(messages.uploadImageDropzoneText, {
identifierFieldText,
})}
</p>
</>
);
const cardImageTextBody = assetImagePath ?
(
<span className="x-small text-gray-700">
<FormattedMessage
{...messages.uploadImageBodyFilled}
values={{
hyperlink: (
<Hyperlink
destination={assetsUrl().href}
target="_blank"
showLaunchIcon={false}
>
{intl.formatMessage(messages.uploadImageFilesAndUploads)}
</Hyperlink>
),
}}
/>
</span>
) :
(
<span className="x-small text-gray-700">
{intl.formatMessage(messages.uploadImageEmpty)}
</span>
);
return (
<Form.Group className="form-group-custom w-100">
<Form.Label>{label}</Form.Label>
<Card>
<Card.Body className="image-body">
<div style={disabled ? { pointerEvents: 'none' } : undefined}>
<Dropzone
onProcessUpload={handleProcessUpload}
inputComponent={inputComponent}
accept={{
'image/*': ['.png', '.jpeg'],
}}
disabled={disabled}
/>
</div>
{showImageBodyText && cardImageTextBody}
</Card.Body>
<Card.Divider />
<Card.Footer className="p-2.5">
<Form.Control
value={assetImagePath}
onChange={(e) => handleChangeImageAsset(e.target.value)}
placeholder={customInputPlaceholder
|| intl.formatMessage(messages.uploadImageInputPlaceholder, {
identifierFieldText,
})}
disabled={disabled}
/>
</Card.Footer>
</Card>
<Form.Control.Feedback>
{customHelpText
|| intl.formatMessage(messages.uploadImageHelpText, {
identifierFieldText,
})}
</Form.Control.Feedback>
</Form.Group>
);
};
CourseUploadImage.defaultProps = {
assetImagePath: '',
customHelpText: '',
imageNameField: '',
assetImageField: '',
showImageBodyText: false,
identifierFieldText: '',
customInputPlaceholder: '',
};
CourseUploadImage.propTypes = {
label: PropTypes.string.isRequired,
assetImagePath: PropTypes.string,
customHelpText: PropTypes.string,
imageNameField: PropTypes.string,
assetImageField: PropTypes.string,
showImageBodyText: PropTypes.bool,
identifierFieldText: PropTypes.string,
customInputPlaceholder: PropTypes.string,
onChange: PropTypes.func.isRequired,
};
export default CourseUploadImage;