Skip to content

Answer: 48 #1316

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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 apps/forms/48-avoid-losing-form-data/src/app/app.routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ export const appRoutes: Route[] = [
{
path: 'form',
loadComponent: () => JoinComponent,
canDeactivate: [
(component: JoinComponent) => component.hasUnsavedChanges(),
],
},
{
path: 'page-1',
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,34 @@
import { ChangeDetectionStrategy, Component } from '@angular/core';
import { Dialog } from '@angular/cdk/dialog';
import { ChangeDetectionStrategy, Component, inject } from '@angular/core';
import { map } from 'rxjs';
import { AlertDialogComponent } from '../ui/dialog.component';
import { FormComponent } from '../ui/form.component';

@Component({
imports: [FormComponent],
template: `
<section class="mx-auto max-w-screen-sm">
<div class="rounded-lg bg-white p-8 shadow-lg lg:p-12">
<app-form />
<app-form (hasValue)="hasValue = $event" />
</div>
</section>
`,
changeDetection: ChangeDetectionStrategy.OnPush,
})
export class JoinComponent {}
export class JoinComponent {
hasValue = false;
dialog = inject(Dialog);

hasUnsavedChanges() {
if (this.hasValue) {
const dialogRef = this.dialog.open(AlertDialogComponent, {
disableClose: true,
ariaLabel: 'Alert dialog',
ariaDescribedBy: 'alert-description',
});
return dialogRef.closed.pipe(map((res) => res));
} else {
return true;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,24 +1,31 @@
import { ChangeDetectionStrategy, Component } from '@angular/core';
import { DialogRef } from '@angular/cdk/dialog';
import { ChangeDetectionStrategy, Component, inject } from '@angular/core';

// NOTE : this is just the dialog content, you need to implement dialog logic

@Component({
standalone: true,
template: `
<div role="alert" class="rounded-xl border border-gray-100 bg-white p-5">
<h3 class="block text-xl font-medium text-red-600">
<div
class="rounded-xl border border-gray-100 bg-white p-5"
role="alertdialog"
aria-labelledby="alert-title"
aria-describedby="alert-description">
<h3 id="alert-title" class="block text-xl font-medium text-red-600">
You have unsaved information!
</h3>

<p class="mt-1 text-gray-700">Do you want to continue and lose them?</p>

<p id="alert-description" class="mt-1 text-gray-700">
Do you want to continue and lose them?
</p>
<div class="mt-4 flex gap-2">
<button
(click)="dialogRef.close(true)"
class="inline-flex items-center gap-2 rounded-lg bg-red-600 px-4 py-2 text-white hover:bg-red-700">
Yes continue
</button>

<button
(click)="dialogRef.close(false)"
class="block rounded-lg px-4 py-2 text-gray-700 transition hover:bg-gray-50">
Stay on page
</button>
Expand All @@ -27,4 +34,6 @@ import { ChangeDetectionStrategy, Component } from '@angular/core';
`,
changeDetection: ChangeDetectionStrategy.OnPush,
})
export class AlertDialogComponent {}
export class AlertDialogComponent {
public dialogRef = inject(DialogRef);
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import { ChangeDetectionStrategy, Component, inject } from '@angular/core';
import {
ChangeDetectionStrategy,
Component,
inject,
output,
} from '@angular/core';
import { FormBuilder, ReactiveFormsModule, Validators } from '@angular/forms';

@Component({
Expand Down Expand Up @@ -63,6 +68,7 @@ import { FormBuilder, ReactiveFormsModule, Validators } from '@angular/forms';
})
export class FormComponent {
private fb = inject(FormBuilder);
hasValue = output<boolean>();

form = this.fb.nonNullable.group({
name: ['', { validators: [Validators.required] }],
Expand All @@ -74,4 +80,11 @@ export class FormComponent {
onSubmit() {
if (this.form.valid) this.form.reset();
}

ngOnInit() {
this.form.valueChanges.subscribe((value) => {
const hasValue = Object.values(value).some((v) => v.trim());
this.hasValue.emit(hasValue);
});
}
}
2 changes: 2 additions & 0 deletions apps/forms/48-avoid-losing-form-data/src/styles.scss
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,6 @@
@tailwind components;
@tailwind utilities;

@import '@angular/cdk/overlay-prebuilt.css'

/* You can add global styles to this file, and also import other style files */
Loading