Enhance shift handling: Move storage to file system and add month selection (#36)

* Initial plan

* Initial setup: Downgrade .NET version to 8.0 for compatibility

Co-authored-by: clayschaad <11883505+clayschaad@users.noreply.github.com>

* Implement enhanced shift handling with file system storage and month selection

Co-authored-by: clayschaad <11883505+clayschaad@users.noreply.github.com>

* Move component styles from global app.css to Index.razor.css for proper component isolation

Co-authored-by: clayschaad <11883505+clayschaad@users.noreply.github.com>

---------

Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: clayschaad <11883505+clayschaad@users.noreply.github.com>
This commit is contained in:
Copilot 2025-08-28 19:47:12 +02:00 committed by GitHub
parent 497379f62b
commit 7f8b2c4b8c
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 252 additions and 57 deletions

View file

@ -2,10 +2,16 @@
@using ShiftScheduler.Client.Components
<div class="top-nav-buttons">
<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="ExportToPdf" disabled="@_isLoadingInitial">Export to PDF</button>
<button class="btn config-btn" @onclick="ShowConfiguration" disabled="@_isLoadingInitial">⚙️ Configuration</button>
<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="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 config-btn" @onclick="ShowConfiguration" disabled="@_isLoadingInitial">⚙️ Configuration</button>
</div>
</div>
<h3>Shift Scheduler - @CurrentMonthYear</h3>

View file

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

View file

@ -1,13 +1,49 @@
/* Top navigation buttons */
.top-nav-buttons {
position: fixed;
top: 60px;
right: 24px;
display: flex;
gap: 10px;
z-index: 1000;
padding: 14px 0;
background-color: transparent;
justify-content: space-between;
align-items: center;
margin-bottom: 1rem;
flex-wrap: wrap;
gap: 1rem;
}
/* Month selection buttons */
.month-selection {
display: flex;
gap: 0.5rem;
}
/* Action buttons container */
.action-buttons {
display: flex;
gap: 0.5rem;
flex-wrap: wrap;
}
/* Month navigation buttons */
.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;
}
/* Loading overlay */
@ -41,47 +77,56 @@
/* Calendar container */
.calendar-grid {
display: grid;
grid-template-columns: repeat(7, 1fr);
gap: 10px;
margin: 20px 0;
grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
gap: 1rem;
margin-top: 1rem;
}
/* Each day cell */
.day-cell {
border: 1px solid #ccc;
border: 1px solid #dee2e6;
border-radius: 8px;
padding: 10px;
text-align: center;
background-color: #f9f9f9;
padding: 1rem;
background-color: #fff;
box-shadow: 0 2px 4px rgba(0,0,0,0.1);
}
/* Day number */
.day-cell > div:first-child {
.day-header {
font-weight: bold;
margin-bottom: 5px;
margin-bottom: 0.5rem;
color: #333;
font-size: 0.9rem;
}
/* Shift buttons container */
.shifts {
display: flex;
justify-content: center;
gap: 5px;
flex-wrap: wrap;
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 0.5rem;
margin-bottom: 0.5rem;
}
/* Individual shift buttons */
.shift-button {
border: 1px solid #888;
border-radius: 5px;
padding: 5px 8px;
background-color: white;
border: 2px solid #ddd;
border-radius: 6px;
padding: 0.5rem;
background-color: #fff;
cursor: pointer;
font-size: 18px;
font-size: 1.2rem;
transition: all 0.2s ease;
text-align: center;
min-height: 2.5rem;
display: flex;
align-items: center;
justify-content: center;
}
.shift-button:hover {
background-color: #e0e0e0;
background-color: #f8f9fa;
border-color: #007bff;
transform: translateY(-1px);
}
/* Selected shift */

View file

@ -143,6 +143,48 @@ namespace ShiftScheduler.Server.Controllers
var pdf = _pdfExportService.GenerateMonthlySchedulePdf(shiftsWithTransport);
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
@ -150,4 +192,11 @@ namespace ShiftScheduler.Server.Controllers
public string ShiftName { get; set; } = string.Empty;
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

@ -11,6 +11,11 @@ namespace ShiftScheduler.Services
Task ImportConfigurationAsync(string jsonContent);
List<Shift> GetShifts();
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
@ -128,6 +133,75 @@ namespace ShiftScheduler.Services
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)
{
try