Implement enhanced shift handling with file system storage and month selection

Co-authored-by: clayschaad <11883505+clayschaad@users.noreply.github.com>
This commit is contained in:
copilot-swe-agent[bot] 2025-08-28 17:14:46 +00:00
parent 16550cfb0b
commit 8102fc9303
10 changed files with 415 additions and 42 deletions

View file

@ -2,10 +2,16 @@
@using ShiftScheduler.Client.Components @using ShiftScheduler.Client.Components
<div class="top-nav-buttons"> <div class="top-nav-buttons">
<div class="month-selection">
<button class="btn month-btn @(_isCurrentMonth ? "" : "selected")" @onclick="SelectNextMonth" disabled="@_isLoadingInitial">Next Month</button>
<button class="btn month-btn @(_isCurrentMonth ? "selected" : "")" @onclick="SelectCurrentMonth" disabled="@_isLoadingInitial">Current Month</button>
</div>
<div class="action-buttons">
<button class="reset-btn" @onclick="ResetSchedule" disabled="@_isLoadingInitial">Reset Schedule</button> <button class="reset-btn" @onclick="ResetSchedule" disabled="@_isLoadingInitial">Reset Schedule</button>
<button class="btn export-btn" @onclick="ExportToIcs" disabled="@_isLoadingInitial">Export to ICS</button> <button class="btn export-btn" @onclick="ExportToIcs" disabled="@_isLoadingInitial">Export to ICS</button>
<button class="btn export-btn" @onclick="ExportToPdf" disabled="@_isLoadingInitial">Export to PDF</button> <button class="btn export-btn" @onclick="ExportToPdf" disabled="@_isLoadingInitial">Export to PDF</button>
<button class="btn config-btn" @onclick="ShowConfiguration" disabled="@_isLoadingInitial">⚙️ Configuration</button> <button class="btn config-btn" @onclick="ShowConfiguration" disabled="@_isLoadingInitial">⚙️ Configuration</button>
</div>
</div> </div>
<h3>Shift Scheduler - @CurrentMonthYear</h3> <h3>Shift Scheduler - @CurrentMonthYear</h3>

View file

