1 import { Component, Input, OnChanges, SimpleChanges, TemplateRef, ViewChild } from '@angular/core';
2 import { NgbModalRef } from '@ng-bootstrap/ng-bootstrap';
3 import { ActionLabelsI18n } from '~/app/shared/constants/app.constants';
4 import { Icons } from '~/app/shared/enum/icons.enum';
5 import { CdTableAction } from '~/app/shared/models/cd-table-action';
6 import { CdTableColumn } from '~/app/shared/models/cd-table-column';
7 import { CdTableSelection } from '~/app/shared/models/cd-table-selection';
8 import { Permission } from '~/app/shared/models/permissions';
9 import { ModalService } from '~/app/shared/services/modal.service';
10 import { RgwMultisiteService } from '~/app/shared/api/rgw-multisite.service';
11 import { CriticalConfirmationModalComponent } from '~/app/shared/components/critical-confirmation-modal/critical-confirmation-modal.component';
12 import { FinishedTask } from '~/app/shared/models/finished-task';
13 import { Observable, Subscriber, forkJoin as observableForkJoin } from 'rxjs';
14 import { TaskWrapperService } from '~/app/shared/services/task-wrapper.service';
15 import { TableComponent } from '~/app/shared/datatable/table/table.component';
16 import { RgwMultisiteSyncFlowModalComponent } from '../rgw-multisite-sync-flow-modal/rgw-multisite-sync-flow-modal.component';
17 import { FlowType } from '../models/rgw-multisite';
20 selector: 'cd-rgw-multisite-sync-policy-details',
21 templateUrl: './rgw-multisite-sync-policy-details.component.html',
22 styleUrls: ['./rgw-multisite-sync-policy-details.component.scss']
24 export class RgwMultisiteSyncPolicyDetailsComponent implements OnChanges {
28 permission: Permission;
30 @ViewChild(TableComponent)
31 table: TableComponent;
32 @ViewChild('deleteTpl', { static: true })
33 deleteTpl: TemplateRef<any>;
36 modalRef: NgbModalRef;
37 symmetricalFlowData: any = [];
38 directionalFlowData: any = [];
39 symmetricalFlowCols: CdTableColumn[];
40 directionalFlowCols: CdTableColumn[];
41 symFlowTableActions: CdTableAction[];
42 dirFlowTableActions: CdTableAction[];
43 symFlowSelection = new CdTableSelection();
44 dirFlowSelection = new CdTableSelection();
47 private actionLabels: ActionLabelsI18n,
48 private modalService: ModalService,
49 private rgwMultisiteService: RgwMultisiteService,
50 private taskWrapper: TaskWrapperService
52 this.symmetricalFlowCols = [
64 this.directionalFlowCols = [
71 name: 'Destination Zone',
76 const symAddAction: CdTableAction = {
79 name: this.actionLabels.CREATE,
80 click: () => this.openModal(FlowType.symmetrical),
81 canBePrimary: (selection: CdTableSelection) => !selection.hasSelection
83 const symEditAction: CdTableAction = {
86 name: this.actionLabels.EDIT,
87 click: () => this.openModal(FlowType.symmetrical, true)
89 const symDeleteAction: CdTableAction = {
92 disable: () => !this.symFlowSelection.hasSelection,
93 name: this.actionLabels.DELETE,
94 click: () => this.deleteFlow(FlowType.symmetrical),
95 canBePrimary: (selection: CdTableSelection) => selection.hasMultiSelection
97 this.symFlowTableActions = [symAddAction, symEditAction, symDeleteAction];
98 const dirAddAction: CdTableAction = {
101 name: this.actionLabels.CREATE,
102 click: () => this.openModal(FlowType.directional),
103 canBePrimary: (selection: CdTableSelection) => !selection.hasSelection
105 const dirEditAction: CdTableAction = {
106 permission: 'update',
108 name: this.actionLabels.EDIT,
109 click: () => this.openModal(FlowType.directional, true),
110 disable: () => true // TODO: disabling 'edit' as we are not getting flow ID from backend which is needed for edit
112 const dirDeleteAction: CdTableAction = {
113 permission: 'delete',
115 disable: () => true, // TODO: disabling 'delete' as we are not getting flow ID from backend which is needed for deletion
116 name: this.actionLabels.DELETE,
117 click: () => this.deleteFlow(FlowType.directional),
118 canBePrimary: (selection: CdTableSelection) => selection.hasMultiSelection
120 this.dirFlowTableActions = [dirAddAction, dirEditAction, dirDeleteAction];
123 ngOnChanges(changes: SimpleChanges): void {
124 if (changes.expandedRow.currentValue && changes.expandedRow.currentValue.groupName) {
125 this.symmetricalFlowData = [];
126 this.directionalFlowData = [];
131 loadFlowData(context?: any) {
132 if (this.expandedRow) {
133 this.rgwMultisiteService
134 .getSyncPolicyGroup(this.expandedRow.groupName, this.expandedRow.bucket)
137 this.symmetricalFlowData = policy.data_flow[FlowType.symmetrical] || [];
138 this.directionalFlowData = policy.data_flow[FlowType.directional] || [];
149 updateSelection(selection: any, type: FlowType) {
150 if (type === FlowType.directional) {
151 this.dirFlowSelection = selection;
153 this.symFlowSelection = selection;
157 async openModal(flowType: FlowType, edit = false) {
158 const action = edit ? 'edit' : 'create';
159 const initialState = {
161 groupExpandedRow: this.expandedRow,
163 flowType === FlowType.symmetrical
164 ? this.symFlowSelection.first()
165 : this.dirFlowSelection.first(),
169 this.modalRef = this.modalService.show(RgwMultisiteSyncFlowModalComponent, initialState, {
174 const res = await this.modalRef.result;
175 if (res === 'success') {
181 deleteFlow(flowType: FlowType) {
182 let selection = this.symFlowSelection;
183 if (flowType === FlowType.directional) {
184 selection = this.dirFlowSelection;
186 const flowIds = selection.selected.map((flow: any) => flow.id);
187 this.modalService.show(CriticalConfirmationModalComponent, {
188 itemDescription: selection.hasSingleSelection ? $localize`Flow` : $localize`Flows`,
190 bodyTemplate: this.deleteTpl,
191 submitActionObservable: () => {
192 return new Observable((observer: Subscriber<any>) => {
194 .wrapTaskAroundCall({
195 task: new FinishedTask('rgw/multisite/sync-flow/delete', {
198 call: observableForkJoin(
199 selection.selected.map((flow: any) => {
200 return this.rgwMultisiteService.removeSyncFlow(
203 this.expandedRow.groupName,
204 this.expandedRow.bucket
210 error: (error: any) => {
211 // Forward the error to the observer.
212 observer.error(error);
213 // Reload the data table content because some deletions might
214 // have been executed successfully in the meanwhile.
215 this.table.refreshBtn();
218 // Notify the observer that we are done.
220 // Reload the data table content.
221 this.table.refreshBtn();