Files
chapter-organizer/WebApp/Components/Pages/ChapterSettings.razor
T
poprhythmandCursor 4cfd85b902 feat: import leftover student fields into notes and show them on the roster
Store leftover CSV columns on hidden student notes, move catalog import to /events/import, and persist Students index columns from Chapter Settings.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-08-29 23:55:03 -04:00

260 lines
10 KiB
Plaintext

@page "/settings/chapter"
@attribute [Authorize(Roles = AuthRoles.Administrator)]
@implements IAsyncDisposable
@using WebApp.Authentication
@using WebApp.Models
@using WebApp.Components.Shared.Components
@using WebApp.Services
@using Core.Models
@inject IConfiguration Configuration
@inject IChapterSettingsWriter ChapterSettingsWriter
@inject INotesService NotesService
@rendermode InteractiveServer
<PageHeader
Title="Chapter Settings"
Description="Configure chapter information. Student index columns apply the next time you open Students. Printouts that cache chapter name or year may still need a restart." />
<MudContainer MaxWidth="MaxWidth.Large" Class="mt-4">
@if (_settings != null)
{
<MudPaper Class="pa-6 mb-4">
<MudText Typo="Typo.h5" Class="mb-4">Basic Information</MudText>
<MudGrid>
<MudItem xs="12" md="8">
<MudTextField @bind-Value="_settings.Name"
Label="Chapter Name"
Variant="Variant.Outlined"
HelperText="Full chapter name"
Required="true" />
</MudItem>
<MudItem xs="12" md="4">
<MudTextField @bind-Value="_settings.ShortName"
Label="Short Name"
Variant="Variant.Outlined"
HelperText="Abbreviation (e.g., YCN)"
MaxLength="10"
Required="true" />
</MudItem>
<MudItem xs="12" md="6">
<MudTextField @bind-Value="_settings.CompetitionYear"
Label="Competition Year"
Variant="Variant.Outlined"
HelperText="Year of competition (e.g., 2026)"
MaxLength="4"
Required="true" />
</MudItem>
<MudItem xs="12" md="6">
<MudTextField @bind-Value="_settings.YearlyTheme"
Label="Yearly Theme"
Variant="Variant.Outlined"
HelperText="National TSA yearly theme (e.g., Unity Through Community)" />
</MudItem>
<MudItem xs="12" md="6">
<MudSelect T="SchoolLevel?" @bind-Value="_settings.SchoolLevel"
Label="School Level"
Variant="Variant.Outlined"
HelperText="Filter event occurrences by school level (leave empty to import both MS and HS)">
<MudSelectItem T="SchoolLevel?" Value="null">Both (MS and HS)</MudSelectItem>
<MudSelectItem T="SchoolLevel?" Value="@SchoolLevel.MiddleSchool">Middle School (MS)</MudSelectItem>
<MudSelectItem T="SchoolLevel?" Value="@SchoolLevel.HighSchool">High School (HS)</MudSelectItem>
</MudSelect>
</MudItem>
</MudGrid>
</MudPaper>
<MudPaper Class="pa-6 mb-4">
<MudText Typo="Typo.h5" Class="mb-4">Chapter IDs</MudText>
<MudGrid>
<MudItem xs="12" md="4">
<MudTextField @bind-Value="_settings.NationalId"
Label="National ID"
Variant="Variant.Outlined"
HelperText="4-digit national chapter ID"
MaxLength="4" />
</MudItem>
<MudItem xs="12" md="4">
<MudTextField @bind-Value="_settings.StateId"
Label="State ID"
Variant="Variant.Outlined"
HelperText="5-digit state chapter ID"
MaxLength="5" />
</MudItem>
<MudItem xs="12" md="4">
<MudTextField @bind-Value="_settings.RegionalId"
Label="Regional ID"
Variant="Variant.Outlined"
HelperText="5-digit regional chapter ID"
MaxLength="5" />
</MudItem>
</MudGrid>
</MudPaper>
<MudPaper Class="pa-6 mb-4">
<MudText Typo="Typo.h5" Class="mb-4">Student Index Columns</MudText>
<MudText Typo="Typo.body2" Class="mb-3">
Field names from each student's imported notes table to show as extra Students index columns. Click a field found in notes to add or remove it, or type names (one per line).
</MudText>
@if (_availableFields.Count > 0)
{
<MudText Typo="Typo.subtitle2" Class="mb-2">Fields in student notes</MudText>
<MudStack Row="true" Spacing="1" Wrap="Wrap.Wrap" Class="mb-4">
@foreach (var field in _availableFields)
{
var selected = IsFieldSelected(field);
<MudChip T="string"
Size="Size.Small"
Color="@(selected ? Color.Primary : Color.Default)"
Variant="@(selected ? Variant.Filled : Variant.Outlined)"
OnClick="() => ToggleField(field)">
@field
</MudChip>
}
</MudStack>
}
else
{
<MudText Typo="Typo.caption" Class="mud-text-secondary mb-3">
No imported fields found in student notes yet.
</MudText>
}
<MudTextField @bind-Value="_noteFieldsText"
Label="Imported field columns"
Variant="Variant.Outlined"
Lines="5"
HelperText="Example: Interview Time" />
</MudPaper>
<MudPaper Class="pa-6">
<MudGrid>
<MudItem xs="12">
<MudButton Variant="Variant.Filled"
Color="Color.Primary"
StartIcon="@Icons.Material.Filled.Save"
OnClick="SaveSettings"
Disabled="_isSaving">
@if (_isSaving)
{
<MudProgressCircular Class="mr-2" Size="Size.Small" Indeterminate="true" />
<span>Saving...</span>
}
else
{
<span>Save Settings</span>
}
</MudButton>
</MudItem>
</MudGrid>
</MudPaper>
@if (!string.IsNullOrEmpty(_statusMessage))
{
<MudAlert Severity="@_statusSeverity" Class="mt-4">@_statusMessage</MudAlert>
}
}
else
{
<MudProgressCircular Indeterminate="true" />
}
</MudContainer>
@code {
private Models.ChapterSettings? _settings;
private string _noteFieldsText = string.Empty;
private IReadOnlyList<string> _availableFields = [];
private bool _isSaving;
private string? _statusMessage;
private Severity _statusSeverity = Severity.Success;
private CancellationTokenSource? _cancellationTokenSource;
private bool _isDisposed;
protected override void OnInitialized()
{
_cancellationTokenSource = new CancellationTokenSource();
_settings = Models.ChapterSettings.FromConfiguration(Configuration);
_noteFieldsText = string.Join(Environment.NewLine, _settings.StudentIndexNoteFields);
}
protected override async Task OnInitializedAsync()
{
try
{
var cancellationToken = _cancellationTokenSource?.Token ?? CancellationToken.None;
_availableFields = await NotesService.GetImportedFieldNamesAsync(cancellationToken);
}
catch (OperationCanceledException)
{
}
}
private IReadOnlyList<string> SelectedFields() =>
_noteFieldsText
.Split(['\r', '\n'], StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries)
.Where(line => !string.IsNullOrWhiteSpace(line))
.ToList();
private bool IsFieldSelected(string field) =>
SelectedFields().Any(selected => selected.Equals(field, StringComparison.OrdinalIgnoreCase));
private void ToggleField(string field)
{
var selected = SelectedFields().ToList();
var index = selected.FindIndex(name => name.Equals(field, StringComparison.OrdinalIgnoreCase));
if (index >= 0)
selected.RemoveAt(index);
else
selected.Add(field);
_noteFieldsText = string.Join(Environment.NewLine, selected);
}
private async Task SaveSettings()
{
if (_settings == null) return;
_isSaving = true;
_statusMessage = null;
try
{
_settings.StudentIndexNoteFields = [.. SelectedFields()];
var cancellationToken = _cancellationTokenSource?.Token ?? CancellationToken.None;
await ChapterSettingsWriter.WriteAsync(_settings, cancellationToken);
if (_isDisposed)
return;
_statusMessage = "Settings saved. Open Students again to see the updated index columns.";
_statusSeverity = Severity.Success;
}
catch (OperationCanceledException)
{
}
catch (Exception ex)
{
if (_isDisposed)
return;
_statusMessage = $"Error saving settings: {ex.Message}";
_statusSeverity = Severity.Error;
}
finally
{
if (!_isDisposed)
_isSaving = false;
}
}
public async ValueTask DisposeAsync()
{
if (!_isDisposed)
{
_isDisposed = true;
_cancellationTokenSource?.Cancel();
_cancellationTokenSource?.Dispose();
_cancellationTokenSource = null;
}
await ValueTask.CompletedTask;
}
}