diff options
| author | transtrike <transtrike@gmail.com> | 2021-02-12 19:04:53 +0200 |
|---|---|---|
| committer | transtrike <transtrike@gmail.com> | 2021-02-12 19:04:53 +0200 |
| commit | bcd88af53b1a920d728ec98b45daa9ac2e2c0917 (patch) | |
| tree | fd27eef086822b0f02f74364cac0b940956d2458 /src/app/components/register/register.component.ts | |
| parent | 1d1f05e3f74d70a558b4847a9107afa7952131cf (diff) | |
| download | DevHive-Angular-bcd88af53b1a920d728ec98b45daa9ac2e2c0917.tar DevHive-Angular-bcd88af53b1a920d728ec98b45daa9ac2e2c0917.tar.gz DevHive-Angular-bcd88af53b1a920d728ec98b45daa9ac2e2c0917.zip | |
Moved from DevHive
Diffstat (limited to 'src/app/components/register/register.component.ts')
| -rw-r--r-- | src/app/components/register/register.component.ts | 86 |
1 files changed, 86 insertions, 0 deletions
diff --git a/src/app/components/register/register.component.ts b/src/app/components/register/register.component.ts new file mode 100644 index 0000000..36eaa55 --- /dev/null +++ b/src/app/components/register/register.component.ts @@ -0,0 +1,86 @@ +import { HttpErrorResponse } from '@angular/common/http'; +import { Component, OnInit, ViewChild } from '@angular/core'; +import { AbstractControl, FormBuilder, FormControl, FormGroup, Validators } from '@angular/forms'; +import { Title } from '@angular/platform-browser'; +import { Router } from '@angular/router'; +import { TokenService } from 'src/app/services/token.service'; +import { UserService } from 'src/app/services/user.service'; +import { ErrorBarComponent } from '../error-bar/error-bar.component'; + +@Component({ + selector: 'app-register', + templateUrl: './register.component.html', + styleUrls: ['./register.component.css'] +}) +export class RegisterComponent implements OnInit { + @ViewChild(ErrorBarComponent) private _errorBar: ErrorBarComponent; + private _title = 'Register'; + public registerUserFormGroup: FormGroup; + + constructor(private _titleService: Title, private _fb: FormBuilder, private _router: Router, private _userService: UserService, private _tokenService: TokenService) { + this._titleService.setTitle(this._title); + } + + ngOnInit(): void { + this.registerUserFormGroup = this._fb.group({ + firstName: new FormControl('', [ + Validators.required, + Validators.minLength(3) + ]), + lastName: new FormControl('', [ + Validators.required, + Validators.minLength(3) + ]), + username: new FormControl('', [ + Validators.required, + Validators.minLength(3) + ]), + email: new FormControl('', [ + Validators.required, + Validators.email, + ]), + password: new FormControl('', [ + Validators.required, + Validators.minLength(3), + Validators.pattern('.*[0-9].*') // Check if password contains atleast one number + ]), + }); + + // this.registerUserFormGroup.valueChanges.subscribe(console.log); + } + + onSubmit(): void { + this._userService.registerUserRequest(this.registerUserFormGroup).subscribe( + res => { + this._tokenService.setUserTokenToSessionStorage(res); + this._router.navigate(['/']); + }, + (err: HttpErrorResponse) => { + this._errorBar.showError(err); + } + ); + } + onRedirectLogin(): void { + this._router.navigate(['/login']); + } + + get firstName(): AbstractControl | null { + return this.registerUserFormGroup.get('firstName'); + } + + get lastName(): AbstractControl | null { + return this.registerUserFormGroup.get('lastName'); + } + + get username(): AbstractControl | null { + return this.registerUserFormGroup.get('username'); + } + + get email(): AbstractControl | null { + return this.registerUserFormGroup.get('email'); + } + + get password(): AbstractControl | null { + return this.registerUserFormGroup.get('password'); + } +} |
