@@ -21,8 +21,21 @@ type Differences<T> = {
21
21
updated : { old : T ; new : T } [ ] ;
22
22
deleted : T [ ] ;
23
23
} ;
24
+ type GetOptionsDifferences = Differences <
25
+ FieldMetadataDefaultOption | FieldMetadataComplexOption
26
+ > ;
27
+
28
+ type Tmp = {
29
+ viewFilter : ViewFilterWorkspaceEntity ;
30
+ deletedOption : ( FieldMetadataDefaultOption | FieldMetadataComplexOption ) [ ] ;
31
+ updatedOption : GetOptionsDifferences [ 'updated' ] ;
32
+ filteredOptionsCounter : number ;
33
+ } ;
34
+
35
+ const MAX_OPTIONS_TO_DISPLAY = 3 ; // TODO mutualize;
24
36
25
- export type SelectFieldMetadataEntity = FieldMetadataEntity < FieldMetadataType . SELECT > ;
37
+ export type SelectFieldMetadataEntity =
38
+ FieldMetadataEntity < FieldMetadataType . SELECT > ;
26
39
27
40
@Injectable ( )
28
41
export class FieldMetadataRelatedRecordsService {
@@ -108,6 +121,41 @@ export class FieldMetadataRelatedRecordsService {
108
121
}
109
122
}
110
123
124
+ private computeViewFilterDisplayValue ( {
125
+ deletedOption,
126
+ updatedOption,
127
+ viewFilter,
128
+ filteredOptionsCounter,
129
+ } : Tmp ) : string {
130
+ if ( filteredOptionsCounter > MAX_OPTIONS_TO_DISPLAY ) {
131
+ return `${ filteredOptionsCounter } options` ;
132
+ }
133
+
134
+ const viewFilterDisplayValues = viewFilter . displayValue . split ( ',' ) ;
135
+
136
+ const remainingViewFilterDisplayValue = viewFilterDisplayValues . filter (
137
+ ( viewFilterOptionLabel ) => {
138
+ ! deletedOption . find ( ( option ) => option . label === viewFilterOptionLabel ) ;
139
+ } ,
140
+ ) ;
141
+
142
+ const updatedViewFilterDisplayValue = remainingViewFilterDisplayValue . map (
143
+ ( viewFilterOptionLabel ) => {
144
+ const matchingUpdatedOption = updatedOption . find (
145
+ ( option ) => option . old . label === viewFilterOptionLabel ,
146
+ ) ;
147
+
148
+ if ( ! isDefined ( matchingUpdatedOption ) ) {
149
+ return viewFilterOptionLabel ;
150
+ }
151
+
152
+ return matchingUpdatedOption . new . label ;
153
+ } ,
154
+ ) ;
155
+
156
+ return updatedViewFilterDisplayValue . join ( ', ' ) ;
157
+ }
158
+
111
159
public async updateRelatedViewFilters (
112
160
oldFieldMetadata : SelectFieldMetadataEntity ,
113
161
newFieldMetadata : SelectFieldMetadataEntity ,
@@ -144,47 +192,53 @@ export class FieldMetadataRelatedRecordsService {
144
192
for ( const viewFilter of filter . viewFilters ) {
145
193
try {
146
194
const viewFilterValue : string [ ] = JSON . parse ( viewFilter . value ) ;
147
- const relatedDeletedLabels = deletedFieldMetadata
148
- . filter ( ( deleted ) => viewFilterValue . includes ( deleted . label ) )
149
- . map ( ( { label } ) => label ) ;
195
+ const relatedDeletedOptions = deletedFieldMetadata . filter ( ( deleted ) =>
196
+ viewFilterValue . includes ( deleted . value ) ,
197
+ ) ;
150
198
151
- if ( relatedDeletedLabels . length === viewFilterValue . length ) {
199
+ if ( relatedDeletedOptions . length === viewFilterValue . length ) {
152
200
await viewFilterRepository . delete ( { id : viewFilter . id } ) ;
153
201
continue ;
154
202
}
155
203
156
- const remainingFilterLabels = viewFilterValue . filter (
157
- ( viewFilterLabel ) => ! relatedDeletedLabels . includes ( viewFilterLabel ) ,
204
+ const remainingViewFilterValues = viewFilterValue . filter (
205
+ ( viewFilterOptionValue ) =>
206
+ ! relatedDeletedOptions . find (
207
+ ( option ) => option . value === viewFilterOptionValue ,
208
+ ) ,
209
+ ) ;
210
+
211
+ const relatedUpdatedOptions = updatedFieldMetadata . filter ( ( updated ) =>
212
+ remainingViewFilterValues . includes ( updated . old . value ) ,
158
213
) ;
159
- const relatedUpdatedLabels = updatedFieldMetadata
160
- . filter ( ( updated ) =>
161
- remainingFilterLabels . includes ( updated . old . label ) ,
162
- )
163
- . map ( ( data ) => ( {
164
- newLabel : data . new . label ,
165
- oldLabel : data . old . label ,
166
- } ) ) ;
167
-
168
- const updatedFilterLabels = remainingFilterLabels . flatMap ( ( label ) => {
169
- const containsUpdatedFilter = relatedUpdatedLabels . find (
170
- ( { oldLabel } ) => label === oldLabel ,
171
- ) ;
172
- if ( ! isDefined ( containsUpdatedFilter ) ) {
173
- return label ;
174
- }
175
-
176
- return containsUpdatedFilter . newLabel ;
214
+
215
+ const updatedViewFilterValues = remainingViewFilterValues . map (
216
+ ( viewFilterOptionValue ) => {
217
+ const containsUpdatedFilter = relatedUpdatedOptions . find (
218
+ ( { old } ) => viewFilterOptionValue === old . value ,
219
+ ) ;
220
+ if ( ! isDefined ( containsUpdatedFilter ) ) {
221
+ return viewFilterOptionValue ;
222
+ }
223
+
224
+ return containsUpdatedFilter . new . value ;
225
+ } ,
226
+ ) ;
227
+
228
+ const displayValue = this . computeViewFilterDisplayValue ( {
229
+ deletedOption : relatedDeletedOptions ,
230
+ updatedOption : relatedUpdatedOptions ,
231
+ viewFilter,
232
+ filteredOptionsCounter : updatedViewFilterValues . length ,
177
233
} ) ;
178
234
179
235
await viewFilterRepository . update (
180
236
{ id : viewFilter . id } ,
181
237
{
182
- value : JSON . stringify ( updatedFilterLabels ) ,
183
- displayValue : updatedFilterLabels . join ( ',' ) ,
238
+ value : JSON . stringify ( updatedViewFilterValues ) ,
239
+ displayValue,
184
240
} ,
185
241
) ;
186
-
187
- ///
188
242
} catch ( error ) {
189
243
// TODO
190
244
console . error ( error ) ;
@@ -221,7 +275,7 @@ export class FieldMetadataRelatedRecordsService {
221
275
private getOptionsDifferences (
222
276
oldOptions : ( FieldMetadataDefaultOption | FieldMetadataComplexOption ) [ ] ,
223
277
newOptions : ( FieldMetadataDefaultOption | FieldMetadataComplexOption ) [ ] ,
224
- ) : Differences < FieldMetadataDefaultOption | FieldMetadataComplexOption > {
278
+ ) : GetOptionsDifferences {
225
279
const differences : Differences <
226
280
FieldMetadataDefaultOption | FieldMetadataComplexOption
227
281
> = {
0 commit comments