console.log(array, array.length)
import { Component, OnInit } from '@angular/core';
import { FormControl, FormGroup, Validators } from '@angular/forms'
import { User } from '../../shared/models/user.model';
import { Notification } from '../../shared/models/notification.model';
import { AuthService } from '../../shared/services/auth.service';
import { UsersService } from '../../shared/services/users.service';
@Component({
selector: 'gl-signup',
templateUrl: './signup.component.html',
})
export class SignupComponent implements OnInit {
form: FormGroup;
notification: Notification;
constructor(
private usersService: UsersService,
private authService: AuthService
) {}
ngOnInit() {
this.notification = new Notification('disable', '');
this.form = new FormGroup({
'nickname': new FormControl(null, [Validators.required]),
'email': new FormControl(null, [Validators.required, Validators.email]),
'password': new FormControl(null, [Validators.required, Validators.minLength(6)]),
'passwordConfirm': new FormControl(null, [Validators.required])
})
}
private showNotification(type: string, messages: string) {
this.notification = new Notification (type, messages);
}
onSubmit() {
const formData = this.form.value,
{nickname, email, password} = this.form.value,
user = new User(groupId, nickname, email, emailComfirmed, password);
var groupId = 1,
emailComfirmed: boolean = false,
emailValid = false,
formNicknameCheck = /^[a-zA-Z1-9]{1,20}$/,
formEmailCheck = /^((([0-9A-Za-z]{1}[-0-9A-z\.]{1,}[0-9A-Za-z]{1})|([0-9А-Яа-я]{1}[-0-9А-я\.]{1,}[0-9А-Яа-я]{1}))@([-0-9A-Za-z]{1,}\.){1,2}[-A-Za-z]{2,})$/u;
let notificationMessages: any = [];
this.usersService.getUserByNickname(formData.nickname).subscribe((user: User) => {
var notificationType = 'error';
if(user) {
notificationMessages.push("Никнейм уже занят")
} else {
if(formData.nickname == null) {
notificationMessages.push("Похоже, вы не указали никнейм")
} else {
if(formData.nickname.length == 0) {
notificationMessages.push("Похоже, вы не указали никнейм")
} else {
if(formData.nickname.length < 3) {
notificationMessages.push("Никнейм не может быть меньше 3 символов")
} else {
if(!formNicknameCheck.exec(this.form.value.nickname)) {
notificationMessages.push("Разрешени следующие символы: a-z, A-Z, 0-9")
} else {
if(formData.nickname.length >= 20) {
notificationMessages.push("Никнейм не может быть больше 20 символов")
} else {
notificationType = 'disable'
}
}
}
}
}
}
this.showNotification(notificationType, notificationMessages)
})
this.usersService.getUserByEmail("?email=" + formData.email).subscribe((user: User) => {
var notificationType = 'error';
if(user) {
notificationMessages.push("Почта уже используется")
} else {
if(formData.email == null) {
notificationMessages.push("Похоже, вы не указали почту")
} else {
if(formData.email.length == 0) {
notificationMessages.push("Похоже, вы не указали почту")
} else {
if(formData.email.length < 3) {
notificationMessages.push("Почта не может быть меньше 3 символов")
} else {
if(!formEmailCheck.exec(this.form.value.email)) {
notificationMessages.push("Похоже, вы ошиблись при вводе почты")
} else {
if(formData.email.length >= 50) {
notificationMessages.push("Никнейм не может быть больше 50 символов")
} else {
notificationType = 'disable'
}
}
}
}
}
}
this.showNotification(notificationType, notificationMessages)
})
// this.usersService.createNewUser(user)
// .subscribe((user: User) => {
// console.log(user);
// this.authService.signin();
// window.localStorage.setItem('user', JSON.stringify(user));
// });
}
}