-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathuser-form.component.ts
More file actions
138 lines (120 loc) · 4.25 KB
/
user-form.component.ts
File metadata and controls
138 lines (120 loc) · 4.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
import { Component, EventEmitter, Input, OnInit, Output } from '@angular/core';
import { FormBuilder, FormGroup, ValidatorFn, Validators } from '@angular/forms';
import { ActivatedRoute } from '@angular/router';
import { Observable } from 'rxjs';
import { Role } from '../../../services/identity/domain/role.model';
import { User } from '../../../services/identity/domain/user.model';
import { UserWithPassword } from '../../../services/identity/domain/user-with-password.model';
import { FimsValidators } from '../../common/validator/validators';
import { Store } from '@ngrx/store';
import * as fromRoot from '../../../store/index';
import { SEARCH as SEARCH_ROLE } from '../../../store/role/role.actions';
import { map } from 'rxjs/operators';
@Component({
selector: 'ngx-user-form',
templateUrl: './user-form.component.html',
styleUrls: ['./user-form.component.scss'],
})
export class UserFormComponent implements OnInit {
roles: Observable<Role[]>;
detailForm: FormGroup;
@Input() editMode: boolean;
@Input('user') set user(user: User) {
this.prepareForm(user);
}
title: String;
feedbackArr: Array<Object> = [];
@Output() onSave = new EventEmitter<UserWithPassword>();
@Output() onCancel = new EventEmitter<void>();
constructor(private formBuilder: FormBuilder, private store: Store<fromRoot.State>, private route: ActivatedRoute) {}
ngOnInit(): void {
this.route.data.subscribe((data: any) => {
this.title = data.title;
});
this.roles = this.store.select(fromRoot.getRoleSearchResults).pipe(map(rolesPage => rolesPage.roles));
this.fetchRoles();
}
prepareForm(user: User): void {
const passwordValidators: ValidatorFn[] = [Validators.minLength(8), Validators.pattern(/[A-Z]/),
Validators.pattern(/[a-z]/), Validators.pattern(/[0-9]/), Validators.pattern(/[!@#$%*]/)];
if (!this.editMode) {
passwordValidators.push(Validators.required);
}
this.detailForm = this.formBuilder.group({
identifier: [user.identifier, [Validators.required, Validators.minLength(3), Validators.maxLength(32), FimsValidators.urlSafe]],
password: ['', passwordValidators],
role: [user ? user.role : '', Validators.required],
});
}
get role() {
return this.detailForm.get('role');
}
get password() {
return this.detailForm.get('password');
}
formsInvalid(): boolean {
return this.detailForm.invalid;
}
save(): void {
this.onSave.emit(this.detailForm.value);
}
cancel(): void {
this.onCancel.emit();
}
fetchRoles(): void {
this.store.dispatch({ type: SEARCH_ROLE });
}
isLengthMet(password: string): boolean {
if (password.length >= 8) {
this.feedbackArr.push({'label': `Minimum 8 characters`, status: true});
return true;
} else {
this.feedbackArr.push({'label': `Minimum 8 characters`, status: false});
return false;
}
}
isSpecialCharMet(password: string): boolean {
if ( (/[!@#$%*]/).test(password) ) {
this.feedbackArr.push({'label': `One special characters`, status: true});
return true;
} else {
this.feedbackArr.push({'label': `One special characters`, status: false});
return false;
}
}
isNumberMet(password: string): boolean {
if ( (/[0-9]/).test(password) ) {
this.feedbackArr.push({'label': `One number`, status: true});
return true;
} else {
this.feedbackArr.push({'label': `One number`, status: false});
return false;
}
}
isSmallcaseMet(password: string): boolean {
if ( (/[a-z]/).test(password) ) {
this.feedbackArr.push({'label': `One smallcase character`, status: true});
return true;
} else {
this.feedbackArr.push({'label': `One smallcase character`, status: false});
return false;
}
}
isUppercaseMet(password: string): boolean {
if ( (/[A-Z]/).test(password) ) {
this.feedbackArr.push({'label': `One uppercase character`, status: true});
return true;
} else {
this.feedbackArr.push({'label': `One uppercase character`, status: false});
return false;
}
}
onCustomerPasswordStrength(data: any): void {
this.feedbackArr = [];
this.isLengthMet(data);
this.isNumberMet(data);
this.isSmallcaseMet(data);
this.isUppercaseMet(data);
this.isSpecialCharMet(data);
}
}