forked from openedx/frontend-app-authoring
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTextbookForm.jsx
More file actions
201 lines (192 loc) · 8.23 KB
/
TextbookForm.jsx
File metadata and controls
201 lines (192 loc) · 8.23 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
190
191
192
193
194
195
196
197
198
199
200
201
import { useState } from 'react';
import PropTypes from 'prop-types';
import { useIntl } from '@edx/frontend-platform/i18n';
import { FieldArray, Formik } from 'formik';
import {
PictureAsPdf as PdfIcon,
Add as AddIcon,
DeleteOutline as DeleteIcon,
Upload as UploadIcon,
} from '@openedx/paragon/icons';
import {
ActionRow,
Button,
Form,
Icon,
IconButtonWithTooltip,
useToggle,
} from '@openedx/paragon';
import { getUploadFileMaxSize } from '@src/constants';
import FormikControl from '../../generic/FormikControl';
import PromptIfDirty from '../../generic/prompt-if-dirty/PromptIfDirty';
import ModalDropzone from '../../generic/modal-dropzone/ModalDropzone';
import { useModel } from '../../generic/model-store';
import textbookFormValidationSchema from './validations';
import messages from './messages';
const TextbookForm = ({
closeTextbookForm,
initialFormValues,
onSubmit,
onSavingStatus,
courseId,
}) => {
const intl = useIntl();
const courseDetail = useModel('courseDetails', courseId);
const courseTitle = courseDetail ? courseDetail?.name : '';
const [currentTextbookIndex, setCurrentTextbookIndex] = useState(0);
const [isUploadModalOpen, openUploadModal, closeUploadModal] = useToggle(false);
const [selectedFile, setSelectedFile] = useState('');
const onCloseUploadModal = () => {
closeUploadModal();
setSelectedFile('');
};
const onUploadButtonClick = (index) => {
setCurrentTextbookIndex(index);
openUploadModal();
};
return (
<div className="textbook-form" data-testid="textbook-form">
<Formik
initialValues={initialFormValues}
validationSchema={textbookFormValidationSchema(intl)}
onSubmit={onSubmit}
validateOnBlur
validateOnMount
>
{({
values, handleSubmit, isValid, dirty, setFieldValue,
}) => (
<>
<Form.Group size="sm" className="form-field">
<Form.Label size="sm" className="font-weight-bold form-main-label text-black">
{intl.formatMessage(messages.tabTitleLabel)} *
</Form.Label>
<FormikControl
name="tab_title"
value={values.tab_title}
placeholder={intl.formatMessage(messages.tabTitlePlaceholder)}
/>
<Form.Control.Feedback className="form-helper-text">
{intl.formatMessage(messages.tabTitleHelperText)}
</Form.Control.Feedback>
</Form.Group>
<FieldArray
name="chapters"
render={(arrayHelpers) => (
<>
{!!values?.chapters.length && values.chapters.map(({ title, url }, index) => (
<div className="form-chapters-fields" data-testid="form-chapters-fields">
<Form.Group size="sm" className="form-field">
<Form.Label size="sm" className="form-label font-weight-bold required text-black">
{intl.formatMessage(messages.chapterTitleLabel)} *
</Form.Label>
<FormikControl
name={`chapters[${index}].title`}
value={title}
placeholder={intl.formatMessage(messages.chapterTitlePlaceholder, { value: index + 1 })}
/>
<Form.Control.Feedback className="form-helper-text">
{intl.formatMessage(messages.chapterTitleHelperText)}
</Form.Control.Feedback>
</Form.Group>
<Form.Group size="sm" className="form-field">
<div className="d-flex align-items-center mb-1">
<Form.Label size="sm" className="font-weight-bold mb-0 text-black">
{intl.formatMessage(messages.chapterUrlLabel)} *
</Form.Label>
<IconButtonWithTooltip
size="sm"
className="ml-auto field-icon-button"
tooltipContent={intl.formatMessage(messages.uploadButtonTooltip)}
src={UploadIcon}
iconAs={Icon}
data-testid="chapter-upload-button"
alt={intl.formatMessage(messages.uploadButtonAlt)}
onClick={() => onUploadButtonClick(index)}
/>
<IconButtonWithTooltip
size="sm"
className="field-icon-button"
tooltipContent={intl.formatMessage(messages.deleteButtonTooltip)}
src={DeleteIcon}
iconAs={Icon}
data-testid="chapter-delete-button"
alt={intl.formatMessage(messages.deleteButtonAlt)}
onClick={() => arrayHelpers.remove(index)}
/>
</div>
<FormikControl
name={`chapters[${index}].url`}
value={url}
placeholder={intl.formatMessage(messages.chapterUrlPlaceholder)}
/>
<Form.Control.Feedback className="form-helper-text">
{intl.formatMessage(messages.chapterUrlHelperText)}
</Form.Control.Feedback>
</Form.Group>
</div>
))}
<div>
{!values.chapters.length && (
<Form.Control.Feedback className="pgn__form-text-invalid mb-2">
{intl.formatMessage(messages.addChapterHelperText)}
</Form.Control.Feedback>
)}
<Button
variant="outline-primary"
className="w-100"
iconBefore={AddIcon}
onClick={() => arrayHelpers.push({ title: '', url: '' })}
>
{intl.formatMessage(messages.addChapterButton)}
</Button>
</div>
</>
)}
/>
<ActionRow>
<Button variant="tertiary" onClick={closeTextbookForm} data-testid="cancel-button">
{intl.formatMessage(messages.cancelButton)}
</Button>
<Button onClick={handleSubmit} disabled={!isValid} type="submit">
{intl.formatMessage(messages.saveButton)}
</Button>
</ActionRow>
<ModalDropzone
isOpen={isUploadModalOpen}
onClose={onCloseUploadModal}
onCancel={onCloseUploadModal}
onChange={(value) => setFieldValue(`chapters[${currentTextbookIndex}].url`, value)}
fileTypes={['pdf']}
modalTitle={intl.formatMessage(messages.uploadModalTitle, { courseName: courseTitle })}
imageDropzoneText={intl.formatMessage(messages.uploadModalDropzoneText)}
imageHelpText={intl.formatMessage(messages.uploadModalHelperText)}
onSavingStatus={onSavingStatus}
invalidFileSizeMore={intl.formatMessage(
messages.uploadModalFileInvalidSizeText,
{ maxSize: getUploadFileMaxSize() / (1024 * 1024) },
)}
onSelectFile={setSelectedFile}
previewComponent={(
<div className="modal-preview">
<Icon src={PdfIcon} className="modal-preview-icon" />
<span className="modal-preview-text">{selectedFile}</span>
</div>
)}
maxSize={getUploadFileMaxSize()}
/>
<PromptIfDirty dirty={dirty} />
</>
)}
</Formik>
</div>
);
};
TextbookForm.propTypes = {
closeTextbookForm: PropTypes.func.isRequired,
initialFormValues: PropTypes.shape({}).isRequired,
onSubmit: PropTypes.func.isRequired,
onSavingStatus: PropTypes.func.isRequired,
courseId: PropTypes.string.isRequired,
};
export default TextbookForm;