2021-07-07 03:31:12 +00:00
|
|
|
import { Component, OnInit, AfterViewInit, ElementRef, ViewChild } from '@angular/core';
|
2021-07-03 21:48:27 +00:00
|
|
|
import { HttpErrorResponse } from '@angular/common/http';
|
|
|
|
import { FormBuilder, Validators } from '@angular/forms';
|
|
|
|
import { TranslateService } from '@ngx-translate/core';
|
2021-07-07 03:31:12 +00:00
|
|
|
import { Router } from '@angular/router';
|
2021-07-03 21:48:27 +00:00
|
|
|
|
|
|
|
import { EMAIL_ALREADY_USED_TYPE, LOGIN_ALREADY_USED_TYPE } from 'app/config/error.constants';
|
|
|
|
import { RegisterService } from './register.service';
|
|
|
|
|
|
|
|
@Component({
|
|
|
|
selector: 'jhi-register',
|
|
|
|
templateUrl: './register.component.html',
|
2021-07-04 07:03:03 +00:00
|
|
|
styleUrls: ['./register.component.scss'],
|
2021-07-03 21:48:27 +00:00
|
|
|
})
|
|
|
|
export class RegisterComponent implements AfterViewInit {
|
2021-07-04 07:03:03 +00:00
|
|
|
// @ViewChild('name', { static: false })
|
|
|
|
// name?: ElementRef;
|
|
|
|
|
|
|
|
profileIcon: number = 1;
|
|
|
|
profileIcons: any[] = [
|
|
|
|
{ name: 'C1', class: 'active' },
|
|
|
|
{ name: 'C2' },
|
|
|
|
{ name: 'C3' },
|
|
|
|
{ name: 'C4' },
|
|
|
|
{ name: 'C5' },
|
|
|
|
{ name: 'C6' },
|
|
|
|
{ name: 'C7' },
|
|
|
|
{ name: 'C8' },
|
|
|
|
{ name: 'C9' },
|
|
|
|
{ name: 'C10' },
|
|
|
|
{ name: 'C11' },
|
|
|
|
{ name: 'C12' },
|
|
|
|
{ name: 'C13' },
|
|
|
|
{ name: 'C14' },
|
|
|
|
{ name: 'C15' },
|
|
|
|
{ name: 'C16' },
|
|
|
|
{ name: 'C17' },
|
|
|
|
{ name: 'C18' },
|
|
|
|
{ name: 'C19' },
|
|
|
|
{ name: 'C20' },
|
|
|
|
{ name: 'C21' },
|
|
|
|
{ name: 'C22' },
|
|
|
|
{ name: 'C23' },
|
|
|
|
{ name: 'C24' },
|
|
|
|
{ name: 'C25' },
|
|
|
|
{ name: 'C26' },
|
|
|
|
{ name: 'C27' },
|
|
|
|
{ name: 'C28' },
|
|
|
|
];
|
2021-07-03 21:48:27 +00:00
|
|
|
|
|
|
|
doNotMatch = false;
|
|
|
|
error = false;
|
|
|
|
errorEmailExists = false;
|
|
|
|
errorUserExists = false;
|
|
|
|
success = false;
|
|
|
|
|
2021-07-04 07:03:03 +00:00
|
|
|
// Login will be used to store the email as well.
|
|
|
|
// login: ['', [Validators.required, Validators.minLength(5), Validators.maxLength(254), Validators.email]]
|
2021-07-03 21:48:27 +00:00
|
|
|
registerForm = this.fb.group({
|
2021-07-04 07:03:03 +00:00
|
|
|
name: ['', [Validators.required, Validators.minLength(2), Validators.maxLength(254)]],
|
2021-07-03 21:48:27 +00:00
|
|
|
email: ['', [Validators.required, Validators.minLength(5), Validators.maxLength(254), Validators.email]],
|
2021-07-12 02:35:10 +00:00
|
|
|
password: ['', [Validators.required, Validators.minLength(8), Validators.maxLength(50)]],
|
|
|
|
confirmPassword: ['', [Validators.required, Validators.minLength(8), Validators.maxLength(50)]],
|
2021-07-03 21:48:27 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
constructor(private translateService: TranslateService, private registerService: RegisterService, private fb: FormBuilder) {}
|
|
|
|
|
|
|
|
ngAfterViewInit(): void {
|
2021-07-04 07:03:03 +00:00
|
|
|
// if (this.name) {
|
|
|
|
// this.name.nativeElement.focus();
|
|
|
|
// }
|
2021-07-03 21:48:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
register(): void {
|
|
|
|
this.doNotMatch = false;
|
|
|
|
this.error = false;
|
|
|
|
this.errorEmailExists = false;
|
|
|
|
this.errorUserExists = false;
|
|
|
|
|
|
|
|
const password = this.registerForm.get(['password'])!.value;
|
|
|
|
if (password !== this.registerForm.get(['confirmPassword'])!.value) {
|
|
|
|
this.doNotMatch = true;
|
|
|
|
} else {
|
2021-07-04 07:03:03 +00:00
|
|
|
const login = this.registerForm.get(['email'])!.value;
|
2021-07-03 21:48:27 +00:00
|
|
|
const email = this.registerForm.get(['email'])!.value;
|
2021-07-04 07:03:03 +00:00
|
|
|
const name = this.registerForm.get(['name'])!.value;
|
|
|
|
|
|
|
|
this.registerService
|
2021-07-07 03:31:12 +00:00
|
|
|
.save({
|
|
|
|
login,
|
|
|
|
email,
|
|
|
|
password,
|
|
|
|
langKey: this.translateService.currentLang,
|
|
|
|
name,
|
|
|
|
profileIcon: this.profileIcon,
|
2021-07-07 04:59:31 +00:00
|
|
|
isAdmin: 0,
|
2021-07-09 23:35:18 +00:00
|
|
|
isGoogle: 0,
|
2021-07-07 03:31:12 +00:00
|
|
|
})
|
2021-07-04 07:03:03 +00:00
|
|
|
.subscribe(
|
|
|
|
() => (this.success = true),
|
|
|
|
response => this.processError(response)
|
|
|
|
);
|
2021-07-03 21:48:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-07 19:31:07 +00:00
|
|
|
processError(response: HttpErrorResponse): void {
|
2021-07-03 21:48:27 +00:00
|
|
|
if (response.status === 400 && response.error.type === LOGIN_ALREADY_USED_TYPE) {
|
|
|
|
this.errorUserExists = true;
|
|
|
|
} else if (response.status === 400 && response.error.type === EMAIL_ALREADY_USED_TYPE) {
|
|
|
|
this.errorEmailExists = true;
|
|
|
|
} else {
|
|
|
|
this.error = true;
|
|
|
|
}
|
|
|
|
}
|
2021-07-04 07:03:03 +00:00
|
|
|
|
|
|
|
selectIcon(event: MouseEvent): void {
|
|
|
|
if (event.target instanceof Element) {
|
|
|
|
document.querySelectorAll('.active').forEach(e => e.classList.remove('active'));
|
|
|
|
event.target.classList.add('active');
|
|
|
|
this.profileIcon = +event.target.getAttribute('id')! + 1;
|
|
|
|
}
|
|
|
|
}
|
2021-07-03 21:48:27 +00:00
|
|
|
}
|