Browse Source

Extend alert dialog with copy-to-clipboard functionality

pull/7258/head
Thomas Kaul 1 week ago
parent
commit
f7f58e718d
  1. 13
      apps/client/src/app/components/user-account-membership/user-account-membership.component.ts
  2. 20
      libs/ui/src/lib/notifications/alert-dialog/alert-dialog.component.ts
  3. 16
      libs/ui/src/lib/notifications/alert-dialog/alert-dialog.html
  4. 1
      libs/ui/src/lib/notifications/alert-dialog/interfaces/interfaces.ts
  5. 2
      libs/ui/src/lib/notifications/interfaces/interfaces.ts
  6. 7
      libs/ui/src/lib/notifications/notification.service.ts

13
apps/client/src/app/components/user-account-membership/user-account-membership.component.ts

@ -146,11 +146,16 @@ export class GfUserAccountMembershipComponent {
) )
.subscribe(({ apiKey }) => { .subscribe(({ apiKey }) => {
this.notificationService.alert({ this.notificationService.alert({
copyFn: () => {
this.snackBar.open(
'✅ ' + $localize`${apiKey} has been copied to the clipboard`,
undefined,
{ duration: ms('3 seconds') }
);
},
copyValue: apiKey,
discardLabel: $localize`Okay`, discardLabel: $localize`Okay`,
message: message: $localize`Set this API key in your self-hosted environment:`,
$localize`Set this API key in your self-hosted environment:` +
'<br />' +
apiKey,
title: $localize`Ghostfolio Premium Data Provider API Key` title: $localize`Ghostfolio Premium Data Provider API Key`
}); });
}); });

20
libs/ui/src/lib/notifications/alert-dialog/alert-dialog.component.ts

@ -1,3 +1,4 @@
import { Clipboard } from '@angular/cdk/clipboard';
import { ChangeDetectionStrategy, Component, inject } from '@angular/core'; import { ChangeDetectionStrategy, Component, inject } from '@angular/core';
import { MatButtonModule } from '@angular/material/button'; import { MatButtonModule } from '@angular/material/button';
import { MatDialogModule, MatDialogRef } from '@angular/material/dialog'; import { MatDialogModule, MatDialogRef } from '@angular/material/dialog';
@ -12,6 +13,7 @@ import { AlertDialogParams } from './interfaces/interfaces';
templateUrl: './alert-dialog.html' templateUrl: './alert-dialog.html'
}) })
export class GfAlertDialogComponent { export class GfAlertDialogComponent {
public copyValue?: string;
public discardLabel: string; public discardLabel: string;
public message?: string; public message?: string;
public title: string; public title: string;
@ -19,9 +21,25 @@ export class GfAlertDialogComponent {
protected readonly dialogRef = protected readonly dialogRef =
inject<MatDialogRef<GfAlertDialogComponent>>(MatDialogRef); inject<MatDialogRef<GfAlertDialogComponent>>(MatDialogRef);
public initialize({ discardLabel, message, title }: AlertDialogParams) { private readonly clipboard = inject(Clipboard);
public initialize({
copyValue,
discardLabel,
message,
title
}: AlertDialogParams) {
this.copyValue = copyValue;
this.discardLabel = discardLabel; this.discardLabel = discardLabel;
this.message = message; this.message = message;
this.title = title; this.title = title;
} }
public onCopyToClipboard() {
if (this.copyValue) {
this.clipboard.copy(this.copyValue);
}
this.dialogRef.close('copy');
}
} }

16
libs/ui/src/lib/notifications/alert-dialog/alert-dialog.html

@ -2,10 +2,22 @@
<div mat-dialog-title [innerHTML]="title"></div> <div mat-dialog-title [innerHTML]="title"></div>
} }
@if (message) { @if (message || copyValue) {
<div mat-dialog-content [innerHTML]="message"></div> <div mat-dialog-content>
@if (message) {
<div [innerHTML]="message"></div>
}
@if (copyValue) {
<div class="mt-2">{{ copyValue }}</div>
}
</div>
} }
<div align="end" mat-dialog-actions> <div align="end" mat-dialog-actions>
<button mat-button (click)="dialogRef.close()">{{ discardLabel }}</button> <button mat-button (click)="dialogRef.close()">{{ discardLabel }}</button>
@if (copyValue) {
<button color="primary" mat-flat-button (click)="onCopyToClipboard()">
<ng-container i18n>Copy</ng-container>
</button>
}
</div> </div>

1
libs/ui/src/lib/notifications/alert-dialog/interfaces/interfaces.ts

@ -1,4 +1,5 @@
export interface AlertDialogParams { export interface AlertDialogParams {
copyValue?: string;
discardLabel: string; discardLabel: string;
message?: string; message?: string;
title: string; title: string;

2
libs/ui/src/lib/notifications/interfaces/interfaces.ts

@ -1,6 +1,8 @@
import { ConfirmationDialogType } from '@ghostfolio/common/enums'; import { ConfirmationDialogType } from '@ghostfolio/common/enums';
export interface AlertParams { export interface AlertParams {
copyFn?: () => void;
copyValue?: string;
discardFn?: () => void; discardFn?: () => void;
discardLabel?: string; discardLabel?: string;
message?: string; message?: string;

7
libs/ui/src/lib/notifications/notification.service.ts

@ -31,13 +31,16 @@ export class NotificationService {
}); });
dialog.componentInstance.initialize({ dialog.componentInstance.initialize({
copyValue: aParams.copyValue,
discardLabel: aParams.discardLabel, discardLabel: aParams.discardLabel,
message: aParams.message, message: aParams.message,
title: aParams.title title: aParams.title
}); });
return dialog.afterClosed().subscribe(() => { return dialog.afterClosed().subscribe((result) => {
if (isFunction(aParams.discardFn)) { if (result === 'copy' && isFunction(aParams.copyFn)) {
aParams.copyFn();
} else if (isFunction(aParams.discardFn)) {
aParams.discardFn(); aParams.discardFn();
} }
}); });

Loading…
Cancel
Save