87db67f979
Updated the MudPaper component styling in multiple files to use a consistent padding class of "pa-3 pa-md-6" instead of "pa-6". This change enhances the visual consistency of the UI across the Calendar, Events, Students, and Teams components, improving the overall user experience.
187 lines
6.0 KiB
Plaintext
187 lines
6.0 KiB
Plaintext
@page "/teams/edit"
|
|
@attribute [Authorize]
|
|
@using Microsoft.EntityFrameworkCore
|
|
@using WebApp.Components.Shared.Components
|
|
@using MudBlazor
|
|
@using WebApp.Services
|
|
@inject AppDbContext Context
|
|
@inject NavigationManager NavigationManager
|
|
@inject ISnackbar Snackbar
|
|
@inject FormValidationService ValidationService
|
|
|
|
@if (Team is null || _editContext is null)
|
|
{
|
|
<p><em>Loading...</em></p>
|
|
return;
|
|
}
|
|
|
|
<PageHeader Title="Edit Team"
|
|
Subtitle=@Team.ToString()
|
|
ShowBackButton="true"
|
|
BackButtonUrl="@(ReturnUrl ?? "/teams")" />
|
|
|
|
<EditForm method="post" EditContext="@_editContext" OnInvalidSubmit="OnInvalidSubmit" FormName="edit" Enhance>
|
|
<FormChangeTracker @ref="_formChangeTracker" />
|
|
<AntiforgeryToken />
|
|
<DataAnnotationsValidator/>
|
|
|
|
<ValidationErrorDisplay Errors="_validationErrors" />
|
|
|
|
<MudGrid>
|
|
<MudItem xs="12" sm="7">
|
|
<MudPaper Elevation="2" Class="pa-3 pa-md-6">
|
|
<StudentToggleSelector Students="@_students"
|
|
@bind-SelectedStudents="_selectedStudents"
|
|
Title="Students"
|
|
ShowFullName="true" />
|
|
<MudStack Class="mt-4">
|
|
<TeamCaptainSelector Students="@_selectedStudents"
|
|
@bind-SelectedCaptain="Team.Captain"
|
|
Title="Captain" />
|
|
</MudStack>
|
|
</MudPaper>
|
|
</MudItem>
|
|
</MudGrid>
|
|
</EditForm>
|
|
|
|
<FormActions>
|
|
<MudButton OnClick="UpdateTeam" Variant="Variant.Filled" Color="Color.Primary">Save</MudButton>
|
|
<MudButton OnClick="HandleCancel" Variant="Variant.Text">Cancel</MudButton>
|
|
</FormActions>
|
|
|
|
@code {
|
|
[SupplyParameterFromQuery]
|
|
private int Id { get; set; }
|
|
|
|
[SupplyParameterFromQuery]
|
|
private string? ReturnUrl { get; set; }
|
|
|
|
[SupplyParameterFromForm]
|
|
private Team? Team { get; set; }
|
|
|
|
private FormChangeTracker? _formChangeTracker;
|
|
private EditContext? _editContext;
|
|
private List<string> _validationErrors = new();
|
|
private IEnumerable<Student>? _selectedStudents = [];
|
|
private List<Student> _students = [];
|
|
|
|
protected override async Task OnInitializedAsync()
|
|
{
|
|
Team ??= await Context.Teams
|
|
.Include(e => e.Event)
|
|
.Include(e => e.Students)
|
|
.FirstOrDefaultAsync(m => m.Id == Id);
|
|
_students = await Context.Students.ToListAsync();
|
|
_selectedStudents = Team?.Students.ToList();
|
|
|
|
if (Team is null)
|
|
{
|
|
NavigationManager.NavigateTo("notfound");
|
|
}
|
|
else
|
|
{
|
|
_editContext = new EditContext(Team);
|
|
|
|
switch (Team.Event.EventFormat)
|
|
{
|
|
case EventFormat.Individual when Team.Students.Count == 1:
|
|
Team.Captain ??= Team.Students[0];
|
|
Team.Identifier ??= Team.Captain.FirstName;
|
|
break;
|
|
case EventFormat.Team:
|
|
break;
|
|
default:
|
|
throw new ArgumentOutOfRangeException();
|
|
}
|
|
}
|
|
}
|
|
|
|
private void OnInvalidSubmit(EditContext editContext)
|
|
{
|
|
_validationErrors = ValidationService.HandleInvalidSubmit(editContext, Team!, errors => _validationErrors = errors);
|
|
StateHasChanged();
|
|
}
|
|
|
|
// To protect from overposting attacks, enable the specific properties you want to bind to.
|
|
// For more information, see https://learn.microsoft.com/aspnet/core/blazor/forms/#mitigate-overposting-attacks.
|
|
private async Task UpdateTeam()
|
|
{
|
|
// Validate before processing
|
|
if (_editContext != null)
|
|
{
|
|
var errors = ValidationService.CollectValidationErrors(_editContext, Team!);
|
|
if (errors.Any())
|
|
{
|
|
_validationErrors = ValidationService.HandleInvalidSubmit(_editContext, Team!, err => _validationErrors = err);
|
|
StateHasChanged();
|
|
return;
|
|
}
|
|
}
|
|
|
|
_validationErrors.Clear();
|
|
|
|
//Context.Attach(Team!).Entity = EntityState.Modified;
|
|
Team?.Students.Clear();
|
|
if (_selectedStudents != null)
|
|
foreach (var s in _selectedStudents)
|
|
{
|
|
Team?.Students.Add(s);
|
|
}
|
|
|
|
// Update identifier for individual events
|
|
if (Team is { Event.EventFormat: EventFormat.Individual, Students.Count: 1 })
|
|
{
|
|
Team.Captain ??= Team.Students[0];
|
|
Team.Identifier = Team.Captain.FirstName;
|
|
}
|
|
|
|
try
|
|
{
|
|
await Context.SaveChangesAsync();
|
|
Snackbar.Add($"Team '{Team}' saved successfully.", Severity.Success);
|
|
_formChangeTracker?.AllowNavigation();
|
|
NavigationManager.NavigateTo(ReturnUrl ?? "/teams");
|
|
}
|
|
catch (DbUpdateConcurrencyException ex)
|
|
{
|
|
if (!ValidationService.HandleDbUpdateConcurrencyException(
|
|
ex,
|
|
() => TeamExists(Team!.Id),
|
|
"team",
|
|
() => NavigationManager.NavigateTo("notfound")))
|
|
{
|
|
throw;
|
|
}
|
|
}
|
|
catch (DbUpdateException ex)
|
|
{
|
|
ValidationService.HandleDbUpdateException(
|
|
ex,
|
|
"An error occurred while saving the team.",
|
|
"team",
|
|
"saving");
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
ValidationService.HandleException(ex);
|
|
}
|
|
}
|
|
|
|
private async Task HandleCancel()
|
|
{
|
|
// Discard all in-memory changes by reloading from database
|
|
if (Team != null)
|
|
{
|
|
await Context.Entry(Team).ReloadAsync();
|
|
}
|
|
|
|
_formChangeTracker?.AllowNavigation();
|
|
NavigationManager.NavigateTo(ReturnUrl ?? "/teams");
|
|
}
|
|
|
|
private bool TeamExists(int id)
|
|
{
|
|
return Context.Teams.Any(e => e.Id == id);
|
|
}
|
|
}
|