1
- import { Component , Input , OnChanges , OnDestroy , SimpleChanges } from '@angular/core' ;
1
+ import { Component , Input , OnDestroy , OnInit } from '@angular/core' ;
2
2
import { EventTimeLine , EventTimeLineCollection } from "./time-line/event-timeline.model" ;
3
3
import { RiderHistoryService } from "./rider-history.service" ;
4
4
import { Subscription } from "rxjs" ;
@@ -8,16 +8,13 @@ import {Subscription} from "rxjs";
8
8
templateUrl : './rider-time-line.component.html' ,
9
9
styleUrls : [ './rider-time-line.component.scss' ]
10
10
} )
11
- export class RiderTimeLineComponent implements OnDestroy , OnChanges {
12
-
13
- private riderTimeLineSubscription ?: Subscription ;
11
+ export class RiderTimeLineComponent implements OnDestroy , OnInit {
14
12
15
13
currentTimeLines ?: EventTimeLine [ ] ;
16
14
historyTimeLineCollection ?: EventTimeLineCollection [ ] ;
17
-
18
- loading : boolean = false ;
19
-
20
- @Input ( ) riderId ?: string ;
15
+ loading : boolean = true ;
16
+ @Input ( ) riderId ! : string ;
17
+ private riderTimeLineSubscription ! : Subscription ;
21
18
22
19
constructor ( private riderHistoryService : RiderHistoryService ) {
23
20
}
@@ -26,41 +23,38 @@ export class RiderTimeLineComponent implements OnDestroy, OnChanges {
26
23
this . riderTimeLineSubscription ?. unsubscribe ( ) ;
27
24
}
28
25
29
- ngOnChanges ( changes : SimpleChanges ) : void {
30
- if ( changes . riderId . currentValue != changes . riderId . previousValue && changes . riderId . currentValue != undefined ) {
31
- this . riderTimeLineSubscription ?. unsubscribe ( ) ;
32
-
33
- if ( this . riderId ) {
34
-
35
- this . loading = true ;
36
-
37
- this . riderTimeLineSubscription = this . riderHistoryService . getRiderTimeLines ( this . riderId ) . subscribe ( ( timelines ) => {
26
+ ngOnInit ( ) : void {
27
+ this . riderTimeLineSubscription = this . riderHistoryService . getRiderTimeLines ( this . riderId ) . subscribe ( ( timelines ) => {
28
+ const currentTimeLines = timelines . filter ( timeline => timeline . ongoing ) . sort ( ( a , b ) => this . timeLineSorter ( a , b ) ) ;
29
+ this . currentTimeLines = currentTimeLines . length > 0 ? currentTimeLines : undefined ;
30
+ const historyTimeLines = timelines . filter ( timeline => ! timeline . ongoing ) . sort ( ( a , b ) => this . timeLineSorter ( a , b ) ) ;
31
+ const historyTimeLineCollection : EventTimeLineCollection [ ] = [ ] ;
32
+ for ( const timeLine of historyTimeLines ) {
33
+ const year = new Date ( timeLine . surfEvent . startDateTime ) . getFullYear ( ) ;
34
+ const index = historyTimeLineCollection . findIndex ( htl => htl . year === year ) ;
35
+ if ( index != - 1 ) {
36
+ historyTimeLineCollection [ index ] . timeLines . push ( timeLine ) ;
38
37
39
- const currentTimeLines = timelines . filter ( timeline => timeline . ongoing ) . sort ( ( a , b ) => this . timeLineSorter ( a , b ) ) ;
40
- this . currentTimeLines = currentTimeLines . length > 0 ? currentTimeLines : undefined ;
41
-
42
- const historyTimeLines = timelines . filter ( timeline => ! timeline . ongoing ) . sort ( ( a , b ) => this . timeLineSorter ( a , b ) ) ;
43
- const historyTimeLineCollection : EventTimeLineCollection [ ] = [ ] ;
44
- for ( const timeLine of historyTimeLines ) {
45
- const year = new Date ( timeLine . surfEvent . startDateTime ) . getFullYear ( ) ;
46
- const index = historyTimeLineCollection . findIndex ( htl => htl . year === year ) ;
47
- if ( index != - 1 ) {
48
- historyTimeLineCollection [ index ] . timeLines . push ( timeLine ) ;
49
- } else {
50
- historyTimeLineCollection . push ( {
51
- year : year ,
52
- timeLines : [ timeLine ]
53
- } )
54
- }
38
+ } else {
39
+ historyTimeLineCollection . push ( {
40
+ year : year ,
41
+ timeLines : [ timeLine ]
42
+ } )
55
43
}
56
- this . historyTimeLineCollection = historyTimeLineCollection . length > 0 ? historyTimeLineCollection : undefined ;
44
+ }
45
+ this . historyTimeLineCollection = historyTimeLineCollection . length > 0 ? historyTimeLineCollection : undefined ;
46
+ this . loading = false ;
47
+
48
+ } ,
49
+ error => {
50
+ this . currentTimeLines = undefined ;
51
+ this . historyTimeLineCollection = undefined ;
52
+ this . loading = false ;
53
+ } )
57
54
58
- this . loading = false ;
59
- } )
60
- }
61
- }
62
55
}
63
56
57
+
64
58
private timeLineSorter ( tlA : EventTimeLine , tlB : EventTimeLine ) : number {
65
59
if ( new Date ( tlA . surfEvent . startDateTime ) . getFullYear ( ) < new Date ( tlB . surfEvent . startDateTime ) . getFullYear ( ) ) {
66
60
return - 1 ;
@@ -72,4 +66,5 @@ export class RiderTimeLineComponent implements OnDestroy, OnChanges {
72
66
}
73
67
}
74
68
}
69
+
75
70
}
0 commit comments