|
1 | | -import {Component} from '@angular/core'; |
| 1 | +import {Component, OnInit} from '@angular/core'; |
2 | 2 | import {ActivatedRoute, Router} from "@angular/router"; |
3 | | -import {NgbModal} from "@ng-bootstrap/ng-bootstrap"; |
4 | | -import {ClientAbstract} from "sdkgen-client"; |
5 | | -import {ApiService} from "../service/api.service"; |
| 3 | +import {CommonMessage} from "fusio-sdk"; |
6 | 4 | import {ErrorService} from "../service/error.service"; |
7 | | -import {EventService} from "../service/event.service"; |
8 | | -import {Result} from "./modal"; |
9 | | -import {ModelId, Query} from "./query"; |
| 5 | +import {Service} from "./service"; |
10 | 6 |
|
11 | 7 | /** |
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 |
| 8 | + * Base component to list entities, with a pagination and a search |
14 | 9 | */ |
15 | 10 | @Component({ |
16 | 11 | template: '', |
17 | 12 | }) |
18 | | -export abstract class List<C extends ClientAbstract, T extends ModelId> extends Query<C, T> { |
| 13 | +export abstract class List<T> implements OnInit { |
19 | 14 |
|
20 | | - protected modalService: NgbModal; |
| 15 | + public search: string = ''; |
| 16 | + public totalResults: number = 0; |
| 17 | + public entries: Array<T> = []; |
| 18 | + public page: number = 1; |
| 19 | + public pageSize: number = 16; |
| 20 | + public response?: CommonMessage; |
21 | 21 |
|
22 | | - constructor(fusio: ApiService<C>, route: ActivatedRoute, router: Router, event: EventService, error: ErrorService, modalService: NgbModal) { |
23 | | - super(fusio, route, router, event, error); |
24 | | - this.modalService = modalService; |
| 22 | + protected constructor(protected route: ActivatedRoute, public router: Router, protected error: ErrorService) { |
25 | 23 | } |
26 | 24 |
|
27 | | - openCreateDialog() { |
28 | | - const modalRef = this.modalService.open(this.getDetailComponent(), { |
29 | | - size: 'lg' |
30 | | - }); |
31 | | - modalRef.componentInstance.mode = Mode.Create; |
32 | | - modalRef.closed.subscribe(async (result: Result<T>) => { |
33 | | - this.response = result.response; |
34 | | - if (result.response.success) { |
35 | | - this.event.dispatchModelCreated(result.entity, this.getRoute()); |
| 25 | + async ngOnInit(): Promise<void> { |
| 26 | + this.route.queryParams.subscribe(async params => { |
| 27 | + let page, search; |
| 28 | + if (params['page']) { |
| 29 | + page = parseInt(params['page']); |
| 30 | + } |
| 31 | + if (params['search']) { |
| 32 | + search = params['search']; |
| 33 | + } |
36 | 34 |
|
37 | | - await this.doList(); |
| 35 | + if (!this.hasQueryParamsChange(page, search)) { |
| 36 | + return; |
38 | 37 | } |
| 38 | + |
| 39 | + this.page = page || 1; |
| 40 | + this.search = search || ''; |
| 41 | + |
| 42 | + await this.doList(); |
39 | 43 | }); |
40 | 44 | } |
41 | 45 |
|
42 | | - openUpdateDialog(entity: T) { |
43 | | - const modalRef = this.modalService.open(this.getDetailComponent(), { |
44 | | - size: 'lg' |
45 | | - }); |
46 | | - modalRef.componentInstance.mode = Mode.Update; |
47 | | - modalRef.componentInstance.entity = entity; |
48 | | - modalRef.closed.subscribe(async (result: Result<T>) => { |
49 | | - this.response = result.response; |
50 | | - if (result.response.success) { |
51 | | - this.event.dispatchModelUpdated(result.entity, this.getRoute()); |
52 | | - |
53 | | - await this.doList(); |
54 | | - } |
55 | | - }); |
| 46 | + async doList() { |
| 47 | + try { |
| 48 | + const response = await this.getService().getAll(this.getCollectionQuery()); |
| 49 | + |
| 50 | + this.totalResults = response.totalResults || 0; |
| 51 | + this.entries = response.entry || []; |
| 52 | + |
| 53 | + this.onLoad(); |
| 54 | + } catch (error) { |
| 55 | + this.response = this.error.convert(error); |
| 56 | + } |
56 | 57 | } |
57 | 58 |
|
58 | | - openDeleteDialog(entity: T) { |
59 | | - const modalRef = this.modalService.open(this.getDetailComponent(), { |
60 | | - size: 'lg' |
61 | | - }); |
62 | | - modalRef.componentInstance.mode = Mode.Delete; |
63 | | - modalRef.componentInstance.entity = entity; |
64 | | - modalRef.closed.subscribe(async (result: Result<T>) => { |
65 | | - this.response = result.response; |
66 | | - if (result.response.success) { |
67 | | - this.event.dispatchModelDeleted(result.entity, this.getRoute()); |
68 | | - |
69 | | - await this.doList(); |
70 | | - } |
71 | | - }); |
| 59 | + async doSearch(page?: number, search?: string) { |
| 60 | + if (!this.hasQueryParamsChange(page, search)) { |
| 61 | + return; |
| 62 | + } |
| 63 | + |
| 64 | + if (page !== undefined) { |
| 65 | + this.page = page; |
| 66 | + } |
| 67 | + |
| 68 | + if (search !== undefined) { |
| 69 | + this.search = search; |
| 70 | + } |
| 71 | + |
| 72 | + await this.doList(); |
72 | 73 | } |
73 | 74 |
|
74 | | - protected abstract getDetailComponent(): any; |
| 75 | + public getDetailLink(id: any): Array<string> |
| 76 | + { |
| 77 | + const link = this.getService().getLink(); |
| 78 | + link.push('' + id) |
| 79 | + return link; |
| 80 | + } |
75 | 81 |
|
76 | | -} |
| 82 | + public getNewLink(): Array<string> |
| 83 | + { |
| 84 | + const link = this.getService().getLink(); |
| 85 | + link.push('-') |
| 86 | + link.push('new') |
| 87 | + return link; |
| 88 | + } |
| 89 | + |
| 90 | + public getEditLink(id: any): Array<string> |
| 91 | + { |
| 92 | + const link = this.getService().getLink(); |
| 93 | + link.push('' + id) |
| 94 | + link.push('edit') |
| 95 | + return link; |
| 96 | + } |
| 97 | + |
| 98 | + public getDeleteLink(id: any): Array<string> |
| 99 | + { |
| 100 | + const link = this.getService().getLink(); |
| 101 | + link.push('' + id) |
| 102 | + link.push('delete') |
| 103 | + return link; |
| 104 | + } |
| 105 | + |
| 106 | + protected getCollectionQuery(): Array<any> { |
| 107 | + let query: Array<any> = []; |
| 108 | + query.push((this.page - 1) * this.pageSize); |
| 109 | + query.push(this.pageSize); |
| 110 | + if (this.search) { |
| 111 | + query.push(this.search); |
| 112 | + } |
| 113 | + return query; |
| 114 | + } |
77 | 115 |
|
78 | | -export enum Mode { |
79 | | - Create = 1, |
80 | | - Update, |
81 | | - Delete, |
| 116 | + protected hasQueryParamsChange(page?: number, search?: string): boolean { |
| 117 | + return this.page !== page || this.search !== search; |
| 118 | + } |
| 119 | + |
| 120 | + protected abstract getService(): Service<T>; |
| 121 | + |
| 122 | + protected onLoad(): void |
| 123 | + { |
| 124 | + } |
82 | 125 | } |
0 commit comments