Skip to content

Commit afc511e

Browse files
refactor: remove UNSAFE_ method in insert-document-dialog COMPASS-6139 (#3980)
* refactor: remove UNSAFE_ method in insert-document-dialog COMPASS-6139 * refactor: try with componentWillUnount
1 parent fb2c360 commit afc511e

File tree

1 file changed

+42
-35
lines changed

1 file changed

+42
-35
lines changed

packages/compass-crud/src/components/insert-document-dialog.tsx

Lines changed: 42 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ export type InsertDocumentDialogProps = InsertCSFLEWarningBannerProps & {
4848
insertMany: () => void;
4949
isOpen: boolean;
5050
message: string;
51-
mode: 'progress' | 'error';
51+
mode: 'modifying' | 'error';
5252
version: string;
5353
updateJsonDoc: (value: string | null) => void;
5454
jsonDoc: string;
@@ -61,8 +61,7 @@ export type InsertDocumentDialogProps = InsertCSFLEWarningBannerProps & {
6161
};
6262

6363
type InsertDocumentDialogState = {
64-
message: string;
65-
mode: 'progress' | 'error';
64+
insertInProgress: boolean;
6665
};
6766

6867
/**
@@ -81,47 +80,51 @@ class InsertDocumentDialog extends React.PureComponent<
8180
*/
8281
constructor(props: InsertDocumentDialogProps) {
8382
super(props);
84-
this.state = { message: this.props.message, mode: this.props.mode };
83+
this.state = { insertInProgress: false };
8584
this.invalidElements = [];
8685
}
8786

8887
/**
89-
* Handle the property updates and subscriptions to the document.
88+
* Handle subscriptions to the document.
9089
*
91-
* @param {Object} nextProps - The new properties.
90+
* @param {Object} prevProps - The previous properties.
9291
*/
93-
// TODO: COMPASS-5847 Remove deprecated react function usage.
94-
UNSAFE_componentWillReceiveProps(nextProps: InsertDocumentDialogProps) {
95-
const isMany = this.hasManyDocuments();
96-
97-
if (!isMany) {
98-
// When switching to Hadron Document View - reset the invalid elements list, which contains the
99-
// uuids of each element that current has BSON type cast errors.
100-
//
101-
// Subscribe to the validation errors for BSON types on the document.
102-
if (nextProps.isOpen && this.props.jsonView && !nextProps.jsonView) {
92+
componentDidUpdate(
93+
prevProps: InsertDocumentDialogProps,
94+
state: InsertDocumentDialogState
95+
) {
96+
if (this.props.isOpen && !this.hasManyDocuments()) {
97+
if (prevProps.jsonView && !this.props.jsonView) {
98+
// When switching to Hadron Document View.
99+
// Reset the invalid elements list, which contains the
100+
// uuids of each element that has BSON type cast errors.
103101
this.invalidElements = [];
104-
nextProps.doc.on(Element.Events.Invalid, this.handleInvalid);
105-
nextProps.doc.on(Element.Events.Valid, this.handleValid);
106-
// Closing the modal or switching back to jsonView.
107-
//
108-
// Remove the listeners to the BSON type validation errors in order to
109-
// clean up properly.
110-
} else if (
111-
(!nextProps.isOpen && this.props.isOpen && !this.props.jsonView) ||
112-
(nextProps.isOpen &&
113-
this.props.isOpen &&
114-
!this.props.jsonView &&
115-
nextProps.jsonView)
116-
) {
102+
// Subscribe to the validation errors for BSON types on the document.
103+
this.props.doc.on(Element.Events.Invalid, this.handleInvalid);
104+
this.props.doc.on(Element.Events.Valid, this.handleValid);
105+
} else {
106+
// When switching to JSON View.
107+
// Remove the listeners to the BSON type validation errors in order to clean up properly.
117108
this.props.doc.removeListener(
118109
Element.Events.Invalid,
119110
this.handleInvalid
120111
);
121112
this.props.doc.removeListener(Element.Events.Valid, this.handleValid);
122113
}
123114
}
124-
this.setState({ message: nextProps.message, mode: nextProps.mode });
115+
116+
if (state.insertInProgress) {
117+
this.setState({ insertInProgress: false });
118+
}
119+
}
120+
121+
componentWillUnount() {
122+
if (!this.hasManyDocuments()) {
123+
// When closing the modal.
124+
// Remove the listeners to the BSON type validation errors in order to clean up properly.
125+
this.props.doc.removeListener(Element.Events.Invalid, this.handleInvalid);
126+
this.props.doc.removeListener(Element.Events.Valid, this.handleValid);
127+
}
125128
}
126129

127130
/**
@@ -152,7 +155,7 @@ class InsertDocumentDialog extends React.PureComponent<
152155
* Handle the insert.
153156
*/
154157
handleInsert() {
155-
this.setState({ message: 'Inserting Document', mode: 'progress' });
158+
this.setState({ insertInProgress: true });
156159
if (this.hasManyDocuments()) {
157160
this.props.insertMany();
158161
} else {
@@ -254,11 +257,15 @@ class InsertDocumentDialog extends React.PureComponent<
254257
*/
255258
render() {
256259
const currentView = this.props.jsonView ? 'JSON' : 'List';
260+
const variant = this.state.insertInProgress ? 'info' : 'danger';
257261

258-
const message = this.hasErrors()
259-
? INSERT_INVALID_MESSAGE
260-
: this.state.message;
261-
const variant = this.state.mode === 'progress' ? 'info' : 'danger';
262+
let message = this.props.message;
263+
if (this.hasErrors()) {
264+
message = INSERT_INVALID_MESSAGE;
265+
}
266+
if (this.state.insertInProgress) {
267+
message = 'Inserting Document';
268+
}
262269

263270
return (
264271
<FormModal

0 commit comments

Comments
 (0)