|
| 1 | +import { Component, Inject } from '@angular/core'; |
| 2 | +import { MatDialogRef, MAT_DIALOG_DATA } from '@angular/material/dialog'; |
| 3 | +import { FormBuilder, FormGroup, Validators } from '@angular/forms'; |
| 4 | +import { TeamsService } from '../teams-service'; |
| 5 | +import { Team } from '../team'; |
| 6 | + |
| 7 | +/** |
| 8 | + * Dialog component for creating new teams. |
| 9 | + * Provides a form interface with validation for team name (required) and description (optional). |
| 10 | + */ |
| 11 | +@Component({ |
| 12 | + selector: 'mage-admin-team-create', |
| 13 | + templateUrl: './create-team.component.html', |
| 14 | + styleUrls: ['./create-team.component.scss'] |
| 15 | +}) |
| 16 | +export class CreateTeamDialogComponent { |
| 17 | + teamForm: FormGroup; |
| 18 | + errorMessage: string = ''; |
| 19 | + |
| 20 | + constructor( |
| 21 | + public dialogRef: MatDialogRef<CreateTeamDialogComponent>, |
| 22 | + @Inject(MAT_DIALOG_DATA) public data: { team: Partial<Team> }, |
| 23 | + private fb: FormBuilder, |
| 24 | + private teamsService: TeamsService |
| 25 | + ) { |
| 26 | + this.teamForm = this.fb.group({ |
| 27 | + name: [data.team.name || '', [Validators.required]], |
| 28 | + description: [data.team.description || ''] |
| 29 | + }); |
| 30 | + } |
| 31 | + |
| 32 | + /** |
| 33 | + * Handles form submission for creating a new team. |
| 34 | + * Validates the form, creates the team via the teams service, and closes the dialog on success. |
| 35 | + */ |
| 36 | + save(): void { |
| 37 | + if (this.teamForm.invalid) { |
| 38 | + this.errorMessage = 'Please fill in all required fields.'; |
| 39 | + return; |
| 40 | + } |
| 41 | + |
| 42 | + this.errorMessage = ''; |
| 43 | + const teamData = this.teamForm.value; |
| 44 | + this.teamsService.createTeam(teamData).subscribe({ |
| 45 | + next: (newTeam) => { |
| 46 | + this.dialogRef.close(newTeam); |
| 47 | + }, |
| 48 | + error: () => { |
| 49 | + this.errorMessage = 'Failed to create team. Please try again.'; |
| 50 | + } |
| 51 | + }); |
| 52 | + } |
| 53 | + |
| 54 | + /** |
| 55 | + * Closes the dialog without saving any data or making any changes. |
| 56 | + */ |
| 57 | + cancel(): void { |
| 58 | + this.dialogRef.close(); |
| 59 | + } |
| 60 | +} |
0 commit comments