9 } from '@angular/core';
10 import { BehaviorSubject, Observable, of } from 'rxjs';
11 import { RgwBucketService } from '~/app/shared/api/rgw-bucket.service';
12 import { ActionLabelsI18n } from '~/app/shared/constants/app.constants';
13 import { TableComponent } from '~/app/shared/datatable/table/table.component';
14 import { CdTableAction } from '~/app/shared/models/cd-table-action';
15 import { CdTableColumn } from '~/app/shared/models/cd-table-column';
16 import { CdTableSelection } from '~/app/shared/models/cd-table-selection';
17 import { Permission } from '~/app/shared/models/permissions';
18 import { AuthStorageService } from '~/app/shared/services/auth-storage.service';
19 import { URLBuilderService } from '~/app/shared/services/url-builder.service';
20 import { Bucket } from '../models/rgw-bucket';
21 import { CdTableFetchDataContext } from '~/app/shared/models/cd-table-fetch-data-context';
22 import { CellTemplate } from '~/app/shared/enum/cell-template.enum';
23 import { catchError, switchMap } from 'rxjs/operators';
24 import { ModalCdsService } from '~/app/shared/services/modal-cds.service';
25 import { NotificationService } from '~/app/shared/services/notification.service';
26 import { ListWithDetails } from '~/app/shared/classes/list-with-details.class';
27 import { TopicConfiguration } from '~/app/shared/models/notification-configuration.model';
28 import { RgwNotificationFormComponent } from '../rgw-notification-form/rgw-notification-form.component';
29 import { DeleteConfirmationModalComponent } from '~/app/shared/components/delete-confirmation-modal/delete-confirmation-modal.component';
30 import { NotificationType } from '~/app/shared/enum/notification-type.enum';
31 import { Icons } from '~/app/shared/enum/icons.enum';
33 const BASE_URL = 'rgw/bucket';
35 selector: 'cd-rgw-bucket-notification-list',
36 templateUrl: './rgw-bucket-notification-list.component.html',
37 styleUrl: './rgw-bucket-notification-list.component.scss',
38 providers: [{ provide: URLBuilderService, useValue: new URLBuilderService(BASE_URL) }]
40 export class RgwBucketNotificationListComponent extends ListWithDetails implements OnInit {
41 @Input() bucket: Bucket;
42 @Output() updateBucketDetails = new EventEmitter();
43 @ViewChild(TableComponent, { static: true })
44 table: TableComponent;
45 permission: Permission;
46 tableActions: CdTableAction[];
47 columns: CdTableColumn[] = [];
48 selection: CdTableSelection = new CdTableSelection();
49 notification$: Observable<TopicConfiguration[]>;
50 subject = new BehaviorSubject<TopicConfiguration[]>([]);
51 context: CdTableFetchDataContext;
52 @ViewChild('filterTpl', { static: true })
53 filterTpl: TemplateRef<any>;
54 @ViewChild('eventTpl', { static: true })
55 eventTpl: TemplateRef<any>;
58 private rgwBucketService: RgwBucketService,
59 private authStorageService: AuthStorageService,
60 public actionLabels: ActionLabelsI18n,
61 private modalService: ModalCdsService,
62 private notificationService: NotificationService
68 this.permission = this.authStorageService.getPermissions().rgw;
71 name: $localize`Name`,
76 name: $localize`Topic`,
79 cellTransformation: CellTemplate.copy
82 name: $localize`Event`,
85 cellTemplate: this.eventTpl
88 name: $localize`Filter`,
91 cellTemplate: this.filterTpl
95 const createAction: CdTableAction = {
98 click: () => this.openNotificationModal(this.actionLabels.CREATE),
99 name: this.actionLabels.CREATE
101 const editAction: CdTableAction = {
102 permission: 'update',
104 disable: () => this.selection.hasMultiSelection,
105 click: () => this.openNotificationModal(this.actionLabels.EDIT),
106 name: this.actionLabels.EDIT
108 const deleteAction: CdTableAction = {
109 permission: 'delete',
111 click: () => this.deleteAction(),
112 disable: () => !this.selection.hasSelection,
113 name: this.actionLabels.DELETE,
114 canBePrimary: (selection: CdTableSelection) => selection.hasMultiSelection
116 this.tableActions = [createAction, editAction, deleteAction];
117 this.notification$ = this.subject.pipe(
119 this.rgwBucketService.listNotification(this.bucket.bucket).pipe(
120 catchError((error) => {
121 this.context.error(error);
130 this.subject.next([]);
133 openNotificationModal(type: string) {
134 const modalRef = this.modalService.show(RgwNotificationFormComponent, {
136 selectedNotification: this.selection.first(),
137 editing: type === this.actionLabels.EDIT ? true : false
139 modalRef?.close?.subscribe(() => this.updateBucketDetails.emit());
142 updateSelection(selection: CdTableSelection) {
143 this.selection = selection;
147 const selectedNotificationId = this.selection.selected.map((notification) => notification.Id);
148 this.modalRef = this.modalService.show(DeleteConfirmationModalComponent, {
149 itemDescription: $localize`Notification`,
150 itemNames: selectedNotificationId,
151 actionDescription: $localize`delete`,
152 submitAction: () => this.submitDeleteNotifications(selectedNotificationId)
156 submitDeleteNotifications(notificationId: string[]) {
157 this.rgwBucketService
158 .deleteNotification(this.bucket.bucket, notificationId.join(','))
161 this.notificationService.show(
162 NotificationType.success,
163 $localize`Notifications deleted successfully.`
165 this.modalService.dismissAll();
168 this.notificationService.show(
169 NotificationType.success,
170 $localize`Failed to delete notifications. Please try again.`
174 this.modalRef?.close?.subscribe(() => this.updateBucketDetails.emit());