diff --git a/src/app/profile/user-profile/user-profile.component.html b/src/app/profile/user-profile/user-profile.component.html index d8e707b..1cac9f7 100644 --- a/src/app/profile/user-profile/user-profile.component.html +++ b/src/app/profile/user-profile/user-profile.component.html @@ -1,33 +1,41 @@
  Login Related
-
+
- - - First name is required + + + + + + Login is required + Login is used by someone else + Login should be email format
-
+
+ > Modify Login
+ + diff --git a/src/app/profile/user-profile/user-profile.component.ts b/src/app/profile/user-profile/user-profile.component.ts index 6795bbe..b21abd6 100644 --- a/src/app/profile/user-profile/user-profile.component.ts +++ b/src/app/profile/user-profile/user-profile.component.ts @@ -1,11 +1,12 @@ import {Component, Input, OnInit, ViewChild} from '@angular/core'; import {UserExtraModel} from '../../models/user-extra.model'; -import {NgForm} from '@angular/forms'; +import {FormControl, FormGroup, NgForm, Validators} from '@angular/forms'; import {AuthService} from '../../service/auth.service'; import {HttpClient} from '@angular/common/http'; import {MessageBoxComponent} from '../../message-box/message-box.component'; import {BrokerModel} from '../../models/broker.model'; import {PeopleService} from '../../service/people.service'; +import {asyncValidatorLoginAvailable} from '../../validator/unique.login.validator'; @Component({ selector: 'app-user-profile', @@ -19,17 +20,24 @@ export class UserProfileComponent implements OnInit { public showAdmin = false; public isCurrentUser = false; + + public UserForm: FormGroup; + constructor(private auth: AuthService, private http: HttpClient, private ps: PeopleService) { } ngOnInit(): void { + this.showAdmin = this.auth.isAdmin(); this.isCurrentUser = this.auth.isCurrentUser(this.PeopleId); + if (this.auth.isAdmin() && !this.auth.isCurrentUser(this.PeopleId)) { // edit other people, if ( this.PeopleId !== '' && this.UserExtra.Login === '' ){ // we need to load by ourself this.ps.getPeopleExtraById(this.PeopleId).subscribe( resp => { this.UserExtra = new UserExtraModel(resp); + this.UserForm.get('Login').setValue(this.UserExtra.Login); + this.UserForm.get('Enabled').setValue(this.UserExtra.Enabled); } ); } @@ -37,10 +45,17 @@ export class UserProfileComponent implements OnInit { this.PeopleId = this.auth.loggedIn.User.Id; this.UserExtra = this.auth.loggedIn.UserExtra; } + + this.UserForm = new FormGroup({ + Login: new FormControl({value: this.UserExtra.Login, disabled: ! this.showAdmin }, + [Validators.required, Validators.email], + [asyncValidatorLoginAvailable(this.auth, this.UserExtra.Login)] ), + Enabled: new FormControl({value: this.UserExtra.Enabled, disabled: this.isCurrentUser}), + }); } - public save(userForm: NgForm): void { - const expected = userForm.form.get('Login').value; + public save(): void { + const expected = this.UserForm.get('Login').value; this.http.post(this.auth.getUrl('user/' + this.PeopleId), this.UserExtra).subscribe( resp => { if ( resp === expected ) { @@ -62,4 +77,8 @@ export class UserProfileComponent implements OnInit { return this.UserExtra !== undefined && this.UserExtra.Login !== undefined; } + public onChangeLogin(): void { + console.log(this); + } + } diff --git a/src/app/service/auth.service.ts b/src/app/service/auth.service.ts index 0d7afa7..2ae63b1 100644 --- a/src/app/service/auth.service.ts +++ b/src/app/service/auth.service.ts @@ -4,6 +4,7 @@ import {ApiV1LoginResponse} from '../models/api-v1-login-response'; import {Router} from '@angular/router'; import {PeopleModel} from '../models/people.model'; import {UserExtraModel} from '../models/user-extra.model'; +import {Observable} from 'rxjs'; @Injectable() export class AuthService { @@ -138,19 +139,23 @@ export class AuthService { this.saveSessionInfo(); } - public isAdmin(): boolean { - return this.loggedIn.role === 'admin'; - } + public LoginAvailable(v: string): Observable { + return this.http.get(this.getUrl('login-available/' + v)); + } + + public isAdmin(): boolean { + return this.loggedIn.role === 'admin'; + } - public isBroker(): boolean { - return this.loggedIn.role === 'broker'; - } + public isBroker(): boolean { + return this.loggedIn.role === 'broker'; + } - public isUser(): boolean { - return this.loggedIn.login === true; - } + public isUser(): boolean { + return this.loggedIn.login === true; + } - public isCurrentUser(id: string): boolean { - return this.loggedIn.login === true && this.loggedIn.User !== undefined && this.loggedIn.User.Id === id; - } + public isCurrentUser(id: string): boolean { + return this.loggedIn.login === true && this.loggedIn.User !== undefined && this.loggedIn.User.Id === id; + } } diff --git a/src/app/top-bar/top-bar.component.ts b/src/app/top-bar/top-bar.component.ts index e117147..c93166e 100644 --- a/src/app/top-bar/top-bar.component.ts +++ b/src/app/top-bar/top-bar.component.ts @@ -29,7 +29,6 @@ export class TopBarComponent implements OnInit , OnDestroy { ngOnInit(): void { this.initAndSubLogin(); - console.log(this); } public initAndSubLogin(): void{ diff --git a/src/app/validator/unique.login.validator.ts b/src/app/validator/unique.login.validator.ts new file mode 100644 index 0000000..34cfe3e --- /dev/null +++ b/src/app/validator/unique.login.validator.ts @@ -0,0 +1,25 @@ +import {AbstractControl, AsyncValidatorFn, ValidationErrors} from '@angular/forms'; +import { Observable, of } from 'rxjs'; +import {AuthService} from '../service/auth.service'; +import {delay, map, switchMap} from 'rxjs/operators'; + + + +export function asyncValidatorLoginAvailable(auth: AuthService, existing: string): AsyncValidatorFn { + return (control: AbstractControl): Observable | null => { + const v: string = control.value; + + if (v === existing && v !== '') { + return of(null); + } + + return of(control.value).pipe( + delay(500), + switchMap( () => auth.LoginAvailable(v).pipe( + map((result: boolean) => result ? null : {loginAvailable: true}) + ) + ) + ); + }; +} +