scottyaslan commented on code in PR #8811: URL: https://github.com/apache/nifi/pull/8811#discussion_r1597085262
########## nifi-framework-bundle/nifi-framework/nifi-web/nifi-web-frontend/src/main/nifi/src/app/ui/common/close-on-escape-dialog/close-on-escape-dialog.component.ts: ########## @@ -0,0 +1,52 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { Component, inject } from '@angular/core'; +import { CommonModule } from '@angular/common'; +import { filter } from 'rxjs'; +import { MatDialogRef } from '@angular/material/dialog'; +import { takeUntilDestroyed } from '@angular/core/rxjs-interop'; + +@Component({ + selector: 'close-on-escape-dialog', + standalone: true, + imports: [CommonModule], + template: '' +}) +export abstract class CloseOnEscapeDialog { + private dialogRef: MatDialogRef<CloseOnEscapeDialog> = inject(MatDialogRef); + + protected constructor() { + if (this.dialogRef) { + this.dialogRef + .keydownEvents() + .pipe( + filter((event: KeyboardEvent) => event.key === 'Escape'), + takeUntilDestroyed() + ) + .subscribe(() => { + if (!this.isDirty()) { + this.dialogRef.close(); + } + }); + } Review Comment: If the form is dirty and the user types 'Esc' the user has no feedback as to why the dialog will not close. I think we could use the snackbar to inform the user to either apply the change or 'Cancel' to discard the changes. I also think the UX is a little inconsistent. The backdrop click should close the dialog when the form is not dirty same as the 'Esc' button. The following change will implement this: ```suggestion const store: Store<CanvasState> = inject(Store<any>); if (this.dialogRef) { this.dialogRef .keydownEvents() .pipe( filter((event: KeyboardEvent) => event.key === 'Escape'), takeUntilDestroyed() ) .subscribe(() => { if (!this.isDirty()) { this.dialogRef.close(); } else { store.dispatch( ErrorActions.snackBarError({ error: 'You have unsaved changes. Please apply the change or click \'Cancel\' to discard.' }) ); } }); this.dialogRef .backdropClick() .pipe(takeUntilDestroyed()) .subscribe(() => { if (!this.isDirty()) { this.dialogRef.close(); } else { store.dispatch( ErrorActions.snackBarError({ error: "You have unsaved changes. Please apply the change or click 'Cancel' to discard." }) ); } }); } ``` Maybe we can improve upon the message in the snackbar. I am not thrilled with it as is... -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
