diff --git a/Client/Pages/Index.razor.cs b/Client/Pages/Index.razor.cs index 582534b..ceda173 100644 --- a/Client/Pages/Index.razor.cs +++ b/Client/Pages/Index.razor.cs @@ -1,26 +1,34 @@ using Microsoft.AspNetCore.Components; +using Microsoft.JSInterop; using ShiftScheduler.Shared.Models; using System.Net.Http.Json; +using System.Text.Json; namespace ShiftScheduler.Client.Pages { public partial class Index : ComponentBase { [Inject] private HttpClient HttpClient { get; set; } = default!; + [Inject] private IJSRuntime JSRuntime { get; set; } = default!; + [Inject] private NavigationManager NavigationManager { get; set; } = default!; private List DaysInMonth { get; set; } = new(); private List Shifts { get; set; } = new(); private Dictionary SelectedSchedule { get; set; } = new(); + private int EditMonth { get; set; } + private int EditYear { get; set; } protected override async Task OnInitializedAsync() { - var (nextMonth, year) = GetNextMonthAndYear(); + (EditMonth, EditYear) = GetNextMonthAndYear(); - DaysInMonth = Enumerable.Range(1, DateTime.DaysInMonth(year, nextMonth)) - .Select(d => new DateTime(year, nextMonth, d)) + DaysInMonth = Enumerable.Range(1, DateTime.DaysInMonth(EditYear, EditMonth)) + .Select(d => new DateTime(EditYear, EditMonth, d)) .ToList(); Shifts = await HttpClient.GetFromJsonAsync>("api/shift/shifts") ?? new(); + + await LoadScheduleFromStorage(); } private (int nextMonth, int year) GetNextMonthAndYear() @@ -31,9 +39,10 @@ namespace ShiftScheduler.Client.Pages return (nextMonth, year); } - private void SelectShift(DateTime day, string shiftName) + private async Task SelectShift(DateTime day, string shiftName) { SelectedSchedule[day] = shiftName; + await SaveScheduleToStorage(); } private async Task ExportToIcs() @@ -54,6 +63,49 @@ namespace ShiftScheduler.Client.Pages NavigationManager.NavigateTo(fileUrl, true); } - [Inject] private NavigationManager NavigationManager { get; set; } = default!; + private async Task SaveScheduleToStorage() + { + try + { + var storageKey = $"ShiftSchedule_{EditYear}_{EditMonth:D2}"; + + 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); + } + catch (Exception) + { + // ignored + } + } + + private async Task LoadScheduleFromStorage() + { + try + { + var storageKey = $"ShiftSchedule_{EditYear}_{EditMonth:D2}"; + var json = await JSRuntime.InvokeAsync("localStorage.getItem", storageKey); + + if (!string.IsNullOrEmpty(json)) + { + var serializableSchedule = JsonSerializer.Deserialize>(json); + if (serializableSchedule != null) + { + SelectedSchedule = serializableSchedule.ToDictionary( + kvp => DateTime.Parse(kvp.Key), + kvp => kvp.Value + ); + } + } + } + catch (Exception) + { + SelectedSchedule = new Dictionary(); + } + } } } diff --git a/Client/wwwroot/index.html b/Client/wwwroot/index.html index da19b49..9dead99 100644 --- a/Client/wwwroot/index.html +++ b/Client/wwwroot/index.html @@ -27,8 +27,6 @@ 🗙 - -