forked from openedx/frontend-app-authoring
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUpdateForm.jsx
More file actions
142 lines (135 loc) · 4.75 KB
/
UpdateForm.jsx
File metadata and controls
142 lines (135 loc) · 4.75 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
import React from 'react';
import PropTypes from 'prop-types';
import {
ActionRow,
Button,
Form,
Icon,
} from '@openedx/paragon';
import classNames from 'classnames';
import DatePicker from 'react-datepicker';
import { useIntl } from '@edx/frontend-platform/i18n';
import { Calendar as CalendarIcon, Error as ErrorIcon } from '@openedx/paragon/icons';
import { Formik } from 'formik';
import {
convertToStringFromDate,
convertToDateFromString,
isValidDate,
} from '../../utils';
import { DATE_FORMAT, DEFAULT_EMPTY_WYSIWYG_VALUE } from '../../constants';
import { WysiwygEditor } from '../../generic/WysiwygEditor';
import { REQUEST_TYPES } from '../constants';
import { geUpdateFormSettings } from './utils';
import messages from './messages';
const UpdateForm = ({
close,
requestType,
onSubmit,
courseUpdatesInitialValues,
isInnerForm,
isFirstUpdate,
}) => {
const intl = useIntl();
const {
currentContent,
formTitle,
validationSchema,
contentFieldName,
submitButtonText,
} = geUpdateFormSettings(requestType, courseUpdatesInitialValues, intl);
return (
<div className={classNames('update-form', {
'update-form__inner': isInnerForm,
'update-form__inner-first': isFirstUpdate,
})}
>
<Formik
initialValues={courseUpdatesInitialValues}
validationSchema={validationSchema}
validateOnMount
validateOnBlur
onSubmit={onSubmit}
>
{({
values, handleSubmit, isValid, setFieldValue,
}) => (
<>
<h3 className="update-form-title">{formTitle}</h3>
{(requestType !== REQUEST_TYPES.edit_handouts) && (
<Form.Group className="mb-4 datepicker-field datepicker-custom">
<Form.Control.Feedback className="datepicker-float-labels">
{intl.formatMessage(messages.updateFormDate)}
</Form.Control.Feedback>
<div className="position-relative">
<Icon
src={CalendarIcon}
className="datepicker-custom-control-icon"
alt={intl.formatMessage(messages.updateFormCalendarAltText)}
/>
<DatePicker
name="date"
data-testid="course-updates-datepicker"
selected={isValidDate(values.date) ? convertToDateFromString(values.date) : undefined}
dateFormat={DATE_FORMAT}
className={classNames('datepicker-custom-control', {
'datepicker-custom-control_isInvalid': !isValid,
})}
autoComplete="off"
selectsStart
showPopperArrow={false}
onChange={(value) => {
if (!isValidDate(value)) {
return;
}
// eslint-disable-next-line @typescript-eslint/no-floating-promises
setFieldValue('date', convertToStringFromDate(value));
}}
/>
</div>
{!isValid && (
<div className="datepicker-field-error">
<Icon src={ErrorIcon} className="text-danger-500" alt={intl.formatMessage(messages.updateFormErrorAltText)} />
<span className="message-error">{intl.formatMessage(messages.updateFormInValid)}</span>
</div>
)}
</Form.Group>
)}
<Form.Group className="m-0 mb-3">
<WysiwygEditor
initialValue={currentContent}
data-testid="course-updates-wisiwyg-editor"
name={contentFieldName}
minHeight={300}
onChange={async (value) => {
await setFieldValue(contentFieldName, value || DEFAULT_EMPTY_WYSIWYG_VALUE);
}}
/>
</Form.Group>
<ActionRow>
<Button variant="tertiary" type="button" onClick={close}>
{intl.formatMessage(messages.cancelButton)}
</Button>
<Button onClick={handleSubmit} type="submit" disabled={!isValid}>
{submitButtonText}
</Button>
</ActionRow>
</>
)}
</Formik>
</div>
);
};
UpdateForm.defaultProps = {
isInnerForm: false,
isFirstUpdate: false,
};
UpdateForm.propTypes = {
// eslint-disable-next-line react/forbid-prop-types
courseUpdatesInitialValues: PropTypes.object.isRequired,
close: PropTypes.func.isRequired,
requestType: PropTypes.string.isRequired,
onSubmit: PropTypes.func.isRequired,
isInnerForm: PropTypes.bool,
isFirstUpdate: PropTypes.bool,
};
export default UpdateForm;