@ -18,6 +18,7 @@ namespace ShiftScheduler.Client.Pages
private Dictionary<DateTime, ShiftWithTransport> SelectedShiftsWithTransport { get; set; } = new(); private Dictionary<DateTime, ShiftWithTransport> SelectedShiftsWithTransport { get; set; } = new();
private int EditMonth { get; set; } private int EditMonth { get; set; }
private int EditYear { get; set; } private int EditYear { get; set; }
private bool _isCurrentMonth = false;
private bool _isLoadingTransport = false; private bool _isLoadingTransport = false;
private bool _isLoadingInitial = false; private bool _isLoadingInitial = false;
private string _errorMessage = string.Empty; private string _errorMessage = string.Empty;
@ -31,9 +32,7 @@ namespace ShiftScheduler.Client.Pages
(EditMonth, EditYear) = GetNextMonthAndYear(); (EditMonth, EditYear) = GetNextMonthAndYear();
DaysInMonth = Enumerable.Range(1, DateTime.DaysInMonth(EditYear, EditMonth)) UpdateDaysInMonth();
.Select(d => new DateTime(EditYear, EditMonth, d))
.ToList();
Shifts = await HttpClient.GetFromJsonAsync<List<Shift>>("api/shift/shifts") ?? new(); Shifts = await HttpClient.GetFromJsonAsync<List<Shift>>("api/shift/shifts") ?? new();
@ -43,6 +42,13 @@ namespace ShiftScheduler.Client.Pages
StateHasChanged(); StateHasChanged();
} }
private void UpdateDaysInMonth()
{
DaysInMonth = Enumerable.Range(1, DateTime.DaysInMonth(EditYear, EditMonth))
.Select(d => new DateTime(EditYear, EditMonth, d))
.ToList();
}
private (int nextMonth, int year) GetNextMonthAndYear() private (int nextMonth, int year) GetNextMonthAndYear()
{ {
var today = DateTime.Today; var today = DateTime.Today;
@ -51,6 +57,26 @@ namespace ShiftScheduler.Client.Pages
return (nextMonth, year); return (nextMonth, year);
} }
private async Task SelectCurrentMonth()
{
var today = DateTime.Today;
EditMonth = today.Month;
EditYear = today.Year;
_isCurrentMonth = true;
UpdateDaysInMonth();
await LoadScheduleFromStorage();
StateHasChanged();
}
private async Task SelectNextMonth()
{
(EditMonth, EditYear) = GetNextMonthAndYear();
_isCurrentMonth = false;
UpdateDaysInMonth();
await LoadScheduleFromStorage();
StateHasChanged();
}
private async Task SelectShift(DateTime day, string shiftName) private async Task SelectShift(DateTime day, string shiftName)
{ {
SelectedSchedule[day] = shiftName; SelectedSchedule[day] = shiftName;
@ -167,15 +193,14 @@ namespace ShiftScheduler.Client.Pages
{ {
try try
{ {
var storageKey = $"ShiftSchedule_{EditYear}_{EditMonth:D2}"; var request = new
{
Year = EditYear,
Month = EditMonth,
Schedule = SelectedSchedule
};
var serializableSchedule = SelectedSchedule.ToDictionary( await HttpClient.PostAsJsonAsync("api/shift/save_schedule", request);
kvp => kvp.Key.ToString("yyyy-MM-dd"),
kvp => kvp.Value
);
var json = JsonSerializer.Serialize(serializableSchedule);
await JSRuntime.InvokeVoidAsync("localStorage.setItem", storageKey, json);
} }
catch (Exception) catch (Exception)
{ {
@ -187,22 +212,19 @@ namespace ShiftScheduler.Client.Pages
{ {
try try
{ {
var storageKey = $"ShiftSchedule_{EditYear}_{EditMonth:D2}"; var response = await HttpClient.GetAsync($"api/shift/load_schedule/{EditYear}/{EditMonth}");
var json = await JSRuntime.InvokeAsync<string>("localStorage.getItem", storageKey);
if (!string.IsNullOrEmpty(json)) if (response.IsSuccessStatusCode)
{ {
var serializableSchedule = JsonSerializer.Deserialize<Dictionary<string, string>>(json); SelectedSchedule = await response.Content.ReadFromJsonAsync<Dictionary<DateTime, string>>() ?? new();
if (serializableSchedule != null)
{
SelectedSchedule = serializableSchedule.ToDictionary(
kvp => DateTime.Parse(kvp.Key),
kvp => kvp.Value
);
// Reload transport data for all selected shifts // Reload transport data for all selected shifts
await ReloadTransportDataForSchedule(); await ReloadTransportDataForSchedule();
} }
else
{
SelectedSchedule = new Dictionary<DateTime, string>();
SelectedShiftsWithTransport = new Dictionary<DateTime, ShiftWithTransport>();
} }
} }
catch (Exception) catch (Exception)
@ -259,8 +281,7 @@ namespace ShiftScheduler.Client.Pages
try try
{ {
var storageKey = $"ShiftSchedule_{EditYear}_{EditMonth:D2}"; await HttpClient.DeleteAsync($"api/shift/delete_schedule/{EditYear}/{EditMonth}");
await JSRuntime.InvokeVoidAsync("localStorage.removeItem", storageKey);
} }
catch (Exception) catch (Exception)
{ {

View file

@ -1,15 +1,15 @@
<Project Sdk="Microsoft.NET.Sdk.BlazorWebAssembly"> <Project Sdk="Microsoft.NET.Sdk.BlazorWebAssembly">
<PropertyGroup> <PropertyGroup>
<TargetFramework>net8.0</TargetFramework> <TargetFramework>net9.0</TargetFramework>
<Nullable>enable</Nullable> <Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings> <ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly" Version="8.0.8" /> <PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly" Version="9.0.8" />
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.Authentication" Version="8.0.8" /> <PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.Authentication" Version="9.0.8" />
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.DevServer" Version="8.0.8" PrivateAssets="all" /> <PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.DevServer" Version="9.0.8" PrivateAssets="all" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>

View file

@ -99,3 +99,226 @@ a, .btn-link {
.loading-progress-text:after { .loading-progress-text:after {
content: var(--blazor-load-percentage-text, "Loading"); content: var(--blazor-load-percentage-text, "Loading");
} }
/* Shift Scheduler Specific Styles */
.top-nav-buttons {
display: flex;
justify-content: space-between;
align-items: center;
margin-bottom: 1rem;
flex-wrap: wrap;
gap: 1rem;
}
.month-selection {
display: flex;
gap: 0.5rem;
}
.action-buttons {
display: flex;
gap: 0.5rem;
flex-wrap: wrap;
}
.month-btn {
padding: 0.5rem 1rem;
border: 2px solid #1b6ec2;
background-color: #fff;
color: #1b6ec2;
border-radius: 4px;
cursor: pointer;
transition: all 0.2s ease;
}
.month-btn:hover {
background-color: #f8f9fa;
}
.month-btn.selected {
background-color: #1b6ec2;
color: #fff;
}
.month-btn:disabled {
opacity: 0.6;
cursor: not-allowed;
}
.calendar-grid {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
gap: 1rem;
margin-top: 1rem;
}
.day-cell {
border: 1px solid #dee2e6;
border-radius: 8px;
padding: 1rem;
background-color: #fff;
box-shadow: 0 2px 4px rgba(0,0,0,0.1);
}
.day-header {
font-weight: bold;
margin-bottom: 0.5rem;
padding-bottom: 0.5rem;
border-bottom: 1px solid #dee2e6;
}
.shifts {
display: flex;
gap: 0.5rem;
margin-bottom: 0.5rem;
flex-wrap: wrap;
}
.shift-button {
padding: 0.25rem 0.5rem;
border: 2px solid transparent;
background-color: #f8f9fa;
border-radius: 4px;
cursor: pointer;
transition: all 0.2s ease;
font-size: 1.2rem;
}
.shift-button:hover {
background-color: #e9ecef;
border-color: #dee2e6;
}
.shift-button.selected {
background-color: #1b6ec2;
color: #fff;
border-color: #1861ac;
}
.shift-button:disabled {
opacity: 0.6;
cursor: not-allowed;
}
.tooltip-wrapper {
position: relative;
display: inline-block;
}
.tooltip {
visibility: hidden;
position: absolute;
bottom: 125%;
left: 50%;
transform: translateX(-50%);
background-color: #333;
color: #fff;
padding: 0.25rem 0.5rem;
border-radius: 4px;
font-size: 0.8rem;
white-space: nowrap;
z-index: 1;
opacity: 0;
transition: opacity 0.2s ease;
}
.tooltip-wrapper:hover .tooltip {
visibility: visible;
opacity: 1;
}
.shift-times, .transport-info {
font-size: 0.8rem;
color: #6c757d;
margin-top: 0.25rem;
}
.loading-overlay {
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
background-color: rgba(255, 255, 255, 0.8);
display: flex;
justify-content: center;
align-items: center;
z-index: 1000;
}
.loading-content {
text-align: center;
}
.loading-spinner {
font-size: 2rem;
animation: spin 1s linear infinite;
}
@keyframes spin {
from { transform: rotate(0deg); }
to { transform: rotate(360deg); }
}
.alert {
padding: 0.75rem 1.25rem;
margin-bottom: 1rem;
border: 1px solid transparent;
border-radius: 0.25rem;
position: relative;
}
.alert-danger {
color: #721c24;
background-color: #f8d7da;
border-color: #f5c6cb;
}
.alert-success {
color: #155724;
background-color: #d4edda;
border-color: #c3e6cb;
}
.btn-close {
position: absolute;
top: 0.5rem;
right: 0.5rem;
background: none;
border: none;
font-size: 1.25rem;
cursor: pointer;
opacity: 0.5;
}
.btn-close:hover {
opacity: 1;
}
.reset-btn {
background-color: #dc3545;
color: #fff;
border: 1px solid #dc3545;
padding: 0.375rem 0.75rem;
border-radius: 0.25rem;
cursor: pointer;
}
.reset-btn:hover {
background-color: #c82333;
border-color: #bd2130;
}
.export-btn, .config-btn {
background-color: #28a745;
color: #fff;
border: 1px solid #28a745;
padding: 0.375rem 0.75rem;
border-radius: 0.25rem;
cursor: pointer;
}
.export-btn:hover, .config-btn:hover {
background-color: #218838;
border-color: #1e7e34;
}

View file

@ -143,6 +143,48 @@ namespace ShiftScheduler.Server.Controllers
var pdf = _pdfExportService.GenerateMonthlySchedulePdf(shiftsWithTransport); var pdf = _pdfExportService.GenerateMonthlySchedulePdf(shiftsWithTransport);
return File(pdf, "application/pdf", "schedule.pdf"); return File(pdf, "application/pdf", "schedule.pdf");
} }
[HttpPost("save_schedule")]
public async Task<IActionResult> SaveSchedule([FromBody] SaveScheduleRequest request)
{
try
{
await _configurationService.SaveScheduleAsync(request.Year, request.Month, request.Schedule);
return Ok();
}
catch (Exception ex)
{
return StatusCode(500, $"Error saving schedule: {ex.Message}");
}
}
[HttpGet("load_schedule/{year}/{month}")]
public async Task<IActionResult> LoadSchedule(int year, int month)
{
try
{
var schedule = await _configurationService.LoadScheduleAsync(year, month);
return Ok(schedule);
}
catch (Exception ex)
{
return StatusCode(500, $"Error loading schedule: {ex.Message}");
}
}
[HttpDelete("delete_schedule/{year}/{month}")]
public async Task<IActionResult> DeleteSchedule(int year, int month)
{
try
{
await _configurationService.DeleteScheduleAsync(year, month);
return Ok();
}
catch (Exception ex)
{
return StatusCode(500, $"Error deleting schedule: {ex.Message}");
}
}
} }
public class ShiftTransportRequest public class ShiftTransportRequest
@ -150,4 +192,11 @@ namespace ShiftScheduler.Server.Controllers
public string ShiftName { get; set; } = string.Empty; public string ShiftName { get; set; } = string.Empty;
public DateTime Date { get; set; } public DateTime Date { get; set; }
} }
public class SaveScheduleRequest
{
public int Year { get; set; }
public int Month { get; set; }
public Dictionary<DateTime, string> Schedule { get; set; } = new();
}
} }

View file

@ -1,14 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk.Web"> <Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup> <PropertyGroup>
<TargetFramework>net8.0</TargetFramework> <TargetFramework>net9.0</TargetFramework>
<Nullable>enable</Nullable> <Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings> <ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Authentication.Google" Version="8.0.8" /> <PackageReference Include="Microsoft.AspNetCore.Authentication.Google" Version="9.0.8" />
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.Server" Version="8.0.8" /> <PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.Server" Version="9.0.8" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>

View file

@ -11,6 +11,11 @@ namespace ShiftScheduler.Services
Task ImportConfigurationAsync(string jsonContent); Task ImportConfigurationAsync(string jsonContent);
List<Shift> GetShifts(); List<Shift> GetShifts();
TransportConfiguration GetTransportConfiguration(); TransportConfiguration GetTransportConfiguration();
// Schedule persistence methods
Task SaveScheduleAsync(int year, int month, Dictionary<DateTime, string> schedule);
Task<Dictionary<DateTime, string>> LoadScheduleAsync(int year, int month);
Task DeleteScheduleAsync(int year, int month);
} }
public class ConfigurationService : IConfigurationService public class ConfigurationService : IConfigurationService
@ -128,6 +133,75 @@ namespace ShiftScheduler.Services
return GetConfiguration().Transport; return GetConfiguration().Transport;
} }
// Schedule persistence methods
public async Task SaveScheduleAsync(int year, int month, Dictionary<DateTime, string> schedule)
{
try
{
var scheduleFileName = $"schedule-{year:D4}-{month:D2}.json";
var scheduleFilePath = Path.Combine(_configDirectory, scheduleFileName);
var serializableSchedule = schedule.ToDictionary(
kvp => kvp.Key.ToString("yyyy-MM-dd"),
kvp => kvp.Value
);
var jsonOptions = new JsonSerializerOptions { WriteIndented = true };
var json = JsonSerializer.Serialize(serializableSchedule, jsonOptions);
await File.WriteAllTextAsync(scheduleFilePath, json);
}
catch
{
// Log error if needed, but don't throw to avoid breaking the application
}
}
public async Task<Dictionary<DateTime, string>> LoadScheduleAsync(int year, int month)
{
try
{
var scheduleFileName = $"schedule-{year:D4}-{month:D2}.json";
var scheduleFilePath = Path.Combine(_configDirectory, scheduleFileName);
if (!File.Exists(scheduleFilePath))
return new Dictionary<DateTime, string>();
var json = await File.ReadAllTextAsync(scheduleFilePath);
var serializableSchedule = JsonSerializer.Deserialize<Dictionary<string, string>>(json);
if (serializableSchedule == null)
return new Dictionary<DateTime, string>();
return serializableSchedule.ToDictionary(
kvp => DateTime.Parse(kvp.Key),
kvp => kvp.Value
);
}
catch
{
return new Dictionary<DateTime, string>();
}
}
public async Task DeleteScheduleAsync(int year, int month)
{
try
{
var scheduleFileName = $"schedule-{year:D4}-{month:D2}.json";
var scheduleFilePath = Path.Combine(_configDirectory, scheduleFileName);
if (File.Exists(scheduleFilePath))
{
File.Delete(scheduleFilePath);
}
await Task.CompletedTask;
}
catch
{
// Log error if needed, but don't throw
}
}
private ApplicationConfiguration LoadConfigurationFromFiles(ApplicationConfiguration fallbackConfiguration) private ApplicationConfiguration LoadConfigurationFromFiles(ApplicationConfiguration fallbackConfiguration)
{ {
try try

View file

@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk"> <Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup> <PropertyGroup>
<TargetFramework>net8.0</TargetFramework> <TargetFramework>net9.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings> <ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable> <Nullable>enable</Nullable>
</PropertyGroup> </PropertyGroup>
@ -13,7 +13,7 @@
<ItemGroup> <ItemGroup>
<PackageReference Include="Ical.Net" Version="5.1.0" /> <PackageReference Include="Ical.Net" Version="5.1.0" />
<PackageReference Include="QuestPDF" Version="2025.7.0" /> <PackageReference Include="QuestPDF" Version="2025.7.0" />
<PackageReference Include="Microsoft.Extensions.Caching.Memory" Version="8.0.0" /> <PackageReference Include="Microsoft.Extensions.Caching.Memory" Version="9.0.0" />
</ItemGroup> </ItemGroup>
</Project> </Project>

View file

@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk"> <Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup> <PropertyGroup>
<TargetFramework>net8.0</TargetFramework> <TargetFramework>net9.0</TargetFramework>
<Nullable>enable</Nullable> <Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings> <ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup> </PropertyGroup>

View file

@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk"> <Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup> <PropertyGroup>
<TargetFramework>net8.0</TargetFramework> <TargetFramework>net9.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings> <ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable> <Nullable>enable</Nullable>
<IsPackable>false</IsPackable> <IsPackable>false</IsPackable>