Files
chapter-organizer/WebApp/Components/Features/Teams/Edit.razor
T
poprhythm 5c1e0b7444 Update padding for MudPaper components across various features
Increased padding from 'pa-4' to 'pa-6' for MudPaper components in Events, MeetingSchedule, Students, and Teams features to ensure consistent styling and improved visual spacing.
2025-12-27 10:43:15 -05:00

141 lines
4.3 KiB
Plaintext

@page "/teams/edit"
@attribute [Authorize]
@using Microsoft.EntityFrameworkCore
@using WebApp.Components.Shared.Components
@inject AppDbContext Context
@inject NavigationManager NavigationManager
@if (Team is null)
{
<p><em>Loading...</em></p>
return;
}
<PageHeader Title="Edit Team"
Subtitle=@Team.ToString()
ShowBackButton="true"
BackButtonUrl="@(ReturnUrl ?? "/teams")" />
<EditForm method="post" Model="Team" OnValidSubmit="UpdateTeam" FormName="edit" Enhance>
<FormChangeTracker @ref="_formChangeTracker" />
<AntiforgeryToken />
<DataAnnotationsValidator/>
<MudGrid>
<MudItem xs="12" sm="7">
<MudPaper Elevation="2" Class="pa-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 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");
}
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();
}
}
// 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()
{
//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();
_formChangeTracker?.AllowNavigation();
NavigationManager.NavigateTo(ReturnUrl ?? "/teams");
}
catch (DbUpdateConcurrencyException)
{
if (!TeamExists(Team!.Id))
{
NavigationManager.NavigateTo("notfound");
}
else
{
throw;
}
}
}
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);
}
}