@@ -40,10 +40,16 @@ import {
4040 applyAllPendingErrors ,
4141 hasSubfieldErrors ,
4242} from '../../edit/utility/subfieldErrors.js' ;
43- import ExternalLinksEditor , {
44- _ExternalLinksEditor ,
45- prepareExternalLinksHtmlFormSubmission ,
46- } from '../../external-links-editor/components/ExternalLinksEditor.js' ;
43+ import useExternalLinksEditor
44+ from '../../external-links-editor/hooks/useExternalLinksEditor.js' ;
45+ import {
46+ createInitialState as createExternalLinksEditorState ,
47+ reducer as externalLinksEditorReducer ,
48+ } from '../../external-links-editor/state.js' ;
49+ import type {
50+ LinksEditorActionT ,
51+ LinksEditorStateT ,
52+ } from '../../external-links-editor/types.js' ;
4753import {
4854 NonHydratedRelationshipEditorWrapper as RelationshipEditorWrapper ,
4955} from '../../relationship-editor/components/RelationshipEditorWrapper.js' ;
@@ -55,18 +61,27 @@ type ActionT =
5561 | { + type : 'show-all-pending-errors' }
5662 | { + type : 'toggle-type-bubble' }
5763 | { + type : 'update-date-range' , + action : DateRangeFieldsetActionT }
64+ | { + type : 'update-external-links-editor' , + action : LinksEditorActionT }
5865 | { + type : 'update-name' , + action : NameActionT } ;
5966/* eslint-enable ft-flow/sort-keys */
6067
6168type StateT = {
69+ + externalLinksEditor : LinksEditorStateT ,
6270 + form : EventFormT ,
6371 + guessCaseOptions : GuessCaseOptionsStateT ,
6472 + isGuessCaseOptionsOpen : boolean ,
6573 + showTypeBubble : boolean ,
6674} ;
6775
68- function createInitialState ( form : EventFormT ) {
76+ function createInitialState ( {
77+ $c,
78+ form,
79+ } : {
80+ + $c : SanitizedCatalystContextT ,
81+ + form : EventFormT ,
82+ } ) {
6983 return {
84+ externalLinksEditor : createExternalLinksEditorState ( $c ) ,
7085 form,
7186 guessCaseOptions : createGuessCaseOptionsState ( ) ,
7287 isGuessCaseOptionsOpen : false ,
@@ -111,6 +126,12 @@ function reducer(state: StateT, action: ActionT): StateT {
111126 . set ( 'guessCaseOptions' , nameState . guessCaseOptions )
112127 . set ( 'isGuessCaseOptionsOpen' , nameState . isGuessCaseOptionsOpen ) ;
113128 }
129+ { type: 'update-external-links-editor' , const action } => {
130+ newStateCtx . set (
131+ 'externalLinksEditor' ,
132+ externalLinksEditorReducer ( state . externalLinksEditor , action ) ,
133+ ) ;
134+ }
114135 { type : 'toggle-type-bubble' } = > {
115136 newStateCtx . set ( 'showTypeBubble' , true ) ;
116137 }
@@ -153,7 +174,7 @@ component EventEditForm(
153174
154175 const [ state , dispatch ] = React . useReducer (
155176 reducer ,
156- initialForm ,
177+ { $c , form : initialForm } ,
157178 createInitialState ,
158179 ) ;
159180
@@ -183,11 +204,12 @@ component EventEditForm(
183204 dispatch ( { action, type : 'update-date-range' } ) ;
184205 } , [ dispatch ] ) ;
185206
186- const hasErrors = hasSubfieldErrors ( state . form ) ;
207+ const externalLinksEditor = useExternalLinksEditor ( state , dispatch ) ;
187208
188- const eventEntity : EventT = getSourceEntityData ( $c , 'event' ) ;
209+ const hasErrors = hasSubfieldErrors ( state . form ) ||
210+ externalLinksEditor . hasErrors ;
189211
190- const externalLinksEditorRef = React . createRef < _ExternalLinksEditor > ( ) ;
212+ const eventEntity : EventT = getSourceEntityData ( $c , 'event' ) ;
191213
192214 // Ensure errors are shown if the user tries to submit with Enter
193215 const handleKeyDown = ( event : SyntheticKeyboardEvent < HTMLFormElement > ) => {
@@ -201,11 +223,6 @@ component EventEditForm(
201223 dispatch ( { type : 'show-all-pending-errors' } ) ;
202224 event . preventDefault ( ) ;
203225 }
204- invariant ( externalLinksEditorRef . current ) ;
205- prepareExternalLinksHtmlFormSubmission (
206- 'edit-event' ,
207- externalLinksEditorRef . current ,
208- ) ;
209226 } ;
210227
211228 const typeSelectRef = React . useRef < HTMLDivElement | null > ( null ) ;
@@ -299,14 +316,7 @@ component EventEditForm(
299316 seededRelationships = { $c . stash . seeded_relationships }
300317 />
301318
302- < fieldset >
303- < legend > { l ( 'External links' ) } </ legend >
304- < ExternalLinksEditor
305- isNewEntity = { ! eventEntity . id }
306- ref = { externalLinksEditorRef }
307- sourceData = { eventEntity }
308- />
309- </ fieldset >
319+ { externalLinksEditor . element }
310320
311321 < EnterEditNote field = { state . form . field . edit_note } />
312322 < EnterEdit disabled = { hasErrors } form = { state . form } />
0 commit comments