]>
Commit | Line | Data |
---|---|---|
11fdf7f2 TL |
1 | import { Component, ViewChild } from '@angular/core'; |
2 | ||
3 | import { I18n } from '@ngx-translate/i18n-polyfill'; | |
4 | import { BsModalService } from 'ngx-bootstrap/modal'; | |
5 | import { forkJoin as observableForkJoin, Observable, Subscriber } from 'rxjs'; | |
6 | ||
7 | import { RgwBucketService } from '../../../shared/api/rgw-bucket.service'; | |
8 | import { CriticalConfirmationModalComponent } from '../../../shared/components/critical-confirmation-modal/critical-confirmation-modal.component'; | |
9 | import { ActionLabelsI18n } from '../../../shared/constants/app.constants'; | |
10 | import { TableComponent } from '../../../shared/datatable/table/table.component'; | |
9f95a23c | 11 | import { Icons } from '../../../shared/enum/icons.enum'; |
11fdf7f2 TL |
12 | import { CdTableAction } from '../../../shared/models/cd-table-action'; |
13 | import { CdTableColumn } from '../../../shared/models/cd-table-column'; | |
14 | import { CdTableFetchDataContext } from '../../../shared/models/cd-table-fetch-data-context'; | |
15 | import { CdTableSelection } from '../../../shared/models/cd-table-selection'; | |
16 | import { Permission } from '../../../shared/models/permissions'; | |
17 | import { AuthStorageService } from '../../../shared/services/auth-storage.service'; | |
18 | import { URLBuilderService } from '../../../shared/services/url-builder.service'; | |
19 | ||
20 | const BASE_URL = 'rgw/bucket'; | |
21 | ||
22 | @Component({ | |
23 | selector: 'cd-rgw-bucket-list', | |
24 | templateUrl: './rgw-bucket-list.component.html', | |
25 | styleUrls: ['./rgw-bucket-list.component.scss'], | |
26 | providers: [{ provide: URLBuilderService, useValue: new URLBuilderService(BASE_URL) }] | |
27 | }) | |
28 | export class RgwBucketListComponent { | |
9f95a23c | 29 | @ViewChild(TableComponent, { static: true }) |
11fdf7f2 TL |
30 | table: TableComponent; |
31 | ||
32 | permission: Permission; | |
33 | tableActions: CdTableAction[]; | |
34 | columns: CdTableColumn[] = []; | |
35 | buckets: object[] = []; | |
36 | selection: CdTableSelection = new CdTableSelection(); | |
37 | ||
38 | constructor( | |
39 | private authStorageService: AuthStorageService, | |
40 | private rgwBucketService: RgwBucketService, | |
41 | private bsModalService: BsModalService, | |
42 | private i18n: I18n, | |
43 | private urlBuilder: URLBuilderService, | |
44 | public actionLabels: ActionLabelsI18n | |
45 | ) { | |
46 | this.permission = this.authStorageService.getPermissions().rgw; | |
47 | this.columns = [ | |
48 | { | |
49 | name: this.i18n('Name'), | |
50 | prop: 'bid', | |
51 | flexGrow: 1 | |
52 | }, | |
53 | { | |
54 | name: this.i18n('Owner'), | |
55 | prop: 'owner', | |
56 | flexGrow: 1 | |
57 | } | |
58 | ]; | |
59 | const getBucketUri = () => | |
60 | this.selection.first() && `${encodeURIComponent(this.selection.first().bid)}`; | |
61 | const addAction: CdTableAction = { | |
62 | permission: 'create', | |
9f95a23c | 63 | icon: Icons.add, |
11fdf7f2 | 64 | routerLink: () => this.urlBuilder.getCreate(), |
9f95a23c TL |
65 | name: this.actionLabels.CREATE, |
66 | canBePrimary: (selection: CdTableSelection) => !selection.hasSelection | |
11fdf7f2 TL |
67 | }; |
68 | const editAction: CdTableAction = { | |
69 | permission: 'update', | |
9f95a23c | 70 | icon: Icons.edit, |
11fdf7f2 TL |
71 | routerLink: () => this.urlBuilder.getEdit(getBucketUri()), |
72 | name: this.actionLabels.EDIT | |
73 | }; | |
74 | const deleteAction: CdTableAction = { | |
75 | permission: 'delete', | |
9f95a23c | 76 | icon: Icons.destroy, |
11fdf7f2 | 77 | click: () => this.deleteAction(), |
9f95a23c TL |
78 | disable: () => !this.selection.hasSelection, |
79 | name: this.actionLabels.DELETE, | |
80 | canBePrimary: (selection: CdTableSelection) => selection.hasMultiSelection | |
11fdf7f2 TL |
81 | }; |
82 | this.tableActions = [addAction, editAction, deleteAction]; | |
83 | } | |
84 | ||
85 | getBucketList(context: CdTableFetchDataContext) { | |
86 | this.rgwBucketService.list().subscribe( | |
87 | (resp: object[]) => { | |
88 | this.buckets = resp; | |
89 | }, | |
90 | () => { | |
91 | context.error(); | |
92 | } | |
93 | ); | |
94 | } | |
95 | ||
96 | updateSelection(selection: CdTableSelection) { | |
97 | this.selection = selection; | |
98 | } | |
99 | ||
100 | deleteAction() { | |
101 | this.bsModalService.show(CriticalConfirmationModalComponent, { | |
102 | initialState: { | |
103 | itemDescription: this.selection.hasSingleSelection | |
104 | ? this.i18n('bucket') | |
105 | : this.i18n('buckets'), | |
eafe8130 | 106 | itemNames: this.selection.selected.map((bucket: any) => bucket['bid']), |
11fdf7f2 TL |
107 | submitActionObservable: () => { |
108 | return new Observable((observer: Subscriber<any>) => { | |
109 | // Delete all selected data table rows. | |
110 | observableForkJoin( | |
111 | this.selection.selected.map((bucket: any) => { | |
112 | return this.rgwBucketService.delete(bucket.bid); | |
113 | }) | |
114 | ).subscribe( | |
115 | null, | |
116 | (error) => { | |
117 | // Forward the error to the observer. | |
118 | observer.error(error); | |
119 | // Reload the data table content because some deletions might | |
120 | // have been executed successfully in the meanwhile. | |
121 | this.table.refreshBtn(); | |
122 | }, | |
123 | () => { | |
124 | // Notify the observer that we are done. | |
125 | observer.complete(); | |
126 | // Reload the data table content. | |
127 | this.table.refreshBtn(); | |
128 | } | |
129 | ); | |
130 | }); | |
131 | } | |
132 | } | |
133 | }); | |
134 | } | |
135 | } |