From 643fa5b715ed397cbb192e8bd65a6f2ea99d3e45 Mon Sep 17 00:00:00 2001 From: phil Date: Sun, 31 Mar 2024 17:48:36 +0530 Subject: [PATCH] Login: implement as dialog --- src/app/app-routing.module.ts | 3 +- src/app/app.component.html | 2 +- src/app/app.component.ts | 15 ++++++++++ src/app/login/login.component.css | 6 +++- src/app/login/login.component.html | 4 +-- src/app/login/login.component.spec.ts | 10 +++---- src/app/login/login.component.ts | 40 ++++++++++++++++----------- src/app/login/login.module.ts | 11 ++++++-- 8 files changed, 62 insertions(+), 29 deletions(-) diff --git a/src/app/app-routing.module.ts b/src/app/app-routing.module.ts index 24ad21a..530bc70 100644 --- a/src/app/app-routing.module.ts +++ b/src/app/app-routing.module.ts @@ -1,12 +1,11 @@ import { NgModule } from '@angular/core' import { RouterModule, Routes } from '@angular/router' import { configResolver } from './_services/config.service' -import { LoginComponent } from './login/login.component' +import { LoginDialogComponent } from './login/login.component' import { PageNotFoundComponent } from './pageNotFound.component' const routes: Routes = [ { path: '', redirectTo: '/dashboard', pathMatch: 'full' }, - { path: 'login', component: LoginComponent }, { path: 'dashboard', loadChildren: () => import('./dashboard/dashboard.module').then(m => m.DashboardModule) }, { path: 'map', loadChildren: () => import('./map/map.module').then(m => m.MapModule), resolve: {conf: configResolver}, }, { path: 'measures', loadChildren: () => import('./measures/measures.module').then(m => m.MeasuresModule) }, diff --git a/src/app/app.component.html b/src/app/app.component.html index 4f591f1..e3737be 100644 --- a/src/app/app.component.html +++ b/src/app/app.component.html @@ -43,7 +43,7 @@ diff --git a/src/app/app.component.ts b/src/app/app.component.ts index eee9b12..0f58c35 100644 --- a/src/app/app.component.ts +++ b/src/app/app.component.ts @@ -4,8 +4,10 @@ import { Title } from '@angular/platform-browser' import { BootstrapService } from './_services/bootstrap.service' import { ConfigService } from './_services/config.service' import { MatSnackBar } from '@angular/material/snack-bar' +import { MatDialog, MatDialogRef } from '@angular/material/dialog' import { AuthenticationService } from './_services/authentication.service' +import { LoginDialogComponent } from './login/login.component' @Component({ selector: 'app-root', @@ -42,6 +44,8 @@ export class AppComponent implements OnInit { public authenticationService: AuthenticationService, private snackBar: MatSnackBar, private cdr: ChangeDetectorRef, + public dialogRef: MatDialogRef, + public dialog: MatDialog ) {} ngOnInit() { @@ -69,4 +73,15 @@ export class AppComponent implements OnInit { } }) } + + openLoginDialog() { + const dialogRef = this.dialog.open(LoginDialogComponent, { + height: '24em', + width: '21em' + }) + + dialogRef.afterClosed().subscribe(result => { + console.log('The dialog was closed') + }) + } } diff --git a/src/app/login/login.component.css b/src/app/login/login.component.css index d20c094..e7b58ac 100644 --- a/src/app/login/login.component.css +++ b/src/app/login/login.component.css @@ -1,3 +1,7 @@ .error { - color: red + color: red; + text-align: center; + margin-top: 1em; + font-size: 125%; + font-weight: bolder; } \ No newline at end of file diff --git a/src/app/login/login.component.html b/src/app/login/login.component.html index 5178a22..9aa6b96 100644 --- a/src/app/login/login.component.html +++ b/src/app/login/login.component.html @@ -11,13 +11,13 @@ -
{{error}}
+
{{error}}
\ No newline at end of file diff --git a/src/app/login/login.component.spec.ts b/src/app/login/login.component.spec.ts index 093cec3..8790743 100644 --- a/src/app/login/login.component.spec.ts +++ b/src/app/login/login.component.spec.ts @@ -3,21 +3,21 @@ import { ComponentFixture, TestBed, waitForAsync } from '@angular/core/testing'; import { By } from '@angular/platform-browser'; import { DebugElement } from '@angular/core'; -import { LoginComponent } from './login.component'; +import { LoginDialogComponent } from './login.component'; describe('LoginComponent', () => { - let component: LoginComponent; - let fixture: ComponentFixture; + let component: LoginDialogComponent; + let fixture: ComponentFixture; beforeEach(waitForAsync(() => { TestBed.configureTestingModule({ - declarations: [ LoginComponent ] + declarations: [ LoginDialogComponent ] }) .compileComponents(); })); beforeEach(() => { - fixture = TestBed.createComponent(LoginComponent); + fixture = TestBed.createComponent(LoginDialogComponent); component = fixture.componentInstance; fixture.detectChanges(); }); diff --git a/src/app/login/login.component.ts b/src/app/login/login.component.ts index 351a959..2358d7f 100644 --- a/src/app/login/login.component.ts +++ b/src/app/login/login.component.ts @@ -1,30 +1,34 @@ import { Component, OnInit, ViewChild, ElementRef, - ChangeDetectorRef, ChangeDetectionStrategy + ChangeDetectorRef, ChangeDetectionStrategy, Inject } from '@angular/core' import { Router } from '@angular/router' - import { UntypedFormGroup, UntypedFormControl, Validators } from '@angular/forms' +import { MAT_DIALOG_DATA, MatDialog, MatDialogRef } from '@angular/material/dialog' import { ConfigService } from '../_services/config.service' import { AuthenticationService } from '../_services/authentication.service' -@Component({ - templateUrl: './login.component.html', - styleUrls: ['./login.component.css'], - changeDetection: ChangeDetectionStrategy.OnPush, -}) +export interface LoginDialogData { + username: string; + password: string; +} -export class LoginComponent implements OnInit { - loading = false +@Component({ + selector: 'gisaf-login-dialog', + templateUrl: 'login.component.html', + styleUrls: ['./login.component.css'], + standalone: false, +}) +export class LoginDialogComponent implements OnInit { error = '' formGroup: UntypedFormGroup @ViewChild('userName', { static: true }) userName: ElementRef constructor( - public configService: ConfigService, - private router: Router, + public dialogRef: MatDialogRef, + @Inject(MAT_DIALOG_DATA) public data: LoginDialogData, private authenticationService: AuthenticationService, private cdr: ChangeDetectorRef, ) { } @@ -37,19 +41,23 @@ export class LoginComponent implements OnInit { }) } + onNoClick(): void { + this.dialogRef.close(); + } + submit() { - this.loading = true this.authenticationService.login(this.formGroup.value.userName, this.formGroup.value.password).subscribe({ next: result => { - // login successful - this.router.navigate(['/']) + console.log('Login successful') + // this.router.navigate(['/']) + this.dialogRef.close() + this.cdr.markForCheck() }, error: error => { console.error(error) this.error = error.statusText - this.loading = false this.cdr.markForCheck() } }) } -} +} \ No newline at end of file diff --git a/src/app/login/login.module.ts b/src/app/login/login.module.ts index 6d58ea6..861a110 100644 --- a/src/app/login/login.module.ts +++ b/src/app/login/login.module.ts @@ -8,7 +8,8 @@ import { FormsModule, ReactiveFormsModule } from '@angular/forms' import { FlexLayoutModule } from 'ngx-flexible-layout' -import { LoginComponent } from './login.component' +import { LoginDialogComponent } from './login.component' +import { MatDialogRef } from '@angular/material/dialog' @NgModule({ imports: [ @@ -24,8 +25,14 @@ import { LoginComponent } from './login.component' FlexLayoutModule, ], declarations: [ - LoginComponent, + LoginDialogComponent, ], + providers: [ + { + provide: MatDialogRef, + useValue: {} + }, + ] }) export class LoginModule {}