2025-04-23 11:21:14 +02:00
|
|
|
import { ChangeDetectionStrategy, Component, DestroyRef } from '@angular/core';
|
|
|
|
|
import { lastValueFrom, Observable, of, ReplaySubject } from 'rxjs';
|
2025-04-02 16:53:06 +02:00
|
|
|
import { ActionService } from 'src/app/services/action.service';
|
|
|
|
|
import { ToastService } from 'src/app/services/toast.service';
|
|
|
|
|
import { takeUntilDestroyed } from '@angular/core/rxjs-interop';
|
2025-04-23 11:21:14 +02:00
|
|
|
import { catchError, map, startWith, switchMap } from 'rxjs/operators';
|
2025-04-02 16:53:06 +02:00
|
|
|
import { MatDialog } from '@angular/material/dialog';
|
|
|
|
|
import { ActionTwoAddTargetDialogComponent } from '../actions-two-add-target/actions-two-add-target-dialog.component';
|
|
|
|
|
import { MessageInitShape } from '@bufbuild/protobuf';
|
|
|
|
|
import { Target } from '@zitadel/proto/zitadel/action/v2beta/target_pb';
|
|
|
|
|
import {
|
|
|
|
|
CreateTargetRequestSchema,
|
|
|
|
|
UpdateTargetRequestSchema,
|
|
|
|
|
} from '@zitadel/proto/zitadel/action/v2beta/action_service_pb';
|
2025-04-29 13:25:49 +02:00
|
|
|
import { InfoSectionType } from '../../info-section/info-section.component';
|
2025-04-02 16:53:06 +02:00
|
|
|
|
|
|
|
|
@Component({
|
|
|
|
|
selector: 'cnsl-actions-two-targets',
|
|
|
|
|
templateUrl: './actions-two-targets.component.html',
|
|
|
|
|
styleUrls: ['./actions-two-targets.component.scss'],
|
|
|
|
|
changeDetection: ChangeDetectionStrategy.OnPush,
|
|
|
|
|
})
|
2025-04-23 11:21:14 +02:00
|
|
|
export class ActionsTwoTargetsComponent {
|
2025-04-02 16:53:06 +02:00
|
|
|
protected readonly targets$: Observable<Target[]>;
|
|
|
|
|
protected readonly refresh$ = new ReplaySubject<true>(1);
|
|
|
|
|
|
|
|
|
|
constructor(
|
|
|
|
|
private readonly actionService: ActionService,
|
|
|
|
|
private readonly toast: ToastService,
|
|
|
|
|
private readonly destroyRef: DestroyRef,
|
|
|
|
|
private readonly dialog: MatDialog,
|
|
|
|
|
) {
|
2025-04-23 11:21:14 +02:00
|
|
|
this.targets$ = this.getTargets$();
|
2025-04-02 16:53:06 +02:00
|
|
|
}
|
|
|
|
|
|
2025-04-23 11:21:14 +02:00
|
|
|
private getTargets$() {
|
2025-04-02 16:53:06 +02:00
|
|
|
return this.refresh$.pipe(
|
|
|
|
|
startWith(true),
|
|
|
|
|
switchMap(() => {
|
|
|
|
|
return this.actionService.listTargets({});
|
|
|
|
|
}),
|
|
|
|
|
map(({ result }) => result),
|
|
|
|
|
catchError((err) => {
|
2025-04-23 11:21:14 +02:00
|
|
|
this.toast.showError(err);
|
|
|
|
|
return of([]);
|
2025-04-02 16:53:06 +02:00
|
|
|
}),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async deleteTarget(target: Target) {
|
|
|
|
|
await this.actionService.deleteTarget({ id: target.id });
|
|
|
|
|
await new Promise((res) => setTimeout(res, 1000));
|
|
|
|
|
this.refresh$.next(true);
|
|
|
|
|
}
|
|
|
|
|
|
2025-04-23 11:21:14 +02:00
|
|
|
public async openDialog(target?: Target) {
|
|
|
|
|
const request$ = this.dialog
|
|
|
|
|
.open<
|
|
|
|
|
ActionTwoAddTargetDialogComponent,
|
|
|
|
|
{ target?: Target },
|
|
|
|
|
MessageInitShape<typeof UpdateTargetRequestSchema | typeof CreateTargetRequestSchema>
|
|
|
|
|
>(ActionTwoAddTargetDialogComponent, {
|
|
|
|
|
width: '550px',
|
|
|
|
|
data: {
|
|
|
|
|
target: target,
|
|
|
|
|
},
|
|
|
|
|
})
|
2025-04-02 16:53:06 +02:00
|
|
|
.afterClosed()
|
2025-04-23 11:21:14 +02:00
|
|
|
.pipe(takeUntilDestroyed(this.destroyRef));
|
|
|
|
|
|
|
|
|
|
const request = await lastValueFrom(request$);
|
|
|
|
|
if (!request) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
if ('id' in request) {
|
|
|
|
|
await this.actionService.updateTarget(request);
|
|
|
|
|
} else {
|
2025-04-29 13:25:49 +02:00
|
|
|
const resp = await this.actionService.createTarget(request);
|
|
|
|
|
console.log(`Your singing key: ${resp.signingKey}`);
|
2025-04-23 11:21:14 +02:00
|
|
|
}
|
2025-04-02 16:53:06 +02:00
|
|
|
|
2025-04-23 11:21:14 +02:00
|
|
|
await new Promise((res) => setTimeout(res, 1000));
|
|
|
|
|
this.refresh$.next(true);
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error(error);
|
|
|
|
|
this.toast.showError(error);
|
|
|
|
|
}
|
2025-04-02 16:53:06 +02:00
|
|
|
}
|
2025-04-29 13:25:49 +02:00
|
|
|
|
|
|
|
|
protected readonly InfoSectionType = InfoSectionType;
|
2025-04-02 16:53:06 +02:00
|
|
|
}
|