1
1
import { Component , OnInit } from '@angular/core' ;
2
2
import { ActivatedRoute , Router } from '@angular/router' ;
3
- import { AnyObject } from '@project-lib/core/api' ;
3
+ import { AnyObject , BackendFilter , Count } from '@project-lib/core/api' ;
4
4
import { RouteComponentBaseDirective } from '@project-lib/core/route-component-base' ;
5
- import { ColDef } from 'ag-grid-community' ;
6
- import { takeUntil } from 'rxjs' ;
5
+ import {
6
+ ColDef ,
7
+ GridApi ,
8
+ GridOptions ,
9
+ IDatasource ,
10
+ IGetRowsParams ,
11
+ } from 'ag-grid-community' ;
12
+ import { Observable , combineLatest , map , takeUntil } from 'rxjs' ;
7
13
import { Location } from '@angular/common' ;
8
14
9
15
import { BillingPlanService } from '../../../shared/services/billing-plan-service' ;
10
16
import { SubscriptionStatus } from '../../../shared/enum/subscription-status.enum' ;
17
+ import { Plan } from '../../../shared/models' ;
11
18
12
19
@Component ( {
13
20
selector : 'app-billing-plan' ,
14
21
templateUrl : './billing-plan.component.html' ,
15
22
styleUrls : [ './billing-plan.component.scss' ] ,
16
23
} )
17
- export class BillingPlanComponent
18
- extends RouteComponentBaseDirective
19
- implements OnInit
20
- {
24
+ export class BillingPlanComponent extends RouteComponentBaseDirective {
25
+ gridApi : GridApi ;
26
+ gridOptions : GridOptions ;
27
+ limit = 5 ;
21
28
colDefs : ColDef [ ] = [
22
29
{ field : 'companyName' , width : 200 , minWidth : 20 } ,
23
30
{ field : 'userName' , width : 200 , minWidth : 20 } ,
@@ -37,18 +44,47 @@ export class BillingPlanComponent
37
44
private readonly billingplanService : BillingPlanService ,
38
45
) {
39
46
super ( route , location ) ;
47
+ this . gridOptions = {
48
+ pagination : true ,
49
+ rowModelType : 'infinite' ,
50
+ paginationPageSize : this . limit ,
51
+ paginationPageSizeSelector : [ this . limit , 10 , 20 , 50 , 100 ] ,
52
+ cacheBlockSize : this . limit ,
53
+ onGridReady : this . onGridReady . bind ( this ) ,
54
+ rowHeight : 60 ,
55
+ defaultColDef : { flex : 1 } ,
56
+ } ;
40
57
}
41
58
42
- ngOnInit ( ) : void {
43
- this . getBillingPlan ( ) ;
59
+ onGridReady ( params : AnyObject ) {
60
+ this . gridApi = params . api ;
61
+ const dataSource : IDatasource = {
62
+ getRows : ( params : IGetRowsParams ) => {
63
+ const page = params . endRow / this . limit ;
64
+ const paginatedLeads = this . getPaginatedBillPlans ( page , this . limit ) ;
65
+ const totalLead = this . getTotal ( ) ;
66
+ combineLatest ( [ paginatedLeads , totalLead ] ) . subscribe (
67
+ ( [ data , count ] ) => {
68
+ params . successCallback ( data , count . count ) ;
69
+ } ,
70
+
71
+ err => {
72
+ params . failCallback ( ) ;
73
+ } ,
74
+ ) ;
75
+ } ,
76
+ } ;
77
+ params . api . setDatasource ( dataSource ) ;
44
78
}
45
79
46
- getBillingPlan ( ) {
47
- this . billingplanService
48
- . getBillingDetails ( )
49
- . pipe ( takeUntil ( this . _destroy$ ) )
50
- . subscribe ( res => {
51
- this . rowData = res . map ( item => {
80
+ getPaginatedBillPlans ( page : number , limit : number ) : Observable < AnyObject [ ] > {
81
+ const filter : BackendFilter < Plan > = {
82
+ offset : limit * ( page - 1 ) ,
83
+ limit : limit ,
84
+ } ;
85
+ return this . billingplanService . getBillingDetails ( filter ) . pipe (
86
+ map ( res => {
87
+ const rows = res . map ( item => {
52
88
return {
53
89
companyName : item . companyName ,
54
90
userName : item . userName ,
@@ -58,6 +94,12 @@ export class BillingPlanComponent
58
94
status : SubscriptionStatus [ item . status ] ,
59
95
} ;
60
96
} ) ;
61
- } ) ;
97
+ return rows ;
98
+ } ) ,
99
+ ) ;
100
+ }
101
+
102
+ getTotal ( ) : Observable < Count > {
103
+ return this . billingplanService . getTotalBillingPlan ( ) ;
62
104
}
63
105
}
0 commit comments