Procházet zdrojové kódy

async validator for checking valid login value.

tags/2.037
Patrick Sun před 4 roky
rodič
revize
6403a77a64
5 změnil soubory, kde provedl 82 přidání a 26 odebrání
  1. +18
    -10
      src/app/profile/user-profile/user-profile.component.html
  2. +22
    -3
      src/app/profile/user-profile/user-profile.component.ts
  3. +17
    -12
      src/app/service/auth.service.ts
  4. +0
    -1
      src/app/top-bar/top-bar.component.ts
  5. +25
    -0
      src/app/validator/unique.login.validator.ts

+ 18
- 10
src/app/profile/user-profile/user-profile.component.html Zobrazit soubor

<h5 *ngIf="canEditUser()" > <kendo-icon [name]="'ascx'" > </kendo-icon> &nbsp; Login Related </h5> <h5 *ngIf="canEditUser()" > <kendo-icon [name]="'ascx'" > </kendo-icon> &nbsp; Login Related </h5>


<form *ngIf="canEditUser()" class="k-form" #userForm="ngForm" (submit)="save(userForm)">
<form *ngIf="canEditUser()" class="k-form" [formGroup]="UserForm" (submit)="save()">
<ng-container > <ng-container >
<fieldset class="k-form-fieldset"> <fieldset class="k-form-fieldset">
<kendo-formfield> <kendo-formfield>
<kendo-label [for]="Login" text="Unique Login id:"></kendo-label>
<kendo-textbox kendoTextBox #Login name="Login" [(ngModel)]="UserExtra.Login"
[disabled] = "!showAdmin"
[showSuccessIcon]="UserExtra.Login.length >= 3 && UserExtra.Login.length <=44 "> </kendo-textbox>
<kendo-formerror>First name is required</kendo-formerror>
<kendo-label [for]=login text="Unique Login id: ">
<kendo-loader *ngIf="UserForm.get('Login').status === 'PENDING'"
[type]="'pulsing'"
[themeColor]="'primary'"
[size]="'medium'"
></kendo-loader>
</kendo-label>
<kendo-textbox #login kendoTextBox name="Login" formControlName="Login" (valueChange)="onChangeLogin()">
</kendo-textbox>
<kendo-formerror *ngIf="UserForm.get('Login').hasError('required')" >Login is required</kendo-formerror>
<kendo-formerror *ngIf="UserForm.get('Login').hasError('loginAvailable')" >Login is used by someone else </kendo-formerror>
<kendo-formerror *ngIf="UserForm.get('Login').hasError('email')" >Login should be email format</kendo-formerror>
</kendo-formfield> </kendo-formfield>
<div class="vertical-spacer"></div> <div class="vertical-spacer"></div>


<kendo-formfield *ngIf="showAdmin "> <kendo-formfield *ngIf="showAdmin ">
<label class="k-label">Allow Login &nbsp; <label class="k-label">Allow Login &nbsp;
<input type="checkbox" name="enabled" kendoCheckBox
[(ngModel)]="UserExtra.Enabled" [disabled]="isCurrentUser"/>
<input type="checkbox" name="enabled" kendoCheckBox formControlName="Enabled"/>
</label> </label>
</kendo-formfield> </kendo-formfield>
<div class="vertical-spacer"></div> <div class="vertical-spacer"></div>


</fieldset> </fieldset>
</ng-container> </ng-container>
<div class="k-form-buttons k-buttons-end">
<div class="k-form-buttons k-buttons-end" *ngIf="UserForm.get('Login').status !== 'PENDING'">
<div> <div>
<button kendoButton class="k-button k-primary" type="submit" icon="save" <button kendoButton class="k-button k-primary" type="submit" icon="save"
[disabled]="userForm.form.status !=='VALID'"> Modify Login</button>
> Modify Login</button>
</div> </div>
</div> </div>


</form> </form>


<app-message-box #messageBox ></app-message-box> <app-message-box #messageBox ></app-message-box>

+ 22
- 3
src/app/profile/user-profile/user-profile.component.ts Zobrazit soubor

