From 5db5cf17e1804cff20294baa830fc394ba28a50a Mon Sep 17 00:00:00 2001 From: programmingPug <36635276+programmingPug@users.noreply.github.com> Date: Tue, 24 Apr 2018 09:16:54 -0400 Subject: [PATCH] username works! --- src/app/app.module.ts | 11 ++-- src/app/register/register.component.ts | 5 +- src/app/validators/username.validator.ts | 64 +++++++++++++----------- 3 files changed, 43 insertions(+), 37 deletions(-) diff --git a/src/app/app.module.ts b/src/app/app.module.ts index fa014fe..01d4886 100644 --- a/src/app/app.module.ts +++ b/src/app/app.module.ts @@ -24,10 +24,11 @@ import {MatFormFieldModule} from '@angular/material/form-field'; import { LoginComponent } from './login/login.component'; import { RegisterComponent } from './register/register.component'; + import { RegistrationService } from './registration.service'; -import { CreditCardValidator } from './creditcardvalidator.directive' -import { CreditCardValidatorX } from './creditcardvalidatorx.directive' +//import { CreditCardValidator } from './creditcardvalidator.directive' +import { UsernameValidator } from './validators/username.validator' @@ -37,9 +38,7 @@ import { CreditCardValidatorX } from './creditcardvalidatorx.directive' GameGridComponent, ViewCardComponent, LoginComponent, - RegisterComponent, - CreditCardValidator, - CreditCardValidatorX + RegisterComponent ], imports: [ BrowserModule, @@ -56,7 +55,7 @@ import { CreditCardValidatorX } from './creditcardvalidatorx.directive' MatPaginatorModule, MatFormFieldModule ], - providers: [GamesService, RegistrationService], + providers: [GamesService, RegistrationService, UsernameValidator], bootstrap: [AppComponent] }) export class AppModule { } diff --git a/src/app/register/register.component.ts b/src/app/register/register.component.ts index a9d2194..0ad7f4b 100644 --- a/src/app/register/register.component.ts +++ b/src/app/register/register.component.ts @@ -3,6 +3,7 @@ import { ActivatedRoute, Router } from '@angular/router'; import { FormBuilder, FormGroup, FormControl, Validators, AbstractControl } from '@angular/forms'; import { RegistrationService } from '../registration.service'; +import { UsernameValidator } from '../validators/username.validator' //import { ValidateUserName } from '../validators/username.validator'; @@ -19,7 +20,8 @@ export class RegisterComponent implements OnInit { //userNameValidator = new UserNameValidator(null); constructor( - private registrationService: RegistrationService + private registrationService: RegistrationService, + private usernameValidator: UsernameValidator ){ } ngOnInit() { @@ -79,6 +81,7 @@ export class RegisterComponent implements OnInit { }else if( key == "ValidateUserName" ){ //formValidators.push( this.validateUserName ); //formValidators.push( CreditCardValidator.validateCcNumber ); + formValidators.push( this.usernameValidator.checkUsername.bind( this.usernameValidator ) ); } } return formValidators; diff --git a/src/app/validators/username.validator.ts b/src/app/validators/username.validator.ts index 4cfb897..ecd0d53 100644 --- a/src/app/validators/username.validator.ts +++ b/src/app/validators/username.validator.ts @@ -1,31 +1,35 @@ - -import { AbstractControl } from '@angular/forms'; +import { Injectable } from '@angular/core'; +import { FormControl } from '@angular/forms'; import { RegistrationService } from '../registration.service'; - - -export class ValidateUserName{ - -constructor( - private RS: RegistrationService - ){ } - - validateUserName( control: AbstractControl ){ - //var RS = new RegistrationService(null); - /* - this.registrationService2.validateUserName( control.value ).subscribe( data => { - console.log( data ); - if( control.value != data.username ){ - return{ - validUserName: true - }; - } - return null; - - }); - */ - } - - -} - - + +@Injectable() +export class UsernameValidator { + + debouncer: any; + + constructor(private registrationService: RegistrationService){ + + } + + checkUsername(control: FormControl): any { + + clearTimeout(this.debouncer); + + return new Promise(resolve => { + + this.debouncer = setTimeout(() => { + + this.registrationService.validateUserName(control.value).subscribe((res) => { + if(res.ok){ + resolve(null); + } + }, (err) => { + resolve({'usernameInUse': true}); + }); + + }, 1000); + + }); + } + +} \ No newline at end of file