Ignore:
Timestamp:
01/24/22 21:08:32 (2 years ago)
Author:
Ema <ema_spirova@…>
Branches:
master
Children:
76712b2
Parents:
bdd6491
Message:

signup/login server errors on front and remove location from planner

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trip-planner-front/src/app/homepage/register/register.component.ts

    rbdd6491 rb738035  
    11import { Component, OnInit } from '@angular/core';
    2 import { FormBuilder, FormGroup, Validators } from '@angular/forms';
     2import { FormBuilder, FormControl, FormGroup, Validators } from '@angular/forms';
    33import { DynamicDialogRef } from 'primeng/dynamicdialog';
     4import { CustomValidators } from 'src/app/providers/CustomValidators';
    45import { UserDto } from 'src/app/_models/dto/userDto';
     6import { UserService } from 'src/app/_services/user.service';
    57
    68@Component({
     
    1214
    1315  user: UserDto;
    14   myForm: FormGroup;
     16  form: FormGroup;
     17  usernames: string[];
    1518
    16   constructor(private ref: DynamicDialogRef, private fb: FormBuilder) {
     19
     20  constructor(private ref: DynamicDialogRef,
     21    formBuilder: FormBuilder, private userService: UserService) {
    1722    this.user = new UserDto();
    18     this.myForm = fb.group({
    19       title: fb.control('initial value', Validators.required)
     23    this.usernames = [];
     24
     25    this.form = formBuilder.group({
     26      fullName: new FormControl('', [Validators.required, Validators.minLength(3)]),
     27      username: new FormControl('', [Validators.required, Validators.email]),
     28      password: new FormControl('', [Validators.required, Validators.minLength(6)]),
     29      confirmPassword: new FormControl('', [Validators.required, Validators.minLength(6)])
     30    }, {
     31      validators: [
     32        CustomValidators.mustMatch('password', 'confirmPassword'),
     33        this.validateUsername('username')
     34      ]
    2035    });
    2136  }
    2237
    2338  ngOnInit(): void {
    24     this.myForm = this.fb.group({
    25       fullName: this.fb.control('', Validators.required),
    26       username: this.fb.control('', Validators.required),
    27       password: this.fb.control('', Validators.required),
    28       confirmPassword: this.fb.control('', Validators.required)
    29     });
    3039  }
    3140
    3241  onFormSubmitSignUp(user) {
    33     this.ref.close(user);
    34     //window.location.reload();
     42    const { valid } = this.form;
     43    if (valid) {
     44      this.ref.close(user);
     45    }
     46  }
     47
     48  validateUsername(username: string) {
     49    return (formGroup: FormGroup) => {
     50      const control = formGroup.controls[username];
     51
     52      if (control.errors && !control.errors.validateUsername) {
     53        return;
     54      }
     55
     56      this.userService.getAllUsernames().subscribe(
     57        data => {
     58          this.usernames = data;
     59          for (let i = 0; i < this.usernames.length; i++) {
     60            if (control.value === this.usernames[i]) {
     61              control.setErrors({ validateUsername: true });
     62              break;
     63            } else {
     64              control.setErrors(null);
     65            }
     66          }
     67        }
     68      );
     69      return null;
     70    };
    3571
    3672  }
Note: See TracChangeset for help on using the changeset viewer.