Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix the unexpected confirmation dialog in 'Create from *' #6990

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/app/frontend/common/services/create/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,9 @@ export class CreateService {
} else {
this.router_.navigate(['overview'], {
queryParams: {[NAMESPACE_STATE_PARAM]: this.namespace_.current()},
state: {
bypassGuard: true,
},
});
}

Expand Down
14 changes: 7 additions & 7 deletions src/app/frontend/common/services/guard/candeactivate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,23 +14,23 @@

import {Injectable} from '@angular/core';
import {MatDialog} from '@angular/material/dialog';
import {CanDeactivate} from '@angular/router';
import {CanDeactivate, Router} from '@angular/router';
import {ConfirmDialog, ConfirmDialogConfig} from '@common/dialogs/config/dialog';
import {ICanDeactivate} from '@common/interfaces/candeactivate';
import {Observable, of} from 'rxjs';
import {Observable} from 'rxjs';

@Injectable()
export class CanDeactivateGuard implements CanDeactivate<ICanDeactivate> {
constructor(private readonly dialog_: MatDialog) {}
constructor(private readonly dialog_: MatDialog, private router: Router) {}

canDeactivate(component: ICanDeactivate): Observable<boolean> {
if (component.canDeactivate()) {
return of(true);
canDeactivate(component: ICanDeactivate): boolean | Observable<boolean> {
if (component.canDeactivate() || this.router.getCurrentNavigation()?.extras?.state?.bypassGuard) {
return true;
}

const config = {
title: 'Unsaved changes',
message: 'The form has not been submitted yet, do you really want to leave?',
message: 'The form has not been submitted yet. Do you really want to leave?',
} as ConfirmDialogConfig;

return this.dialog_.open(ConfirmDialog, {data: config}).afterClosed();
Expand Down
13 changes: 3 additions & 10 deletions src/app/frontend/create/from/file/component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

import {Component, ViewChild} from '@angular/core';
import {NgForm} from '@angular/forms';
import {Component} from '@angular/core';
import {KdFile} from '@api/root.ui';
import {ICanDeactivate} from '@common/interfaces/candeactivate';

Expand All @@ -28,8 +27,6 @@ import {NamespaceService} from '@common/services/global/namespace';
})
export class CreateFromFileComponent extends ICanDeactivate {
file: KdFile;
@ViewChild(NgForm, {static: true}) private readonly ngForm: NgForm;
private creating_ = false;

constructor(
private readonly namespace_: NamespaceService,
Expand All @@ -44,11 +41,7 @@ export class CreateFromFileComponent extends ICanDeactivate {
}

create(): void {
this.creating_ = true;
this.create_
.createContent(this.file.content, true, this.file.name)
.then(() => (this.creating_ = false))
.finally(() => (this.creating_ = false));
this.create_.createContent(this.file.content, true, this.file.name);
}

onFileLoad(file: KdFile): void {
Expand All @@ -64,6 +57,6 @@ export class CreateFromFileComponent extends ICanDeactivate {
}

canDeactivate(): boolean {
return this.isCreateDisabled() && !this.creating_;
return this.isCreateDisabled();
}
}
2 changes: 1 addition & 1 deletion src/app/frontend/create/from/file/template.html
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
label="Choose YAML or JSON file"
i18n-label> </kd-upload-file>

<button type="button"
<button type="submit"
[disabled]="isCreateDisabled()"
color="primary"
mat-raised-button
Expand Down
16 changes: 3 additions & 13 deletions src/app/frontend/create/from/form/component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,6 @@ export class CreateFromFormComponent extends ICanDeactivate implements OnInit {
labelArr: DeployLabel[] = [];
form: FormGroup;

private creating_ = false;

constructor(
private readonly namespace_: NamespaceService,
private readonly create_: CreateService,
Expand Down Expand Up @@ -178,10 +176,6 @@ export class CreateFromFormComponent extends ICanDeactivate implements OnInit {
this.imagePullSecret.patchValue('');
}

hasUnsavedChanges(): boolean {
return this.form.dirty;
}

isCreateDisabled(): boolean {
return !this.form.valid || this.create_.isDeployDisabled();
}
Expand Down Expand Up @@ -292,13 +286,9 @@ export class CreateFromFormComponent extends ICanDeactivate implements OnInit {
}

deploy(): void {
this.creating_ = true;
this.form.markAsPristine();
const spec = this.getSpec();

this.create_
.deploy(spec)
.then(() => (this.creating_ = false))
.finally(() => (this.creating_ = false));
this.create_.deploy(spec);
}

private getSpec(): AppDeploymentSpec {
Expand All @@ -325,6 +315,6 @@ export class CreateFromFormComponent extends ICanDeactivate implements OnInit {
}

canDeactivate(): boolean {
return !this.hasUnsavedChanges() && !this.creating_;
return this.form.pristine;
}
}
9 changes: 2 additions & 7 deletions src/app/frontend/create/from/input/component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import {NamespaceService} from '@common/services/global/namespace';
})
export class CreateFromInputComponent extends ICanDeactivate {
inputData = '';
private creating_ = false;

constructor(
private readonly namespace_: NamespaceService,
Expand All @@ -41,11 +40,7 @@ export class CreateFromInputComponent extends ICanDeactivate {
}

create(): void {
this.creating_ = true;
this.create_
.createContent(this.inputData)
.then(() => (this.creating_ = false))
.finally(() => (this.creating_ = false));
this.create_.createContent(this.inputData);
}

cancel(): void {
Expand All @@ -57,6 +52,6 @@ export class CreateFromInputComponent extends ICanDeactivate {
}

canDeactivate(): boolean {
return this.isCreateDisabled() && !this.creating_;
return this.isCreateDisabled();
}
}