feat: login components to handle workspace login and navigation

This commit is contained in:
Kifungo A
2024-10-14 09:30:37 +02:00
parent cdd314b752
commit be6b74d734
2 changed files with 83 additions and 8 deletions
@@ -1,5 +1,6 @@
import { Component } from '@angular/core';
import { Component, OnInit } from '@angular/core';
import { FormBuilder, FormControl, FormGroup, Validators } from '@angular/forms';
import { NavigationExtras, Router } from '@angular/router';
import { HttpStatus, IAuthResponse, IUser, IUserSigninWorkspaceResponse, IWorkspaceResponse } from '@gauzy/contracts';
import { UntilDestroy, untilDestroyed } from '@ngneat/until-destroy';
import { asyncScheduler, catchError, EMPTY, filter, tap } from 'rxjs';
@@ -12,13 +13,14 @@ import { ErrorHandlerService, Store, TimeTrackerDateManager } from '../../../ser
templateUrl: './login-workspace.component.html',
styleUrls: ['./login-workspace.component.scss']
})
export class NgxLoginWorkspaceComponent {
export class NgxLoginWorkspaceComponent implements OnInit {
public confirmedEmail: string;
public totalWorkspaces: number;
public showPopup: boolean = false;
public loading: boolean = false; // Flag to indicate if data loading is in progress
public workspaces: IWorkspaceResponse[] = []; // Array of workspace users
public showPassword = false;
private state: NavigationExtras['state'];
/** The FormGroup for the sign-in form */
public form: FormGroup = NgxLoginWorkspaceComponent.buildForm(this._fb);
@@ -40,8 +42,16 @@ export class NgxLoginWorkspaceComponent {
private readonly _store: Store,
private readonly _fb: FormBuilder,
private readonly _authService: AuthService,
private readonly _errorHandlingService: ErrorHandlerService
) {}
private readonly _errorHandlingService: ErrorHandlerService,
private readonly _router: Router
) {
const navigation = this._router.getCurrentNavigation();
this.state = navigation?.extras?.state as { email: string; password: string };
}
ngOnInit(): void {
this.handleWorkspaceNavigation();
}
/**
* Handle the form submission.
@@ -153,4 +163,12 @@ export class NgxLoginWorkspaceComponent {
public get email() {
return this.form.get('email');
}
private handleWorkspaceNavigation(): void {
if (this.state) {
if (!this.state.email || !this.state.password) return;
this.form.patchValue(this.state);
this.onSubmit();
}
}
}
@@ -1,10 +1,15 @@
import { ChangeDetectorRef, Component, Inject, OnInit } from '@angular/core';
import { Router } from '@angular/router';
import { ChangeDetectorRef, Component, Inject, OnInit, ViewChild } from '@angular/core';
import { NgForm } from '@angular/forms';
import { NavigationExtras, Router } from '@angular/router';
import { HttpStatus, IUserSigninWorkspaceResponse } from '@gauzy/contracts';
import { NB_AUTH_OPTIONS, NbAuthService, NbLoginComponent } from '@nebular/auth';
import { UntilDestroy } from '@ngneat/until-destroy';
import { UntilDestroy, untilDestroyed } from '@ngneat/until-destroy';
import { catchError, EMPTY, tap } from 'rxjs';
import { AuthService } from '../auth';
import { ElectronService } from '../electron/services';
import { LanguageElectronService } from '../language/language-electron.service';
import { GAUZY_ENV } from '../constants';
import { ErrorHandlerService } from '../services';
@UntilDestroy({ checkProperties: true })
@Component({
@@ -13,7 +18,9 @@ import { GAUZY_ENV } from '../constants';
styleUrls: ['./login.component.scss']
})
export class NgxLoginComponent extends NbLoginComponent implements OnInit {
showPassword = false;
@ViewChild('form')
public form: NgForm;
public showPassword = false;
constructor(
public readonly electronService: ElectronService,
@@ -21,6 +28,8 @@ export class NgxLoginComponent extends NbLoginComponent implements OnInit {
public readonly languageElectronService: LanguageElectronService,
public readonly cdr: ChangeDetectorRef,
public readonly router: Router,
private readonly authService: AuthService,
private readonly _errorHandlingService: ErrorHandlerService,
@Inject(NB_AUTH_OPTIONS)
options: any,
@Inject(GAUZY_ENV)
@@ -33,6 +42,54 @@ export class NgxLoginComponent extends NbLoginComponent implements OnInit {
this.languageElectronService.initialize<void>();
}
public override login(): void {
if (!this.form || this.form.invalid) {
return; // Exit if the form is invalid
}
this.submitted = true;
// Get the values of email and password from the form
const email = this.user.email;
const password = this.user.password;
// Send a request to sign in to workspaces using the authentication service
this.authService
.findWorkspaces({ email, password })
.pipe(
tap((response: any) => {
if (response['status'] === HttpStatus.UNAUTHORIZED) {
throw new Error(`${response['message']}`);
}
}),
// Update component state with the fetched workspaces
tap(async ({ total_workspaces }: IUserSigninWorkspaceResponse) => {
/** */
if (total_workspaces === 1) {
super.login();
} else {
const extra: NavigationExtras = {
state: {
email,
password
}
};
await this.router.navigate(['/', 'auth', 'login-workspace'], extra);
this.submitted = false;
}
}),
catchError((error) => {
this.submitted = false;
// Handle and log errors using the error handling service
this._errorHandlingService.handleError(error);
return EMPTY;
}),
// Handle component lifecycle to avoid memory leaks
untilDestroyed(this)
)
.subscribe();
}
public forgot(): void {
this.electronService.shell.openExternal(this.forgotPasswordUrl);
}