1
- import { ChangeDetectionStrategy , Component , Inject , OnInit } from '@angular/core' ;
1
+ import { ChangeDetectionStrategy , Component , ElementRef , Inject , OnInit , ViewChild } from '@angular/core' ;
2
2
import { FormControl , FormGroup , Validators } from '@angular/forms' ;
3
3
import { Quarter } from '../../types/model/Quarter' ;
4
4
import { TeamService } from '../../services/team.service' ;
@@ -24,12 +24,15 @@ import { AlignmentPossibility } from '../../types/model/AlignmentPossibility';
24
24
changeDetection : ChangeDetectionStrategy . OnPush ,
25
25
} )
26
26
export class ObjectiveFormComponent implements OnInit {
27
+ @ViewChild ( 'input' ) input ! : ElementRef < HTMLInputElement > ;
28
+ filteredOptions : AlignmentPossibility [ ] = [ ] ;
29
+
27
30
objectiveForm = new FormGroup ( {
28
31
title : new FormControl < string > ( '' , [ Validators . required , Validators . minLength ( 2 ) , Validators . maxLength ( 250 ) ] ) ,
29
32
description : new FormControl < string > ( '' , [ Validators . maxLength ( 4096 ) ] ) ,
30
33
quarter : new FormControl < number > ( 0 , [ Validators . required ] ) ,
31
34
team : new FormControl < number > ( { value : 0 , disabled : true } , [ Validators . required ] ) ,
32
- alignment : new FormControl < string > ( '' ) ,
35
+ alignment : new FormControl < AlignmentPossibility | null > ( null ) ,
33
36
createKeyResults : new FormControl < boolean > ( false ) ,
34
37
} ) ;
35
38
quarters$ : Observable < Quarter [ ] > = of ( [ ] ) ;
@@ -65,6 +68,19 @@ export class ObjectiveFormComponent implements OnInit {
65
68
onSubmit ( submitType : any ) : void {
66
69
const value = this . objectiveForm . getRawValue ( ) ;
67
70
const state = this . data . objective . objectiveId == null ? submitType : this . state ;
71
+
72
+ let alignmentEntity : string | null = '' ;
73
+ let alignment : any = value . alignment ;
74
+ if ( alignment ) {
75
+ if ( alignment ?. objectiveId ) {
76
+ alignmentEntity = 'O' + alignment . objectiveId ;
77
+ } else {
78
+ alignmentEntity = 'K' + alignment . keyResultId ;
79
+ }
80
+ } else {
81
+ alignmentEntity = null ;
82
+ }
83
+
68
84
let objectiveDTO : Objective = {
69
85
id : this . data . objective . objectiveId ,
70
86
version : this . version ,
@@ -73,7 +89,7 @@ export class ObjectiveFormComponent implements OnInit {
73
89
title : value . title ,
74
90
teamId : value . team ,
75
91
state : state ,
76
- alignedEntityId : value . alignment == 'Onull' ? null : value . alignment ,
92
+ alignedEntityId : alignmentEntity ,
77
93
} as unknown as Objective ;
78
94
79
95
const submitFunction = this . getSubmitFunction ( objectiveDTO . id , objectiveDTO ) ;
@@ -106,14 +122,15 @@ export class ObjectiveFormComponent implements OnInit {
106
122
this . teams$ . subscribe ( ( value ) => {
107
123
this . currentTeam . next ( value . filter ( ( team ) => team . id == teamId ) [ 0 ] ) ;
108
124
} ) ;
109
- this . generateAlignmentPossibilities ( quarterId ) ;
125
+ this . generateAlignmentPossibilities ( quarterId , objective ) ;
110
126
111
127
this . objectiveForm . patchValue ( {
112
128
title : objective . title ,
113
129
description : objective . description ,
114
130
team : teamId ,
115
131
quarter : quarterId ,
116
- alignment : objective . alignedEntityId ? objective . alignedEntityId : 'Onull' ,
132
+ // alignment: null,
133
+ // alignment: objective.alignedEntityId ? objective.alignedEntityId : 'Onull',
117
134
} ) ;
118
135
} ) ;
119
136
}
@@ -238,47 +255,110 @@ export class ObjectiveFormComponent implements OnInit {
238
255
return GJ_REGEX_PATTERN . test ( label ) ;
239
256
}
240
257
241
- generateAlignmentPossibilities ( quarterId : number ) {
258
+ generateAlignmentPossibilities ( quarterId : number , objective : Objective | null ) {
242
259
this . alignmentPossibilities$ = this . objectiveService . getAlignmentPossibilities ( quarterId ) ;
243
260
this . alignmentPossibilities$ . subscribe ( ( value : AlignmentPossibility [ ] ) => {
244
261
if ( this . objective ?. id ) {
245
262
value = value . filter ( ( item : AlignmentPossibility ) => ! ( item . objectiveId == this . objective ! . id ) ) ;
246
263
}
247
- let firstSelectOption = {
248
- objectiveId : null ,
249
- objectiveTitle : 'Kein Alignment' ,
250
- keyResultAlignmentsDtos : [ ] ,
251
- } ;
252
- if ( value . length != 0 ) {
253
- if ( this . objective ?. alignedEntityId ) {
254
- if ( value [ 0 ] . objectiveTitle == 'Bitte wählen' ) {
255
- value . splice ( 0 , 1 ) ;
264
+ // let firstSelectOption = {
265
+ // objectiveId: null,
266
+ // objectiveTitle: 'Kein Alignment',
267
+ // keyResultAlignmentsDtos: [],
268
+ // };
269
+ // if (value.length != 0) {
270
+ // if (this.objective?.alignedEntityId) {
271
+ // if (value[0].objectiveTitle == 'Bitte wählen') {
272
+ // value.splice(0, 1);
273
+ // }
274
+ // } else {
275
+ // firstSelectOption.objectiveTitle = 'Bitte wählen';
276
+ // }
277
+ // }
278
+ // value.unshift(firstSelectOption);
279
+
280
+ if ( objective ) {
281
+ let alignment = objective . alignedEntityId ;
282
+ if ( alignment ) {
283
+ let alignmentType = alignment . charAt ( 0 ) ;
284
+ let alignmentId = parseInt ( alignment . substring ( 1 ) ) ;
285
+ if ( alignmentType == 'O' ) {
286
+ let element = value . find ( ( ap ) => ap . objectiveId == alignmentId ) || null ;
287
+ this . objectiveForm . patchValue ( {
288
+ alignment : element ,
289
+ } ) ;
290
+ } else {
291
+ for ( let objectiveAlignment of value ) {
292
+ let keyResult = objectiveAlignment . keyResultAlignmentsDtos . find ( ( kr ) => kr . keyResultId == alignmentId ) ;
293
+ if ( keyResult ) {
294
+ // TODO change this to keyresult
295
+ this . objectiveForm . patchValue ( {
296
+ alignment : objectiveAlignment ,
297
+ } ) ;
298
+ }
299
+ }
256
300
}
257
- } else {
258
- firstSelectOption . objectiveTitle = 'Bitte wählen' ;
259
301
}
302
+ } else {
303
+ this . objectiveForm . patchValue ( {
304
+ alignment : null ,
305
+ } ) ;
260
306
}
261
- value . unshift ( firstSelectOption ) ;
307
+
308
+ this . filteredOptions = value . slice ( ) ;
262
309
this . alignmentPossibilities$ = of ( value ) ;
263
310
} ) ;
264
311
}
265
312
266
313
updateAlignments ( ) {
267
- this . generateAlignmentPossibilities ( this . objectiveForm . value . quarter ! ) ;
268
- this . objectiveForm . patchValue ( {
269
- alignment : 'Onull' ,
270
- } ) ;
314
+ this . generateAlignmentPossibilities ( this . objectiveForm . value . quarter ! , null ) ;
315
+ // this.objectiveForm.patchValue({
316
+ // alignment: 'Onull',
317
+ // });
271
318
}
272
319
273
- changeFirstAlignmentPossibility ( ) {
274
- this . alignmentPossibilities$ . subscribe ( ( value : AlignmentPossibility [ ] ) => {
275
- let element : AlignmentPossibility = value [ 0 ] ;
276
- element . objectiveTitle = 'Kein Alignment' ;
277
- value . splice ( 0 , 1 ) ;
278
- value . unshift ( element ) ;
279
- this . alignmentPossibilities$ = of ( value ) ;
320
+ // changeFirstAlignmentPossibility() {
321
+ // this.alignmentPossibilities$.subscribe((value: AlignmentPossibility[]) => {
322
+ // let element: AlignmentPossibility = value[0];
323
+ // element.objectiveTitle = 'Kein Alignment';
324
+ // value.splice(0, 1);
325
+ // value.unshift(element);
326
+ // this.alignmentPossibilities$ = of(value);
327
+ // });
328
+ // }
329
+
330
+ filter ( ) {
331
+ let filterValue = this . input . nativeElement . value . toLowerCase ( ) ;
332
+ this . alignmentPossibilities$ . subscribe ( ( value ) => {
333
+ // this.filteredOptions = value.filter((o) => o.objectiveTitle.toLowerCase().includes(filterValue));
334
+ this . filteredOptions = value . filter (
335
+ ( o ) =>
336
+ o . objectiveTitle . toLowerCase ( ) . includes ( filterValue ) || // Check if objectiveTitle includes the filterValue
337
+ o . keyResultAlignmentsDtos . some ( ( kr ) => kr . keyResultTitle . toLowerCase ( ) . includes ( filterValue ) ) , // Check if any keyResultTitle includes the filterValue
338
+ ) ;
280
339
} ) ;
281
340
}
282
341
342
+ displayWith ( value : any ) : string {
343
+ if ( value ) {
344
+ if ( value . objectiveId ) {
345
+ return value . objectiveTitle ;
346
+ } else {
347
+ return value . keyResultTitle ;
348
+ }
349
+ } else {
350
+ return 'Bitte wählen' ;
351
+ }
352
+ }
353
+
354
+ get displayedValue ( ) : string {
355
+ if ( this . input ) {
356
+ const inputValue = this . input . nativeElement . value ;
357
+ return inputValue . length > 40 ? inputValue . slice ( 0 , 40 ) + '...' : inputValue ;
358
+ } else {
359
+ return '' ;
360
+ }
361
+ }
362
+
283
363
protected readonly getQuarterLabel = getQuarterLabel ;
284
364
}
0 commit comments