import {Component, Input, OnInit, ViewChild} from '@angular/core'; import {Component, Input, OnInit, ViewChild} from '@angular/core';
import {UserExtraModel} from '../../models/user-extra.model'; 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 {AuthService} from '../../service/auth.service';
import {HttpClient} from '@angular/common/http'; import {HttpClient} from '@angular/common/http';
import {MessageBoxComponent} from '../../message-box/message-box.component'; import {MessageBoxComponent} from '../../message-box/message-box.component';
import {BrokerModel} from '../../models/broker.model'; import {BrokerModel} from '../../models/broker.model';
import {PeopleService} from '../../service/people.service'; import {PeopleService} from '../../service/people.service';
import {asyncValidatorLoginAvailable} from '../../validator/unique.login.validator';


@Component({ @Component({
selector: 'app-user-profile', selector: 'app-user-profile',


public showAdmin = false; public showAdmin = false;
public isCurrentUser = false; public isCurrentUser = false;

public UserForm: FormGroup;

constructor(private auth: AuthService, private http: HttpClient, private ps: PeopleService) { } constructor(private auth: AuthService, private http: HttpClient, private ps: PeopleService) { }


ngOnInit(): void { ngOnInit(): void {

this.showAdmin = this.auth.isAdmin(); this.showAdmin = this.auth.isAdmin();
this.isCurrentUser = this.auth.isCurrentUser(this.PeopleId); this.isCurrentUser = this.auth.isCurrentUser(this.PeopleId);

if (this.auth.isAdmin() && !this.auth.isCurrentUser(this.PeopleId)) { if (this.auth.isAdmin() && !this.auth.isCurrentUser(this.PeopleId)) {
// edit other people, // edit other people,
if ( this.PeopleId !== '' && this.UserExtra.Login === '' ){ // we need to load by ourself if ( this.PeopleId !== '' && this.UserExtra.Login === '' ){ // we need to load by ourself
this.ps.getPeopleExtraById(this.PeopleId).subscribe( this.ps.getPeopleExtraById(this.PeopleId).subscribe(
resp => { resp => {
this.UserExtra = new UserExtraModel(resp); this.UserExtra = new UserExtraModel(resp);
this.UserForm.get('Login').setValue(this.UserExtra.Login);
this.UserForm.get('Enabled').setValue(this.UserExtra.Enabled);
} }
); );
} }
this.PeopleId = this.auth.loggedIn.User.Id; this.PeopleId = this.auth.loggedIn.User.Id;
this.UserExtra = this.auth.loggedIn.UserExtra; 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<string>(this.auth.getUrl('user/' + this.PeopleId), this.UserExtra).subscribe( this.http.post<string>(this.auth.getUrl('user/' + this.PeopleId), this.UserExtra).subscribe(
resp => { resp => {
if ( resp === expected ) { if ( resp === expected ) {
return this.UserExtra !== undefined && this.UserExtra.Login !== undefined; return this.UserExtra !== undefined && this.UserExtra.Login !== undefined;
} }


public onChangeLogin(): void {
console.log(this);
}

} }

+ 17
- 12
src/app/service/auth.service.ts Zobrazit soubor

import {Router} from '@angular/router'; import {Router} from '@angular/router';
import {PeopleModel} from '../models/people.model'; import {PeopleModel} from '../models/people.model';
import {UserExtraModel} from '../models/user-extra.model'; import {UserExtraModel} from '../models/user-extra.model';
import {Observable} from 'rxjs';


@Injectable() @Injectable()
export class AuthService { export class AuthService {
this.saveSessionInfo(); this.saveSessionInfo();
} }


public isAdmin(): boolean {
return this.loggedIn.role === 'admin';
}
public LoginAvailable(v: string): Observable<boolean> {
return this.http.get<boolean>(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;
}
} }

+ 0
- 1
src/app/top-bar/top-bar.component.ts Zobrazit soubor



ngOnInit(): void { ngOnInit(): void {
this.initAndSubLogin(); this.initAndSubLogin();
console.log(this);
} }


public initAndSubLogin(): void{ public initAndSubLogin(): void{

+ 25
- 0
src/app/validator/unique.login.validator.ts Zobrazit soubor

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<ValidationErrors> | 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})
)
)
);
};
}


Načítá se…
Zrušit
Uložit