-
Notifications
You must be signed in to change notification settings - Fork 0
/
register.component.ts
108 lines (90 loc) 路 3.22 KB
/
register.component.ts
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
import { Component, OnInit, ElementRef } from '@angular/core';
import { Router, ActivatedRoute } from '@angular/router';
import { Location, LocationStrategy, PathLocationStrategy } from '@angular/common';
import { FullScreenPage } from '../../models/full-screen-page.model';
import { User, Company } from 'thesis-dais-internship-manager-core';
import { FormGroup, FormControl, Validators } from '@angular/forms';
import { AuthService } from '../../services/auth.service';
import { CompaniesService } from '../../services/companies.service';
import { LoadingService } from '../../helpers/loading.helper';
import swal from 'sweetalert2';
@Component({
moduleId: module.id,
selector: 'register-cmp',
templateUrl: './register.component.html'
})
export class RegisterComponent extends FullScreenPage {
public user: User;
public company: Company;
myform: any;
constructor(
element: ElementRef,
private authService: AuthService,
private companiesService: CompaniesService,
private router: Router,
private loadingService: LoadingService) {
super(element);
this.user = new User({
registrationDate: new Date(),
phone: []
});
this.company = new Company({
registrationDate: new Date()
});
}
ngOnInit() {
this.myform = new FormGroup({
name: new FormControl(this.user.name, [
Validators.required
]),
phone: new FormControl(this.user.phone[0], [
Validators.required
]),
companyName: new FormControl(this.company.name, [
Validators.required
]),
vatCode: new FormControl(this.company.vatCode, [
Validators.required
]),
email: new FormControl(this.user.email, [
Validators.required,
Validators.pattern("[^ @]*@[^ @]*")
]),
passwords: new FormGroup({
password: new FormControl('', [
Validators.required,
Validators.minLength(8)
]),
repeatPassword: new FormControl('')
}, this.matchValidator)
});
}
matchValidator(group: FormGroup) {
const valid = group.controls['password'].value === group.controls['repeatPassword'].value;
if (valid)
return null;
return {
mismatch: true
};
}
async signup() {
try {
this.loadingService.isLoading = true;
const response = await this.authService.register(this.user);
const user = response.data.user;
this.company.owners = [
user.id as any
];
const company = await this.companiesService.create(this.company);
this.router.navigate(['/auth/user/edit'])
} catch (ex) {
console.error("Login error", ex);
swal(
'Registrazione non riuscita',
ex && ex.error && ex.error.exception ? ex.error.exception.message : ex.error,
'error');
} finally {
this.loadingService.isLoading = false;
}
}
}