forked from folio-org/stripes-form
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStripesFormWrapper.js
95 lines (83 loc) · 2.14 KB
/
StripesFormWrapper.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
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import { withRouter } from 'react-router';
import { submit } from 'redux-form';
import StripesFormModal from './StripesFormModal';
class StripesFormWrapper extends Component {
constructor(props) {
super(props);
this.state = {
openModal: false,
nextLocation: null,
};
this.saveChanges = this.saveChanges.bind(this);
this.continue = this.continue.bind(this);
this.closeModal = this.closeModal.bind(this);
}
componentDidMount() {
if (this.props.formOptions.navigationCheck) {
this.unblock = this.props.history.block((nextLocation) => {
const shouldPrompt = this.props.dirty && !this.props.submitSucceeded;
if (shouldPrompt) {
this.setState({
openModal: true,
nextLocation,
});
}
return !shouldPrompt;
});
}
}
componentWillUnmount() {
if (this.props.formOptions.navigationCheck) {
this.unblock();
}
}
saveChanges() {
this.props.dispatch(submit(this.props.formOptions.form));
if (this.props.invalid) {
this.closeModal();
} else {
this.continue();
}
}
continue() {
this.unblock();
this.props.history.push(this.state.nextLocation.pathname);
}
closeModal() {
this.setState({
openModal: false,
});
}
render() {
return (
<div>
<this.props.Form {...this.props} />
<StripesFormModal
openWhen={this.state.openModal}
saveChanges={this.saveChanges}
discardChanges={this.continue}
remoteSave={this.props.formOptions.allowRemoteSave}
closeCB={this.closeModal}
/>
</div>
);
}
}
StripesFormWrapper.propTypes = {
formOptions: PropTypes.shape({
allowRemoteSave: PropTypes.bool,
navigationCheck: PropTypes.bool,
form: PropTypes.string,
}),
history: PropTypes.shape({
block: PropTypes.func,
push: PropTypes.func,
}),
dirty: PropTypes.bool,
dispatch: PropTypes.func,
invalid: PropTypes.bool,
submitSucceeded: PropTypes.bool,
};
export default withRouter(StripesFormWrapper);