@@ -17,7 +17,7 @@ import { VehicleFilters } from './vehicleFilters';
1717import { MatDialog } from '@angular/material/dialog' ;
1818import { EditPlateComponent } from './edit-plate/edit-plate.component' ;
1919import { LocalStorageService } from '@app/_services/local-storage.service' ;
20- import { EnrichersService } from '@app/settings/enrichers/enrichers.service ' ;
20+ import { timeStamp } from 'console ' ;
2121
2222@Component ( {
2323 selector : 'app-plates' ,
@@ -62,6 +62,7 @@ export class PlatesComponent implements OnInit, OnDestroy, AfterViewInit {
6262 public range : FormGroup ;
6363 public plates : Plate [ ] = [ ] ;
6464 public totalNumberOfPlates : number ;
65+ public todaysDate : Date ;
6566
6667 public filterPlateNumber : string ;
6768 public filterPlateNumberIsValid : boolean = true ;
@@ -94,12 +95,12 @@ export class PlatesComponent implements OnInit, OnDestroy, AfterViewInit {
9495 private pageSizeCacheKey : string = "platePageSize" ;
9596 private pageNumber : number = 0 ;
9697
97- private subscriptions = new Subscription ( ) ;
98-
98+ private eventSubscriptions = new Subscription ( ) ;
99+ private searchSubscription = new Subscription ( ) ;
100+
99101 @ViewChild ( MatPaginator ) paginator : MatPaginator ;
100102
101103 constructor (
102- private enricherService : EnrichersService ,
103104 private plateService : PlateService ,
104105 private signalRHub : SignalrService ,
105106 private snackbarService : SnackbarService ,
@@ -119,27 +120,40 @@ export class PlatesComponent implements OnInit, OnDestroy, AfterViewInit {
119120 this . pageSize = pageSize != null ? parseInt ( pageSize ) : 25 ;
120121 this . searchPlates ( ) ;
121122 this . populateFilters ( ) ;
123+ this . setInitialDateFilter ( ) ;
122124 }
123125
124126 ngOnDestroy ( ) : void {
125- this . subscriptions . unsubscribe ( ) ;
127+ this . eventSubscriptions . unsubscribe ( ) ;
128+ this . searchSubscription . unsubscribe ( ) ;
126129 }
127130
128131 ngAfterViewInit ( ) : void {
129132 this . subscribeForUpdates ( ) ;
130133 }
131134
135+ public setInitialDateFilter ( ) {
136+ this . todaysDate = new Date ( ) ;
137+ this . filterStartOn = new Date ( ) ;
138+ this . filterEndOn = new Date ( ) ;
139+
140+ this . filterStartOn . setDate ( new Date ( ) . getDate ( ) - 15 ) ;
141+ this . filterEndOn = this . todaysDate ;
142+ }
143+
132144 public editPlate ( plateNumber : string ) {
133145 this . openEditDialog ( plateNumber ) ;
134146 }
135147
136148 public subscribeForUpdates ( ) {
137- this . subscriptions . add ( this . signalRHub . connectionStatusChanged . subscribe ( status => {
149+ this . eventSubscriptions . add ( this . signalRHub . connectionStatusChanged . subscribe ( status => {
138150 this . isSignalrConnected = status ;
139151 } ) ) ;
140152
141- this . subscriptions . add ( this . signalRHub . licensePlateReceived . subscribe ( _ => {
142- this . searchPlates ( ) ;
153+ this . eventSubscriptions . add ( this . signalRHub . licensePlateReceived . subscribe ( _ => {
154+ if ( ! this . isLoading ) {
155+ this . searchPlates ( ) ;
156+ }
143157 } ) ) ;
144158 }
145159
@@ -159,8 +173,6 @@ export class PlatesComponent implements OnInit, OnDestroy, AfterViewInit {
159173 }
160174
161175 public searchPlates ( plateNumber : string = '' ) {
162- var request = new PlateRequest ( ) ;
163-
164176 if ( ! this . filterPlateNumberIsValid ) {
165177 return ;
166178 }
@@ -172,6 +184,8 @@ export class PlatesComponent implements OnInit, OnDestroy, AfterViewInit {
172184 this . filterStartOn ?. setUTCHours ( 0 , 0 , 0 , 0 ) ;
173185 this . filterEndOn ?. setUTCHours ( 23 , 59 , 59 , 999 ) ;
174186
187+ var request = new PlateRequest ( ) ;
188+
175189 request . pageNumber = this . pageNumber ;
176190 request . pageSize = this . pageSize ;
177191 request . endSearchOn = this . filterEndOn ;
@@ -186,13 +200,18 @@ export class PlatesComponent implements OnInit, OnDestroy, AfterViewInit {
186200 request . vehicleType = this . filterVehicleType ;
187201 request . vehicleRegion = this . filterVehicleRegion ;
188202
203+ if ( this . isLoading ) {
204+ this . searchSubscription . unsubscribe ( ) ;
205+ }
206+
189207 this . isLoading = true ;
190- this . plateService . searchPlates ( request ) . subscribe ( result => {
208+ this . searchSubscription = this . plateService . searchPlates ( request ) . subscribe ( result => {
191209 this . totalNumberOfPlates = result . totalCount ;
192210 this . plates = result . plates ;
193211 this . isLoading = false ;
194212 } , error => {
195213 this . isLoading = false ;
214+ this . snackbarService . create ( `Error searching for plates, check the logs` , SnackBarType . Error )
196215 } ) ;
197216 }
198217
@@ -237,8 +256,8 @@ export class PlatesComponent implements OnInit, OnDestroy, AfterViewInit {
237256 }
238257
239258 public clearFilters ( ) {
240- this . filterEndOn = null ;
241- this . filterStartOn = null ;
259+ this . setInitialDateFilter ( ) ;
260+
242261 this . filterPlateNumber = '' ;
243262 this . filterPlateNumberIsValid = true ;
244263 this . filterStrictMatch = false ;
0 commit comments