11import { without } from 'lodash' ;
2- import React , { useCallback , useEffect , useRef , useState } from 'react' ;
2+ import React , {
3+ useCallback ,
4+ useEffect ,
5+ useMemo ,
6+ useRef ,
7+ useState ,
8+ } from 'react' ;
39import type Document from 'hadron-document' ;
410import { Element } from 'hadron-document' ;
511import {
@@ -124,7 +130,7 @@ const InsertDocumentDialog: React.FC<InsertDocumentDialogProps> = ({
124130 jsonDoc,
125131 doc,
126132 isCommentNeeded,
127- error,
133+ error : _error ,
128134 ns,
129135 csfleState,
130136 track,
@@ -212,11 +218,13 @@ const InsertDocumentDialog: React.FC<InsertDocumentDialogProps> = ({
212218 // Subscribe to the validation errors for BSON types on the document.
213219 doc . on ( Element . Events . Invalid , handleInvalid ) ;
214220 doc . on ( Element . Events . Valid , handleValid ) ;
221+ doc . on ( Element . Events . Removed , handleValid ) ;
215222 } else {
216223 // When switching to JSON View.
217224 // Remove the listeners to the BSON type validation errors in order to clean up properly.
218225 doc . removeListener ( Element . Events . Invalid , handleInvalid ) ;
219226 doc . removeListener ( Element . Events . Valid , handleValid ) ;
227+ doc . removeListener ( Element . Events . Removed , handleValid ) ;
220228 }
221229 }
222230 } , [ isOpen , jsonView , doc , handleValid , handleInvalid , hasManyDocuments ] ) ;
@@ -227,14 +235,20 @@ const InsertDocumentDialog: React.FC<InsertDocumentDialogProps> = ({
227235 }
228236 } , [ insertInProgress ] ) ;
229237
238+ const docRef = useRef ( doc ) ;
230239 useEffect ( ( ) => {
231- if ( ! hasManyDocuments ( ) && doc ) {
232- // When closing the modal.
240+ if ( isOpen ) {
241+ docRef . current = doc ;
242+ return ;
243+ }
244+ // When closing the modal.
245+ if ( ! hasManyDocuments ( ) && docRef . current ) {
233246 // Remove the listeners to the BSON type validation errors in order to clean up properly.
234- doc . removeListener ( Element . Events . Invalid , handleInvalid ) ;
235- doc . removeListener ( Element . Events . Valid , handleValid ) ;
247+ docRef . current . removeListener ( Element . Events . Invalid , handleInvalid ) ;
248+ docRef . current . removeListener ( Element . Events . Valid , handleValid ) ;
249+ docRef . current . removeListener ( Element . Events . Removed , handleValid ) ;
236250 }
237- } ) ;
251+ } , [ isOpen , doc , handleInvalid , handleValid , hasManyDocuments ] ) ;
238252
239253 const handleInsert = useCallback ( ( ) => {
240254 setInsertInProgress ( true ) ;
@@ -267,13 +281,15 @@ const InsertDocumentDialog: React.FC<InsertDocumentDialogProps> = ({
267281 const currentView = jsonView ? 'JSON' : 'List' ;
268282 const variant = insertInProgress ? 'info' : 'danger' ;
269283
270- if ( hasErrors ( ) ) {
271- error = { message : INSERT_INVALID_MESSAGE } ;
272- }
273-
274- if ( insertInProgress ) {
275- error = { message : 'Inserting Document' } ;
276- }
284+ const error = useMemo ( ( ) => {
285+ if ( hasErrors ( ) ) {
286+ return { message : INSERT_INVALID_MESSAGE } ;
287+ }
288+ if ( insertInProgress ) {
289+ return { message : 'Inserting Document' } ;
290+ }
291+ return _error ;
292+ } , [ _error , hasErrors , insertInProgress ] ) ;
277293
278294 return (
279295 < FormModal
0 commit comments