Skip to content

Commit

Permalink
fix: update show message method to get custom error messages (#42)
Browse files Browse the repository at this point in the history
  • Loading branch information
OlfaBensoussia committed Aug 17, 2023
1 parent 954ab55 commit 82739c9
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ export class AssetViewerComponent implements OnInit {
constructor(private assetService: AssetService,
private notificationService: NotificationService,
private readonly dialog: MatDialog) {
}
}

private showError(error: string) {
this.notificationService.showError("This asset cannot be deleted");
private showError(error: string, errorMessage: string) {
this.notificationService.showError(errorMessage);
console.error(error);
}

Expand All @@ -51,27 +51,34 @@ export class AssetViewerComponent implements OnInit {
}

onDelete(asset: Asset) {

const dialogData = ConfirmDialogModel.forDelete("asset", `"${asset.name}"`)
const ref = this.dialog.open(ConfirmationDialogComponent, {maxWidth: "20%", data: dialogData});

ref.afterClosed().subscribe(res => {
if (res) {
this.assetService.removeAsset(asset.id).subscribe(() => this.fetch$.next(null),
err => this.showError(err),
() => this.notificationService.showInfo("Successfully deleted")
);

ref.afterClosed().subscribe({
next: res => {
if (res) {
this.assetService.removeAsset(asset.id).subscribe({
next: () => this.fetch$.next(null),
error: err => this.showError(err, "This asset cannot be deleted"),
complete: () => this.notificationService.showInfo("Successfully deleted")
});
}
}
});

}

onCreate() {
const dialogRef = this.dialog.open(AssetEditorDialog);
dialogRef.afterClosed().pipe(first()).subscribe((result: { assetEntryDto?: AssetEntryDto }) => {
const newAsset = result?.assetEntryDto;
if (newAsset) {
this.assetService.createAsset(newAsset).subscribe(() => this.fetch$.next(null), error => this.showError(error), () => this.notificationService.showInfo("Successfully created"));
dialogRef.afterClosed().pipe(first()).subscribe({
next: (result: { assetEntryDto?: AssetEntryDto }) => {
const newAsset = result?.assetEntryDto;
if (newAsset) {
this.assetService.createAsset(newAsset).subscribe({
next: () => this.fetch$.next(null),
error: error => this.showError(error, "This asset cannot be created"),
complete: () => this.notificationService.showInfo("Successfully created")
});
}
}
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@ export class PolicyViewComponent implements OnInit {

this.errorOrUpdateSubscriber = {
next: x => this.fetch$.next(null),
error: err => this.showError(err),
error: err => this.showError(err, "An error occurred."),
complete: () => {
this.notificationService.showInfo("Successfully completed")
},
}

}

ngOnInit(): void {
Expand All @@ -49,14 +49,21 @@ export class PolicyViewComponent implements OnInit {
}

onCreate() {
const dialogRef = this.dialog.open(NewPolicyDialogComponent)
dialogRef.afterClosed().pipe(first()).subscribe((result: PolicyDefinitionResponseDto) => {
if (result) {
this.policyService.createPolicy(result).subscribe(this.errorOrUpdateSubscriber);
const dialogRef = this.dialog.open(NewPolicyDialogComponent);
dialogRef.afterClosed().pipe(first()).subscribe({
next: (result: PolicyDefinitionResponseDto) => {
if (result) {
this.policyService.createPolicy(result).subscribe(
{
next: (response: IdResponseDto) => this.errorOrUpdateSubscriber.next(response),
error: (error: Error) => this.showError(error, "An error occurred while creating the policy.")
}
);
}
}
})
});
}

/**
* simple full-text search - serialize to JSON and see if "searchText"
* is contained
Expand All @@ -71,16 +78,24 @@ export class PolicyViewComponent implements OnInit {
const dialogData = ConfirmDialogModel.forDelete("policy", policyId);

const ref = this.dialog.open(ConfirmationDialogComponent, {maxWidth: '20%', data: dialogData});

ref.afterClosed().subscribe({

ref.afterClosed().subscribe(res => {
if (res) {
this.policyService.deletePolicy(policyId).subscribe(this.errorOrUpdateSubscriber);
next: (res: any) => {
if (res) {
this.policyService.deletePolicy(policyId).subscribe(
{
next: (response: IdResponseDto) => this.errorOrUpdateSubscriber.next(response),
error: (error: Error) => this.showError(error, "An error occurred while deleting the policy.")
}
);
}
}
});
}

private showError(error: Error) {
console.error(error)
this.notificationService.showError('This policy cannot be deleted');
private showError(error: Error, errorMessage: string) {
console.error(error);
this.notificationService.showError(errorMessage);
}
}

0 comments on commit 82739c9

Please sign in to comment.