@@ -36,18 +36,15 @@ import * as URLCleanup from './URLCleanup';
36
36
import validation from './validation' ;
37
37
38
38
type LinkStateT = {
39
- relationship : number | string | null ,
39
+ // New relationships will use a unique string ID like "new-1".
40
+ relationship : StrOrNum | null ,
40
41
type : number | null ,
41
42
url : string ,
42
43
video : boolean ,
43
44
...
44
45
} ;
45
46
46
- type LinkHashT = {
47
- __proto__ : empty ,
48
- + [ key : number | string | null ] : LinkStateT ,
49
- ...
50
- } ;
47
+ type LinkMapT = Map < string , LinkStateT > ;
51
48
52
49
type LinksEditorProps = {
53
50
errorObservable : ( boolean ) => void ,
@@ -135,27 +132,27 @@ export class ExternalLinksEditor
135
132
} ) ;
136
133
}
137
134
138
- getOldLinksHash ( ) : LinkHashT {
139
- return keyBy (
135
+ getOldLinksHash ( ) : LinkMapT {
136
+ return keyBy < LinkStateT , string > (
140
137
this . props . initialLinks
141
138
. filter ( link => isPositiveInteger ( link . relationship ) ) ,
142
139
x => String ( x . relationship ) ,
143
140
) ;
144
141
}
145
142
146
143
getEditData ( ) : {
147
- allLinks : LinkHashT ,
148
- newLinks : LinkHashT ,
149
- oldLinks : LinkHashT ,
144
+ allLinks : LinkMapT ,
145
+ newLinks : LinkMapT ,
146
+ oldLinks : LinkMapT ,
150
147
} {
151
148
const oldLinks = this . getOldLinksHash ( ) ;
152
- const newLinks = keyBy <
153
- LinkStateT ,
154
- $ElementType < LinkStateT , 'relationship' > ,
155
- > ( this . state . links , x => String ( x . relationship ) ) ;
149
+ const newLinks : LinkMapT = keyBy (
150
+ this . state . links ,
151
+ x => String ( x . relationship ) ,
152
+ ) ;
156
153
157
154
return {
158
- allLinks : { ...oldLinks , ...newLinks } ,
155
+ allLinks : new Map ( [ ...oldLinks , ...newLinks ] ) ,
159
156
newLinks : newLinks ,
160
157
oldLinks : oldLinks ,
161
158
} ;
@@ -170,10 +167,7 @@ export class ExternalLinksEditor
170
167
const backward = this . props . sourceType > 'url' ;
171
168
const { oldLinks, newLinks, allLinks} = this . getEditData ( ) ;
172
169
173
- for (
174
- const [ relationship , link ] of
175
- ( ( Object . entries ( allLinks ) : any ) : $ReadOnlyArray < [ string , ?LinkStateT ] > )
176
- ) {
170
+ for ( const [ relationship , link ] of allLinks ) {
177
171
if ( ! link ?. type ) {
178
172
return ;
179
173
}
@@ -183,7 +177,7 @@ export class ExternalLinksEditor
183
177
if ( isPositiveInteger ( relationship ) ) {
184
178
pushInput ( prefix , 'relationship_id' , String ( relationship ) ) ;
185
179
186
- if ( ! newLinks [ relationship ] ) {
180
+ if ( ! newLinks . has ( relationship ) ) {
187
181
pushInput ( prefix , 'removed' , '1' ) ;
188
182
}
189
183
}
@@ -192,7 +186,7 @@ export class ExternalLinksEditor
192
186
193
187
if ( link . video ) {
194
188
pushInput ( prefix + '.attributes.0' , 'type.gid' , VIDEO_ATTRIBUTE_GID ) ;
195
- } else if ( ( oldLinks [ relationship ] || { } ) . video ) {
189
+ } else if ( oldLinks . get ( relationship ) ? .video ) {
196
190
pushInput ( prefix + '. attributes .0 ', 'type . gid ', VIDEO_ATTRIBUTE_GID ) ;
197
191
pushInput ( prefix + '. attributes .0 ', 'removed ', '1 ') ;
198
192
}
@@ -231,7 +225,7 @@ export class ExternalLinksEditor
231
225
const linkType = link . type
232
226
? linkedEntities . link_type [ link . type ] : { } ;
233
227
const checker = URLCleanup . validationRules [ linkType . gid ] ;
234
- const oldLink = oldLinks [ link . relationship ] ;
228
+ const oldLink = oldLinks . get ( String ( link . relationship ) ) ;
235
229
const isNewLink = ! isPositiveInteger ( link . relationship ) ;
236
230
const linkChanged = oldLink && link . url !== oldLink . url ;
237
231
const isNewOrChangedLink = ( isNewLink || linkChanged ) ;
@@ -274,7 +268,9 @@ export class ExternalLinksEditor
274
268
and should not be used.` ) ;
275
269
errorTarget = URLCleanup . ERROR_TARGETS . RELATIONSHIP ;
276
270
} else if (
277
- ( linksByTypeAndUrl [ linkTypeAndUrlString ( link ) ] || [ ] ) . length > 1
271
+ ( linksByTypeAndUrl . get (
272
+ linkTypeAndUrlString ( link ) ,
273
+ ) || [ ] ) . length > 1
278
274
) {
279
275
error = l ( 'This relationship already exists.' ) ;
280
276
errorTarget = URLCleanup . ERROR_TARGETS . RELATIONSHIP ;
0 commit comments