-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Prevent page exit on the create page when there are unsaved changes (#…
…6566) * fix: prevent page exit with unsaved changes in create page * fix: remove unsaved changes confirm dialog on create page submit * Refactor code and use custom dialog for internal view change * Update conditions Co-authored-by: Sebastian Florek <sebastian.florek@kubermatic.com>
- Loading branch information
1 parent
0cb2f25
commit 254b752
Showing
12 changed files
with
234 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
// Copyright 2017 The Kubernetes Authors. | ||
// | ||
// Licensed 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 {MAT_DIALOG_DATA, MatDialogRef} from '@angular/material/dialog'; | ||
|
||
export interface ConfirmDialogConfig { | ||
title: string; | ||
message: string; | ||
} | ||
|
||
@Component({ | ||
selector: 'kd-confirm-dialog', | ||
templateUrl: 'template.html', | ||
}) | ||
export class ConfirmDialog { | ||
constructor(@Inject(MAT_DIALOG_DATA) public data: ConfirmDialogConfig) {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<!-- | ||
Copyright 2017 The Kubernetes Authors. | ||
Licensed 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. | ||
--> | ||
|
||
<h2 mat-dialog-title>{{data.title}}</h2> | ||
<mat-dialog-content class="kd-dialog-text">{{data.message}}</mat-dialog-content> | ||
<mat-dialog-actions> | ||
<button mat-button | ||
color="secondary" | ||
[mat-dialog-close]="false">No</button> | ||
|
||
<button mat-button | ||
color="primary" | ||
[mat-dialog-close]="true">Yes</button> | ||
</mat-dialog-actions> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
// Copyright 2017 The Kubernetes Authors. | ||
// | ||
// Licensed 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 {Directive, HostListener} from '@angular/core'; | ||
|
||
@Directive() | ||
export abstract class ICanDeactivate { | ||
abstract canDeactivate(): boolean; | ||
|
||
@HostListener('window:beforeunload', ['$event']) | ||
unloadNotification($event: any) { | ||
if (!this.canDeactivate()) { | ||
$event.returnValue = true; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
// Copyright 2017 The Kubernetes Authors. | ||
// | ||
// Licensed 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 {Injectable} from '@angular/core'; | ||
import {MatDialog} from '@angular/material/dialog'; | ||
import {CanDeactivate} from '@angular/router'; | ||
import {ConfirmDialog, ConfirmDialogConfig} from '@common/dialogs/config/dialog'; | ||
import {ICanDeactivate} from '@common/interfaces/candeactivate'; | ||
import {Observable, of} from 'rxjs'; | ||
|
||
@Injectable() | ||
export class CanDeactivateGuard implements CanDeactivate<ICanDeactivate> { | ||
constructor(private readonly dialog_: MatDialog) {} | ||
|
||
canDeactivate(component: ICanDeactivate): Observable<boolean> { | ||
if (component.canDeactivate()) { | ||
return of(true); | ||
} | ||
|
||
const config = { | ||
title: 'Unsaved changes', | ||
message: 'The form has not been submitted yet, do you really want to leave?', | ||
} as ConfirmDialogConfig; | ||
|
||
return this.dialog_.open(ConfirmDialog, {data: config}).afterClosed(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.