-
Notifications
You must be signed in to change notification settings - Fork 95
/
Copy pathFormBuilderModal.js
231 lines (208 loc) · 5.69 KB
/
FormBuilderModal.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
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
import React, { Component } from 'react';
import i18n from 'i18n';
import { Modal, ModalHeader } from 'reactstrap';
import FormBuilderLoader from 'containers/FormBuilderLoader/FormBuilderLoader';
import castStringToElement from 'lib/castStringToElement';
import classnames from 'classnames';
import PropTypes from 'prop-types';
const noop = () => null;
class FormBuilderModal extends Component {
constructor(props) {
super(props);
this.handleSubmit = this.handleSubmit.bind(this);
this.handleHide = this.handleHide.bind(this);
this.clearResponse = this.clearResponse.bind(this);
this.handleLoadingError = this.handleLoadingError.bind(this);
}
/**
* Defines the form part of the Modal
*
* @returns {Component}
*/
getForm() {
if (!this.props.schemaUrl) {
return null;
}
return (
<FormBuilderLoader
fieldHolder={{ className: classnames('modal-body', this.props.bodyClassName) }}
actionHolder={{ className: 'modal-footer' }}
autoFocus={this.props.autoFocus}
schemaUrl={this.props.schemaUrl}
onSubmit={this.handleSubmit}
onAction={this.props.onAction}
onLoadingError={this.handleLoadingError}
identifier={this.props.identifier}
/>
);
}
/**
* Generates the response part of the Modal
*
* @returns {Component}
*/
getResponse() {
if (!this.state || !this.state.response) {
return null;
}
let className = '';
if (this.state.error) {
className = this.props.responseClassBad;
} else {
className = this.props.responseClassGood;
}
return (
<div className={className}>
{ castStringToElement('span', { html: this.state.response }) }
</div>
);
}
/**
* Removes the response from the state
*/
clearResponse() {
this.setState({
response: null,
});
}
handleLoadingError(schema) {
const providesOnLoadingError = this.props.onLoadingError !== noop;
if (this.props.showErrorMessage || !providesOnLoadingError) {
const error = schema.errors && schema.errors[0];
this.setState({
response: error.value,
error: true,
});
}
if (providesOnLoadingError) {
this.props.onLoadingError(schema);
}
}
/**
* Call the callback for hiding this Modal
*/
handleHide() {
this.clearResponse();
if (typeof this.props.onClosed === 'function') {
this.props.onClosed();
}
}
/**
* Handle submitting the form in the Modal
*
* @param {Object} data
* @param {String} action
* @param {Function} submitFn The original submit function
* @returns {Promise}
*/
handleSubmit(data, action, submitFn) {
this.clearResponse();
let promise = null;
if (typeof this.props.onSubmit === 'function') {
promise = this.props.onSubmit(data, action, submitFn);
} else {
promise = submitFn();
}
if (promise) {
// do not want this as part of the main promise chain.
promise
.then((response) => {
if (response) {
this.setState({
response: response.message,
error: false,
});
}
return response;
})
.catch((errorPromise) => {
errorPromise.then((errorText) => {
this.setState({
response: errorText,
error: true,
});
});
});
} else {
throw new Error('Promise was not returned for submitting');
}
return promise;
}
renderHeader() {
let { title } = this.props;
if (title !== false) {
if (typeof title === 'object') {
// FormSchema title occasionaly contains html, only render text for modal title
const doc = new DOMParser().parseFromString(title.html, 'text/html');
title = doc.body.textContent || '';
}
return (
<ModalHeader toggle={this.handleHide}>{title}</ModalHeader>
);
}
if (this.props.showCloseButton === true && typeof this.props.onClosed === 'function') {
return (
<button
type="button"
className="close modal__close-button"
onClick={this.handleHide}
aria-label={i18n._t('Admin.CLOSE', 'Close')}
/>
);
}
return null;
}
render() {
const form = this.getForm();
const response = this.getResponse();
return (
<Modal
isOpen={this.props.isOpen}
toggle={this.handleHide}
className={this.props.className}
modalClassName={this.props.modalClassName}
size={this.props.size}
>
{this.renderHeader()}
{response}
{form}
{this.props.children}
</Modal>
);
}
}
FormBuilderModal.propTypes = {
autoFocus: PropTypes.bool,
isOpen: PropTypes.bool,
title: PropTypes.oneOfType([
PropTypes.string,
PropTypes.bool,
PropTypes.shape({ html: PropTypes.string })
]),
className: PropTypes.string,
bodyClassName: PropTypes.string,
modalClassName: PropTypes.string,
showCloseButton: PropTypes.bool,
size: PropTypes.string,
onClosed: PropTypes.func,
schemaUrl: PropTypes.string,
onSubmit: PropTypes.func,
onAction: PropTypes.func,
responseClassGood: PropTypes.string,
responseClassBad: PropTypes.string,
identifier: PropTypes.string,
// Ignored and assumed true if onLoadingError is unassigned
showErrorMessage: PropTypes.bool,
onLoadingError: PropTypes.func,
};
FormBuilderModal.defaultProps = {
showErrorMessage: false,
showCloseButton: true,
onLoadingError: noop,
isOpen: false,
title: null,
modalClassName: 'form-builder-modal',
responseClassGood: 'alert alert-success',
responseClassBad: 'alert alert-danger',
};
export default FormBuilderModal;