1
- import React , { Fragment , useCallback , useState } from 'react' ;
1
+ import React , { Fragment , useCallback , useEffect , useState } from 'react' ;
2
2
import { Modal , useModal } from '@faceless-ui/modal' ;
3
3
import { Transforms } from 'slate' ;
4
4
import { ReactEditor , useSlate } from 'slate-react' ;
@@ -49,12 +49,14 @@ const insertRelationship = (editor, { value, relationTo, depth }) => {
49
49
ReactEditor . focus ( editor ) ;
50
50
} ;
51
51
52
- const RelationshipButton = ( { path } ) => {
52
+ const RelationshipButton : React . FC < { path : string } > = ( { path } ) => {
53
53
const { open, closeAll } = useModal ( ) ;
54
54
const editor = useSlate ( ) ;
55
55
const { serverURL, routes : { api } , collections } = useConfig ( ) ;
56
+ const [ renderModal , setRenderModal ] = useState ( false ) ;
56
57
const [ loading , setLoading ] = useState ( false ) ;
57
58
const [ hasEnabledCollections ] = useState ( ( ) => collections . find ( ( { admin : { enableRichTextRelationship } } ) => enableRichTextRelationship ) ) ;
59
+ const modalSlug = `${ path } -add-relationship` ;
58
60
59
61
const handleAddRelationship = useCallback ( async ( _ , { relationTo, value, depth } ) => {
60
62
setLoading ( true ) ;
@@ -64,9 +66,15 @@ const RelationshipButton = ({ path }) => {
64
66
65
67
insertRelationship ( editor , { value : json , depth, relationTo } ) ;
66
68
closeAll ( ) ;
69
+ setRenderModal ( false ) ;
70
+ setLoading ( false ) ;
67
71
} , [ editor , closeAll , api , serverURL ] ) ;
68
72
69
- const modalSlug = `${ path } -add-relationship` ;
73
+ useEffect ( ( ) => {
74
+ if ( renderModal ) {
75
+ open ( modalSlug ) ;
76
+ }
77
+ } , [ renderModal , open , modalSlug ] ) ;
70
78
71
79
if ( ! hasEnabledCollections ) return null ;
72
80
@@ -75,36 +83,41 @@ const RelationshipButton = ({ path }) => {
75
83
< ElementButton
76
84
className = { baseClass }
77
85
format = "relationship"
78
- onClick = { ( ) => open ( modalSlug ) }
86
+ onClick = { ( ) => setRenderModal ( true ) }
79
87
>
80
88
< RelationshipIcon />
81
89
</ ElementButton >
82
- < Modal
83
- slug = { modalSlug }
84
- className = { `${ baseClass } __modal` }
85
- >
86
- < MinimalTemplate >
87
- < header className = { `${ baseClass } __header` } >
88
- < h3 > Add Relationship</ h3 >
89
- < Button
90
- buttonStyle = "none"
91
- onClick = { closeAll }
90
+ { renderModal && (
91
+ < Modal
92
+ slug = { modalSlug }
93
+ className = { `${ baseClass } __modal` }
94
+ >
95
+ < MinimalTemplate >
96
+ < header className = { `${ baseClass } __header` } >
97
+ < h3 > Add Relationship</ h3 >
98
+ < Button
99
+ buttonStyle = "none"
100
+ onClick = { ( ) => {
101
+ closeAll ( ) ;
102
+ setRenderModal ( false ) ;
103
+ } }
104
+ >
105
+ < X />
106
+ </ Button >
107
+ </ header >
108
+ < Form
109
+ onSubmit = { handleAddRelationship }
110
+ initialData = { initialFormData }
111
+ disabled = { loading }
92
112
>
93
- < X />
94
- </ Button >
95
- </ header >
96
- < Form
97
- onSubmit = { handleAddRelationship }
98
- initialData = { initialFormData }
99
- disabled = { loading }
100
- >
101
- < Fields />
102
- < Submit >
103
- Add relationship
104
- </ Submit >
105
- </ Form >
106
- </ MinimalTemplate >
107
- </ Modal >
113
+ < Fields />
114
+ < Submit >
115
+ Add relationship
116
+ </ Submit >
117
+ </ Form >
118
+ </ MinimalTemplate >
119
+ </ Modal >
120
+ ) }
108
121
</ Fragment >
109
122
) ;
110
123
} ;
0 commit comments