|
1 | | -import {Component, OnInit, ViewChild} from '@angular/core'; |
| 1 | +import {Component, OnInit, ViewChild} from '@angular/core'; |
2 | 2 | import { FormGroup, FormControl, Validators } from '@angular/forms';
|
3 | | -import {Router} from "@angular/router"; |
4 | | -import {ToastComponent} from "../toast/toast.component"; |
5 | | -import {AuthService} from "../../services/auth.service"; |
| 3 | +import {Router} from "@angular/router"; |
| 4 | +import {ToastComponent} from "../toast/toast.component"; |
| 5 | +import {AuthService} from "../../services/auth.service"; |
6 | 6 |
|
7 | 7 | @Component({
|
8 | 8 | selector: 'app-register',
|
9 | 9 | templateUrl: './register.component.html',
|
10 | | - styleUrls: ['./register.component.css','../../app.component.css'] |
| 10 | + styleUrls: ['./register.component.css'] |
11 | 11 | })
|
12 | 12 | export class RegisterComponent implements OnInit {
|
| 13 | + // Form Group for registration |
13 | 14 | registerForm: FormGroup;
|
| 15 | + |
| 16 | + // Reference to the ToastComponent for displaying messages |
14 | 17 | @ViewChild('toast') toastComponent!: ToastComponent;
|
15 | 18 |
|
16 | 19 | constructor(private authService: AuthService, private router: Router) {
|
| 20 | + // Initialize the registration form with form controls |
17 | 21 | this.registerForm = new FormGroup({
|
18 | 22 | firstname: new FormControl('', [Validators.required, Validators.minLength(2)]),
|
19 | 23 | lastname: new FormControl('', [Validators.required, Validators.minLength(2)]),
|
20 | | - company: new FormControl('',), |
| 24 | + company: new FormControl(''), |
21 | 25 | email: new FormControl('', [Validators.required, Validators.email]),
|
22 | 26 | password: new FormControl('', [Validators.required, Validators.minLength(6)])
|
23 | 27 | });
|
24 | 28 | }
|
25 | 29 |
|
| 30 | + // Display toast messages for user feedback |
26 | 31 | showToast(message: string, type: 'success' | 'danger' | 'info' | 'warning') {
|
27 | 32 | this.toastComponent.display(message, type);
|
28 | 33 | }
|
29 | 34 |
|
30 | 35 | ngOnInit(): void {
|
| 36 | + // Initialization logic if needed |
31 | 37 | }
|
32 | 38 |
|
| 39 | + // Handle user registration |
33 | 40 | register() {
|
| 41 | + // Check if the registration form is valid |
34 | 42 | if (this.registerForm.valid) {
|
| 43 | + // Call the AuthService to register the user |
35 | 44 | this.authService.register(this.registerForm.value).subscribe({
|
36 | 45 | next: (response) => {
|
37 | | - |
| 46 | +// Redirect to login page upon successful registration |
38 | 47 | this.router.navigate(['/login']);
|
39 | 48 | },
|
40 | 49 | error: (error) => {
|
41 | | - // Handle error here |
| 50 | + // Handle error here, if needed |
| 51 | + console.error('Registration error:', error); |
42 | 52 | }
|
43 | 53 | });
|
44 | 54 | }
|
45 | 55 | }
|
46 | 56 |
|
| 57 | + // Getter to easily access form controls in the template |
47 | 58 | get f() { return this.registerForm.controls; }
|
48 | 59 | }
|
0 commit comments