From 1412362b79b4735c4385af64d10e16e7e6da7d31 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20G=C3=B3reczny?= Date: Sun, 13 Dec 2020 13:05:08 +0100 Subject: [PATCH] SES-89 Switched to subscribe --- .../registration/registration.component.html | 24 +++++++------- .../components/sign-in/sign-in.component.ts | 33 ++++++++++++------- .../ClientApp/src/services/user.service.ts | 15 +++++++-- 3 files changed, 46 insertions(+), 26 deletions(-) diff --git a/SessionCompanion/SessionCompanion/ClientApp/src/app/components/registration/registration.component.html b/SessionCompanion/SessionCompanion/ClientApp/src/app/components/registration/registration.component.html index 1f23ca5..54f6124 100644 --- a/SessionCompanion/SessionCompanion/ClientApp/src/app/components/registration/registration.component.html +++ b/SessionCompanion/SessionCompanion/ClientApp/src/app/components/registration/registration.component.html @@ -2,11 +2,11 @@
arrow_back
Create an Account
- + - lock @@ -32,12 +32,12 @@ - lock @@ -55,4 +55,4 @@ arrow_forward
- \ No newline at end of file + diff --git a/SessionCompanion/SessionCompanion/ClientApp/src/app/components/sign-in/sign-in.component.ts b/SessionCompanion/SessionCompanion/ClientApp/src/app/components/sign-in/sign-in.component.ts index 61488ad..aba66e0 100644 --- a/SessionCompanion/SessionCompanion/ClientApp/src/app/components/sign-in/sign-in.component.ts +++ b/SessionCompanion/SessionCompanion/ClientApp/src/app/components/sign-in/sign-in.component.ts @@ -1,14 +1,18 @@ -import { Component } from '@angular/core'; +import { Component, OnDestroy } from '@angular/core'; import { FormGroup, Validators, FormBuilder } from '@angular/forms'; import { Router } from '@angular/router'; import {UserService} from '../../../services/user.service'; +import {ErrorResponse} from '../../../types/ErrorResponse'; +import {Subscription} from 'rxjs'; @Component({ selector: 'app-sign-in', templateUrl: './sign-in.component.html', styleUrls: ['./sign-in.component.css'] }) -export class SignInComponent { +export class SignInComponent implements OnDestroy { + allSubscriptions = new Subscription(); + isExpanded = false; apiError = false; apiErrorMessage = ''; @@ -24,16 +28,19 @@ export class SignInComponent { }) }); - async onLoginButtonClick() { - const result = await this.userService.tryLogin( + onLoginButtonClick() { + this.allSubscriptions.add( + this.userService.tryLogin( this.signInFormGroup.get('signIn').value['username'], - this.signInFormGroup.get('signIn').value['password']); - if (result.isLeft) { - await this.router.navigate(['player']); - } else { - this.apiError = true; - this.apiErrorMessage = result.right.message; - } + this.signInFormGroup.get('signIn').value['password']).subscribe( + (success) => { + this.router.navigate(['player']); + }, + (error: ErrorResponse) => { + this.apiError = true; + this.apiErrorMessage = error.message; + } + )); } onRegisterButtonClick(){ @@ -52,4 +59,8 @@ export class SignInComponent { toggle() { this.isExpanded = !this.isExpanded; } + + ngOnDestroy() { + this.allSubscriptions.unsubscribe(); + } } diff --git a/SessionCompanion/SessionCompanion/ClientApp/src/services/user.service.ts b/SessionCompanion/SessionCompanion/ClientApp/src/services/user.service.ts index 08718fe..fda8c51 100644 --- a/SessionCompanion/SessionCompanion/ClientApp/src/services/user.service.ts +++ b/SessionCompanion/SessionCompanion/ClientApp/src/services/user.service.ts @@ -1,8 +1,9 @@ import {Inject, Injectable} from '@angular/core'; import {HttpClient, HttpParams} from '@angular/common/http'; -import {Observable} from 'rxjs'; +import {Observable, of, throwError} from 'rxjs'; import {ErrorResponse} from '../types/ErrorResponse'; import {Either} from '../types/Either'; +import {switchMap} from 'rxjs/operators'; Injectable({ providedIn: 'root' @@ -13,10 +14,18 @@ export class UserService { this.baseUrl = baseUrl + this.baseUrl; } - async tryLogin(login: string, password: string): Promise> { + tryLogin(login: string, password: string): Observable { const params = new HttpParams() .set('userName', login) .set('password', password); - return await this.http.get>(this.baseUrl + 'login', { params }).toPromise(); + return this.http.get>(this.baseUrl + 'login', { params }).pipe( + switchMap(response => { + if (response.isLeft) { + return of(response.left); + } else { + return throwError(response.right); + } + }) + ); } }