import { Component , OnInit , AfterViewInit , Renderer , ElementRef } from '@angular/core' ; import { HttpErrorResponse } from '@angular/common/http' ; import { FormBuilder , FormGroup , Validators } from '@angular/forms' ; import { NgbModalRef } from '@ng-bootstrap/ng-bootstrap' ; import { JhiLanguageService } from 'ng-jhipster' ; import { EMAIL_ALREADY_USED_TYPE , LOGIN_ALREADY_USED_TYPE } from 'app/shared' ; import { LoginModalService } from 'app/core' ; import { Register } from './register.service' ; @ Component ( { selector : 'jhi-register' , templateUrl : './register.component.html' } ) export class RegisterComponent implements OnInit , AfterViewInit { doNotMatch : string ; error : string ; errorEmailExists : string ; errorUserExists : string ; success : boolean ; modalRef : NgbModalRef ; registerForm = this . fb . group ( { login : [ '' , [ Validators . required , Validators . minLength ( 1 ) , Validators . maxLength ( 50 ) , Validators . pattern ( '^[_.@A-Za-z0-9-]*$' ) ] ] , email : [ '' , [ Validators . required , Validators . minLength ( 5 ) , Validators . maxLength ( 254 ) , Validators . email ] ] , password : [ '' , [ Validators . required , Validators . minLength ( 4 ) , Validators . maxLength ( 50 ) ] ] , confirmPassword : [ '' , [ Validators . required , Validators . minLength ( 4 ) , Validators . maxLength ( 50 ) ] ] } ) ; constructor ( private languageService , private loginModalService , private registerService , private elementRef , private renderer , private fb ) { } ngOnInit ( ) { this . success = false ; } ngAfterViewInit ( ) { this . renderer . invokeElementMethod ( this . elementRef . nativeElement . querySelector ( '#login' ) , 'focus' , [ ] ) ; } register ( ) { let registerAccount = { } ; const login = this . registerForm . get ( [ 'login' ] ) . value ; const email = this . registerForm . get ( [ 'email' ] ) . value ; const password = this . registerForm . get ( [ 'password' ] ) . value ; if ( password !== this . registerForm . get ( [ 'confirmPassword' ] ) . value ) { this . doNotMatch = 'ERROR' ; } else { registerAccount = { ... registerAccount , login , email , password } ; this . doNotMatch = null ; this . error = null ; this . errorUserExists = null ; this . errorEmailExists = null ; this . languageService . getCurrent ( ) . then ( langKey => { registerAccount = { ... registerAccount , langKey } ; this . registerService . save ( registerAccount ) . subscribe ( ( ) => { this . success = true ; } , response => this . processError ( response ) ) ; } ) ; } } openLogin ( ) { this . modalRef = this . loginModalService . open ( ) ; } private processError ( response ) { this . success = null ; if ( response . status === 400 && response . error . type === LOGIN_ALREADY_USED_TYPE ) { this . errorUserExists = 'ERROR' ; } else if ( response . status === 400 && response . error . type === EMAIL_ALREADY_USED_TYPE ) { this . errorEmailExists = 'ERROR' ; } else { this . error = 'ERROR' ; } } }