25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

59 lines
1.8 KiB

  1. import { Component, OnInit } from '@angular/core';
  2. import { Router, ActivatedRoute } from '@angular/router';
  3. import { FormBuilder, FormGroup, Validators } from '@angular/forms';
  4. import { first } from 'rxjs/operators';
  5. import { AccountService, AlertService } from '@app/_services';
  6. @Component({ templateUrl: 'register.component.html' })
  7. export class RegisterComponent implements OnInit {
  8. form!: FormGroup;
  9. loading = false;
  10. submitted = false;
  11. constructor(
  12. private formBuilder: FormBuilder,
  13. private route: ActivatedRoute,
  14. private router: Router,
  15. private accountService: AccountService,
  16. private alertService: AlertService
  17. ) { }
  18. ngOnInit() {
  19. this.form = this.formBuilder.group({
  20. firstName: ['', Validators.required],
  21. lastName: ['', Validators.required],
  22. username: ['', Validators.required],
  23. password: ['', [Validators.required, Validators.minLength(6)]]
  24. });
  25. }
  26. // convenience getter for easy access to form fields
  27. get f() { return this.form.controls; }
  28. onSubmit() {
  29. this.submitted = true;
  30. // reset alerts on submit
  31. this.alertService.clear();
  32. // stop here if form is invalid
  33. if (this.form.invalid) {
  34. return;
  35. }
  36. this.loading = true;
  37. this.accountService.register(this.form.value)
  38. .pipe(first())
  39. .subscribe({
  40. next: () => {
  41. this.alertService.success('Registration successful', { keepAfterRouteChange: true });
  42. this.router.navigate(['../login'], { relativeTo: this.route });
  43. },
  44. error: error => {
  45. this.alertService.error(error);
  46. this.loading = false;
  47. }
  48. });
  49. }
  50. }