diff --git a/app/src/app/connection.service.ts b/app/src/app/connection.service.ts index 0e8dbb48..aacd30b2 100644 --- a/app/src/app/connection.service.ts +++ b/app/src/app/connection.service.ts @@ -5,6 +5,7 @@ import { Record } from '@web5/api'; import { RecordEntry } from './data'; import { connect } from '../../node_modules/undici-types/api.d'; import { DwnDateSort } from '@web5/agent'; +import { UtilityService } from './utility.service'; export interface ConnectionData { did: string; @@ -25,6 +26,8 @@ export interface ConnectionBlockEntry extends RecordEntry { export class ConnectionService { identity = inject(IdentityService); + utility = inject(UtilityService); + blocks = signal([]); connections = signal([]); @@ -94,16 +97,16 @@ export class ConnectionService { } as ConnectionEntry; } - async deleteBlock(entry: any) { + async deleteBlock(entry: ConnectionEntry) { await entry.record.delete(); - entry.record.send(this.identity.did); this.blocks.update((list) => [...list.filter((n) => n.id !== entry.id)]); + this.utility.executeAsyncWithToast(entry.record.send(this.identity.did)); } async deleteConnection(entry: any) { await entry.record.delete(); - entry.record.send(this.identity.did); this.connections.update((list) => [...list.filter((n) => n.id !== entry.id)]); + this.utility.executeAsyncWithToast(entry.record.send(this.identity.did)); } async block(did: string) { diff --git a/app/src/app/data/index.ts b/app/src/app/data/index.ts index 0f925786..e4a1c4d7 100644 --- a/app/src/app/data/index.ts +++ b/app/src/app/data/index.ts @@ -10,4 +10,5 @@ export interface RecordEntry { record: Record; id: string; data: T; + loading?: boolean; } diff --git a/app/src/app/notifications/notifications.component.ts b/app/src/app/notifications/notifications.component.ts index 16d3c302..c357dcb9 100644 --- a/app/src/app/notifications/notifications.component.ts +++ b/app/src/app/notifications/notifications.component.ts @@ -55,10 +55,6 @@ export class NotificationsComponent { } async accept(entry: NotificationEvent) { - if (entry.loading) { - return; - } - entry.loading = true; console.log('Accepting connection request'); @@ -92,10 +88,6 @@ export class NotificationsComponent { } async deleteNotification(entry: NotificationEvent) { - if (entry.loading) { - return; - } - entry.loading = true; const did = entry.record.author; @@ -118,10 +110,6 @@ export class NotificationsComponent { } async block(entry: NotificationEvent) { - if (entry.loading) { - return; - } - entry.loading = true; console.log('Blocking user', entry); diff --git a/app/src/app/settings/blocks/blocks.component.html b/app/src/app/settings/blocks/blocks.component.html index db8083e2..75626c44 100644 --- a/app/src/app/settings/blocks/blocks.component.html +++ b/app/src/app/settings/blocks/blocks.component.html @@ -15,7 +15,7 @@

Blocks Management

- + } @empty { No blocks found. } diff --git a/app/src/app/settings/blocks/blocks.component.ts b/app/src/app/settings/blocks/blocks.component.ts index d84bcb3b..ae544ad3 100644 --- a/app/src/app/settings/blocks/blocks.component.ts +++ b/app/src/app/settings/blocks/blocks.component.ts @@ -33,6 +33,8 @@ export class BlocksComponent { // } deleteBlock(entry: any) { + entry.loading = true; + this.service.deleteBlock(entry); } } diff --git a/app/src/app/settings/connections/connections.component.html b/app/src/app/settings/connections/connections.component.html index c40423f0..c8cd0902 100644 --- a/app/src/app/settings/connections/connections.component.html +++ b/app/src/app/settings/connections/connections.component.html @@ -15,7 +15,7 @@

Connections Management

- + } @empty { No connections found. } diff --git a/app/src/app/settings/connections/connections.component.ts b/app/src/app/settings/connections/connections.component.ts index 916df6cc..a062c6b4 100644 --- a/app/src/app/settings/connections/connections.component.ts +++ b/app/src/app/settings/connections/connections.component.ts @@ -32,7 +32,9 @@ export class ConnectionsComponent { // this.blocks.set(blocks); // } - deleteBlock(entry: any) { + deleteConnection(entry: any) { + entry.loading = true; + this.service.deleteConnection(entry); } } diff --git a/app/src/app/utility.service.spec.ts b/app/src/app/utility.service.spec.ts new file mode 100644 index 00000000..f2add0d9 --- /dev/null +++ b/app/src/app/utility.service.spec.ts @@ -0,0 +1,16 @@ +import { TestBed } from '@angular/core/testing'; + +import { UtilityService } from './utility.service'; + +describe('UtilityService', () => { + let service: UtilityService; + + beforeEach(() => { + TestBed.configureTestingModule({}); + service = TestBed.inject(UtilityService); + }); + + it('should be created', () => { + expect(service).toBeTruthy(); + }); +}); diff --git a/app/src/app/utility.service.ts b/app/src/app/utility.service.ts new file mode 100644 index 00000000..a4d3b233 --- /dev/null +++ b/app/src/app/utility.service.ts @@ -0,0 +1,36 @@ +import { inject, Injectable } from '@angular/core'; +import { AppService } from './app.service'; +import { MatSnackBar } from '@angular/material/snack-bar'; + +@Injectable({ + providedIn: 'root', +}) +export class UtilityService { + snackBar = inject(MatSnackBar); + + constructor() {} + + // executeAsyncWithoutWaiting(action: () => Promise) { + // action(); + // } + + async executeAsyncWithToast(action: Promise, successMessage?: string, errorMessage?: string) { + try { + await action; + + if (successMessage) { + this.openSnackBar(successMessage); + } + } catch (error) { + if (errorMessage) { + this.openSnackBar(errorMessage); + } else { + this.openSnackBar(`Error: ${error}.`); + } + } + } + + openSnackBar(message: string) { + this.snackBar.open(message, undefined, { duration: 2000 }); + } +}