@@ -33,15 +33,12 @@ import {
33
33
EuiText ,
34
34
} from '@elastic/eui' ;
35
35
36
- import { PortMode , ProductTag } from '@/components' ;
37
36
import { useWithOrchestratorTheme } from '@/hooks' ;
38
- import { useGetSubscriptionDropdownOptions } from '@/hooks/deprecated/useGetSubscriptionDropdownOptions' ;
39
- import { SubscriptionDropdownOption } from '@/types' ;
37
+ import { useGetSurfSubscriptionDropdownOptions } from '@/hooks/deprecated/useGetSurfSubcriptionDropdownOptions' ;
40
38
41
- import { getSelectFieldStyles } from './SelectField/styles' ;
39
+ import { getSelectFieldStyles } from '../SelectField/styles' ;
40
+ import { FieldProps , Option } from '../types' ;
42
41
import { subscriptionFieldStyling } from './SubscriptionFieldStyling' ;
43
- import { FieldProps , Option } from './types' ;
44
- import { getPortMode } from './utils' ;
45
42
46
43
declare module 'uniforms' {
47
44
interface FilterDOMProps {
@@ -81,6 +78,18 @@ export type SubscriptionFieldProps = FieldProps<
81
78
}
82
79
> ;
83
80
81
+ function toPortModes ( visiblePortMode : string ) : string [ ] {
82
+ if ( visiblePortMode === 'all' ) {
83
+ return [ ] ;
84
+ }
85
+
86
+ if ( visiblePortMode === 'normal' ) {
87
+ return [ 'tagged' , 'untagged' ] ;
88
+ }
89
+
90
+ return [ visiblePortMode ] ;
91
+ }
92
+
84
93
function SubscriptionFieldDefinition ( {
85
94
disabled,
86
95
id,
@@ -110,9 +119,6 @@ function SubscriptionFieldDefinition({
110
119
const { reactSelectInnerComponentStyles } =
111
120
useWithOrchestratorTheme ( getSelectFieldStyles ) ;
112
121
113
- const { refetch, subscriptions, isFetching } =
114
- useGetSubscriptionDropdownOptions ( tags , statuses ) ;
115
-
116
122
const nameArray = joinName ( null , name ) ;
117
123
let parentName = joinName ( nameArray . slice ( 0 , - 1 ) ) ;
118
124
@@ -136,160 +142,50 @@ function SubscriptionFieldDefinition({
136
142
? get ( model , customerKey , 'nonExistingOrgToFilterEverything' )
137
143
: customerId ;
138
144
139
- const makeLabel = ( subscription : SubscriptionDropdownOption ) => {
140
- const description =
141
- subscription . description ||
142
- t ( 'widgets.subscription.missingDescription' ) ;
143
- const subscriptionSubstring = subscription . subscriptionId . substring (
144
- 0 ,
145
- 8 ,
146
- ) ;
147
-
148
- if ( [ 'Node' ] . includes ( subscription . product . tag ) ) {
149
- const description =
150
- subscription . description ||
151
- t ( 'widgets.subscription.missingDescription' ) ;
152
- return `${ subscription . subscriptionId . substring (
153
- 0 ,
154
- 8 ,
155
- ) } ${ description . trim ( ) } `;
156
- } else if (
157
- [
158
- ProductTag . SP ,
159
- ProductTag . SPNL ,
160
- ProductTag . AGGSP ,
161
- ProductTag . AGGSPNL ,
162
- ProductTag . MSC ,
163
- ProductTag . MSCNL ,
164
- ProductTag . IRBSP ,
165
- ] . includes ( subscription . product . tag as ProductTag )
145
+ const getFilteredOptions = ( optionsInput : Option [ ] ) : Option [ ] => {
146
+ // Remnant of the old logic in which much more filtering happened clientside, which is now done
147
+ // server-side by setting the required URL parameters.
148
+
149
+ // The 'uniqueItems' filter below should exclude options already chosen in other SubscriptionFields in the same parent Array.
150
+ // Although this partly relies on uniforms magic which will be reworked/replaced with pydantic-forms.
151
+ if (
152
+ parentName !== name &&
153
+ parent . fieldType === Array &&
154
+ // @ts -expect-error Parent field can have the uniqueItems boolean property but this is not part of JSONSchema6 type
155
+ // TODO: Figure out why this is so
156
+ parent . uniqueItems
166
157
) {
167
- const portMode = getPortMode ( subscription . productBlockInstances ) ;
168
- const subscriptionTitle =
169
- subscription . productBlockInstances [ 0 ] . productBlockInstanceValues . find (
170
- ( item ) => item . field === 'title' ,
171
- ) ;
172
- if ( subscriptionTitle ) {
173
- return `${ subscriptionSubstring } - ${ description . trim ( ) } - ${
174
- subscriptionTitle . value
175
- } `;
176
- }
177
- return `${ subscriptionSubstring } ${ portMode ?. toUpperCase ( ) } ${ description . trim ( ) } ${
178
- subscription . customer ?. fullname
179
- } `;
158
+ const allValues : string [ ] = get ( model , parentName , [ ] ) ;
159
+ const chosenValues = allValues . filter (
160
+ ( _item , index ) =>
161
+ index . toString ( ) !== nameArray [ nameArray . length - 1 ] ,
162
+ ) ;
163
+
164
+ return optionsInput . filter ( ( option ) =>
165
+ chosenValues . includes ( option . value ) ,
166
+ ) ;
180
167
} else {
181
- return description . trim ( ) ;
168
+ return optionsInput ;
182
169
}
183
170
} ;
184
171
185
- // Filter by product, needed because getSubscriptions might return more than we want
186
- const getSubscriptionOptions = ( ) : Option [ ] => {
187
- const filteredSubscriptions = subscriptions ?. filter ( ( subscription ) => {
188
- // NOTE: useBandWith, productIds and tags need to be checked in this order as per the V1 logic
189
-
190
- // If a bandwidth filter is supplied it needs to be applied to the subscription product
191
- if ( usedBandwidth ) {
192
- const portSpeedInput = subscription . fixedInputs . find (
193
- ( fixedInput ) => fixedInput . field === 'port_speed' ,
194
- ) ;
195
- if (
196
- portSpeedInput ?. value &&
197
- parseInt ( portSpeedInput . value . toString ( ) , 10 ) <
198
- parseInt ( usedBandwidth . toString ( ) , 10 )
199
- ) {
200
- return false ;
201
- }
202
- }
203
-
204
- // If specific productIds are provided the subscriptions needs to have one of those
205
- if (
206
- ! usedBandwidth &&
207
- productIds &&
208
- productIds . length > 0 &&
209
- ! productIds . includes ( subscription . product . productId )
210
- ) {
211
- return false ;
212
- }
213
-
214
- if (
215
- ! usedBandwidth &&
216
- ! productIds &&
217
- tags &&
218
- tags ?. length > 0 &&
219
- ! tags . includes ( subscription . product . tag )
220
- ) {
221
- return false ;
222
- }
223
-
224
- // If specific subscriptionIds are excluded the subscription can't be one ot those
225
- if (
226
- excludedSubscriptionIds &&
227
- excludedSubscriptionIds . length > 0 &&
228
- excludedSubscriptionIds . includes ( subscription . subscriptionId )
229
- ) {
230
- return false ;
231
- }
232
-
233
- // If a Port mode filter is applied we need to filter on that
234
- if ( visiblePortMode !== 'all' ) {
235
- const portMode = getPortMode (
236
- subscription . productBlockInstances ,
237
- ) ;
238
- // For normal mode filter out all subscriptions that don't have tagged or untagged ports
239
- if (
240
- visiblePortMode === 'normal' &&
241
- ! [ PortMode . TAGGED , PortMode . UNTAGGED , undefined ] . includes (
242
- portMode ,
243
- )
244
- ) {
245
- return false ;
246
- } else if (
247
- portMode !== visiblePortMode &&
248
- visiblePortMode !== 'normal'
249
- ) {
250
- return false ;
251
- }
252
- }
253
-
254
- // If a customer filter is applied we need to filter on that
255
- if (
256
- usedCustomerId &&
257
- subscription . customer ?. customerId !== usedCustomerId
258
- ) {
259
- return false ;
260
- }
261
-
262
- if ( parentName !== name ) {
263
- if (
264
- parent . fieldType === Array &&
265
- // @ts -expect-error Parent field can have the uniqueItems boolean property but this is not part of JSONSchema6 type
266
- // TODO: Figure out why this is so
267
- parent . uniqueItems
268
- ) {
269
- const allValues : string [ ] = get ( model , parentName , [ ] ) ;
270
- const chosenValues = allValues . filter (
271
- ( _item , index ) =>
272
- index . toString ( ) !==
273
- nameArray [ nameArray . length - 1 ] ,
274
- ) ;
275
- if ( ! chosenValues . includes ( subscription . subscriptionId ) ) {
276
- return false ;
277
- }
278
- }
279
- }
280
-
281
- return true ;
282
- } ) ;
283
-
284
- return filteredSubscriptions
285
- ? filteredSubscriptions . map ( ( subscription ) => ( {
286
- label : makeLabel ( subscription ) ,
287
- value : subscription . subscriptionId ,
288
- } ) )
289
- : [ ] ;
290
- } ;
172
+ const excludeSubscriptionIds = excludedSubscriptionIds ;
173
+ const portModes = toPortModes ( visiblePortMode ) ;
174
+ const {
175
+ refetch,
176
+ options : unfilteredOptions ,
177
+ isFetching,
178
+ } = useGetSurfSubscriptionDropdownOptions (
179
+ tags ,
180
+ statuses ,
181
+ productIds ,
182
+ excludeSubscriptionIds ,
183
+ usedCustomerId ,
184
+ portModes ,
185
+ usedBandwidth ,
186
+ ) ;
291
187
292
- const options = getSubscriptionOptions ( ) ;
188
+ const options = getFilteredOptions ( unfilteredOptions ) ;
293
189
294
190
const selectedValue = options . find (
295
191
( option : Option ) => option . value === value ,
0 commit comments