1
- import { Component , OnInit } from '@angular/core' ;
2
- import { CollectionCategoryQuery } from "fusio-sdk/dist/src/generated/backend/CollectionCategoryQuery" ;
1
+ import { Component } from '@angular/core' ;
3
2
import { ActivatedRoute , Router } from "@angular/router" ;
4
3
import { NgbModal } from "@ng-bootstrap/ng-bootstrap" ;
5
- import { AxiosResponse } from "axios" ;
6
- import { Collection } from "fusio-sdk/dist/src/generated/backend/Collection" ;
7
- import { Message } from "fusio-sdk/dist/src/generated/backend/Message" ;
8
- import { CollectionQuery } from "fusio-sdk/dist/src/generated/backend/CollectionQuery" ;
9
- import { FusioService } from "../service/fusio.service" ;
10
4
import { ClientAbstract } from "sdkgen-client" ;
5
+ import { FusioService } from "../service/fusio.service" ;
11
6
import { ErrorService } from "../service/error.service" ;
12
7
import { EventService } from "../service/event.service" ;
13
8
import { Result } from "./modal" ;
9
+ import { ModelId , Query } from "./query" ;
14
10
11
+ /**
12
+ * List panel which uses a modal to CRUD entities. Note it depends on ng-bootstrap so you can use it only if your
13
+ * project also uses ng-bootstrap
14
+ */
15
15
@Component ( {
16
16
template : '' ,
17
17
} )
18
- export abstract class List < C extends ClientAbstract , T extends ModelId > implements OnInit {
19
-
20
- public search : string = '' ;
21
- public totalResults : number = 0 ;
22
- public entries : Array < T > = [ ] ;
23
- public selected ?: T ;
24
- public page : number = 1 ;
25
- public pageSize : number = 16 ;
26
- public response ?: Message ;
27
- public loading : boolean = true ;
28
-
29
- constructor ( protected fusio : FusioService < C > , protected route : ActivatedRoute , protected router : Router , protected modalService : NgbModal , protected event : EventService , protected error : ErrorService ) {
30
- }
31
-
32
- async ngOnInit ( ) : Promise < void > {
33
- this . startLoading ( ) ;
34
- this . route . queryParams . subscribe ( async params => {
35
- let page , search ;
36
- if ( params [ 'page' ] ) {
37
- page = parseInt ( params [ 'page' ] ) ;
38
- }
39
- if ( params [ 'search' ] ) {
40
- search = params [ 'search' ] ;
41
- }
42
- if ( ! this . hasQueryParamsChange ( page , search ) ) {
43
- return ;
44
- }
45
- this . startLoading ( ) ;
46
- this . page = page || 1 ;
47
- this . search = search || '' ;
48
- await this . doList ( ) ;
49
- } ) ;
50
-
51
- this . route . paramMap . subscribe ( async params => {
52
- const id = params . get ( 'id' ) ;
53
- if ( id ) {
54
- this . startLoading ( ) ;
55
- await this . doGet ( id ) ;
56
- }
57
- } ) ;
58
- }
59
-
60
- async doList ( ) {
61
- try {
62
- const response = await this . getAll ( this . getCollectionQuery ( ) ) ;
63
-
64
- this . totalResults = response . data . totalResults || 0 ;
65
- this . entries = response . data . entry || [ ] ;
66
-
67
- this . onList ( ) ;
68
- } catch ( error ) {
69
- this . response = this . error . convert ( error ) ;
70
- }
71
-
72
- // in case we are not at a specific route redirect to the first
73
- const isDetailRoute : boolean | undefined = this . route . routeConfig ?. path ?. endsWith ( ':id' ) ;
74
- if ( this . entries . length > 0 && this . entries [ 0 ] . id && isDetailRoute === false && ! this . selected ) {
75
- await this . doGet ( '' + this . entries [ 0 ] . id ) ;
76
- }
77
-
78
- this . finishLoading ( ) ;
79
- }
80
-
81
- protected getCollectionQuery ( ) : CollectionQuery {
82
- let query : CollectionQuery = { } ;
83
- query . startIndex = ( this . page - 1 ) * this . pageSize ;
84
- query . count = this . pageSize ;
85
- if ( this . search ) {
86
- query . search = this . search ;
87
- }
88
- return query ;
89
- }
90
-
91
- async doGet ( id : string ) {
92
- try {
93
- const response = await this . get ( id ) ;
94
-
95
- this . selected = response . data ;
96
-
97
- this . onGet ( ) ;
98
- } catch ( error ) {
99
- this . response = this . error . convert ( error ) ;
100
- }
18
+ export abstract class List < C extends ClientAbstract , T extends ModelId > extends Query < C , T > {
101
19
102
- this . finishLoading ( ) ;
103
- }
104
-
105
- async doSearch ( page ?: number , search ?: string ) {
106
- if ( ! this . hasQueryParamsChange ( page , search ) || this . loading ) {
107
- return ;
108
- }
109
- if ( this . selected ) {
110
- await this . router . navigate ( [ this . getRoute ( ) , this . selected . id ] , {
111
- queryParams : this . getQueryParams ( page , search )
112
- } ) ;
113
- } else {
114
- await this . router . navigate ( [ this . getRoute ( ) ] , {
115
- queryParams : this . getQueryParams ( page , search )
116
- } ) ;
117
- }
118
- return false ;
119
- }
20
+ protected modalService : NgbModal ;
120
21
121
- async doSelect ( entry : T ) {
122
- await this . router . navigate ( [ this . getRoute ( ) , entry . id ] , {
123
- queryParams : this . getQueryParams ( this . page , this . search )
124
- } ) ;
22
+ constructor ( fusio : FusioService < C > , route : ActivatedRoute , router : Router , event : EventService , error : ErrorService , modalService : NgbModal ) {
23
+ super ( fusio , route , router , event , error ) ;
24
+ this . modalService = modalService ;
125
25
}
126
26
127
27
openCreateDialog ( ) {
@@ -136,7 +36,7 @@ export abstract class List<C extends ClientAbstract, T extends ModelId> implemen
136
36
137
37
await this . doList ( ) ;
138
38
}
139
- } )
39
+ } ) ;
140
40
}
141
41
142
42
openUpdateDialog ( entity : T ) {
@@ -152,8 +52,7 @@ export abstract class List<C extends ClientAbstract, T extends ModelId> implemen
152
52
153
53
await this . doList ( ) ;
154
54
}
155
- } )
156
-
55
+ } ) ;
157
56
}
158
57
159
58
openDeleteDialog ( entity : T ) {
@@ -169,60 +68,11 @@ export abstract class List<C extends ClientAbstract, T extends ModelId> implemen
169
68
170
69
await this . doList ( ) ;
171
70
}
172
- } )
173
- }
174
-
175
- getQueryParams ( page ?: number , search ?: string ) : QueryParams {
176
- const queryParams : QueryParams = { } ;
177
- if ( page ) {
178
- queryParams . page = page ;
179
- }
180
- if ( search ) {
181
- queryParams . search = search ;
182
- }
183
- return queryParams ;
184
- }
185
-
186
- hasQueryParamsChange ( page ?: number , search ?: string ) : boolean {
187
- return this . page !== page || this . search !== search ;
188
- }
189
-
190
- private startLoading ( ) : void {
191
- this . loading = true ;
192
- }
193
-
194
- private finishLoading ( ) : void {
195
- setTimeout ( ( ) => {
196
- this . loading = false ;
197
- } , 100 ) ;
198
- }
199
-
200
- protected abstract getAll ( query : CollectionCategoryQuery ) : Promise < AxiosResponse < Collection < T > > > ;
201
- protected abstract get ( id : string ) : Promise < AxiosResponse < T > > ;
202
- protected abstract getDetailComponent ( ) : any ;
203
- protected abstract getRoute ( ) : string ;
204
-
205
- protected onList ( ) : void
206
- {
207
- this . event . dispatchModelList ( this . getRoute ( ) ) ;
208
- }
209
-
210
- protected onGet ( ) : void
211
- {
212
- this . event . dispatchModelDetail ( this . selected , this . getRoute ( ) ) ;
71
+ } ) ;
213
72
}
214
73
215
74
}
216
75
217
- export interface QueryParams {
218
- page ?: number
219
- search ?: string
220
- }
221
-
222
- export interface ModelId {
223
- id ?: number | string
224
- }
225
-
226
76
export enum Mode {
227
77
Create = 1 ,
228
78
Update ,
0 commit comments