From b1d55679029fe17181922c1ffb2aefe9fd4d1813 Mon Sep 17 00:00:00 2001 From: Alex Mickelson Date: Fri, 11 Aug 2023 15:19:15 -0600 Subject: [PATCH] got debounced saving working. some of the assignment saving logic is real time --- Management.Web/Program.cs | 3 +- .../AssignmentDescriptionEditor.razor | 39 ++--- .../AssignmentForm/AssignmentForm.razor | 153 +++++++++--------- Management.Web/Shared/Components/Modal.razor | 1 + .../Shared/Course/CourseDetails.razor | 5 +- .../Module/Assignment/AssignmentDetails.razor | 9 +- .../Shared/Semester/AssignmentInDay.razor | 11 +- .../Configuration/AssignmentEditorContext.cs | 52 ++++++ .../Features/Configuration/CoursePlanner.cs | 27 +++- 9 files changed, 170 insertions(+), 130 deletions(-) create mode 100644 Management/Features/Configuration/AssignmentEditorContext.cs diff --git a/Management.Web/Program.cs b/Management.Web/Program.cs index 2f3704f..57c0c3b 100644 --- a/Management.Web/Program.cs +++ b/Management.Web/Program.cs @@ -8,7 +8,7 @@ global using CanvasModel; global using LocalModels; global using Management.Planner; global using Management.Web.Shared.Components; -global using Management.Web.Shared.Course; +global using Management.Web.Shared; using Microsoft.AspNetCore.Components; using Microsoft.AspNetCore.Components.Web; @@ -39,6 +39,7 @@ builder.Services.AddScoped(); builder.Services.AddScoped(); builder.Services.AddScoped(); +builder.Services.AddScoped(); builder.Services.AddScoped(); var app = builder.Build(); diff --git a/Management.Web/Shared/Components/AssignmentForm/AssignmentDescriptionEditor.razor b/Management.Web/Shared/Components/AssignmentForm/AssignmentDescriptionEditor.razor index c7f0545..96f5afa 100644 --- a/Management.Web/Shared/Components/AssignmentForm/AssignmentDescriptionEditor.razor +++ b/Management.Web/Shared/Components/AssignmentForm/AssignmentDescriptionEditor.razor @@ -4,7 +4,7 @@ @code { - protected override void OnInitialized() + @* protected override void OnInitialized() { planner.StateHasChanged += reload; } @@ -15,7 +15,7 @@ public void Dispose() { planner.StateHasChanged -= reload; - } + } *@ [Parameter, EditorRequired] @@ -41,7 +41,6 @@ { UseTemplate = Assignment.TemplateId != null && Assignment.TemplateId != ""; } - Console.WriteLine(Description) } @@ -77,7 +76,7 @@ } : m ).ToArray(); - + planner.LocalCourse = planner.LocalCourse with { Modules=updatedModules @@ -85,41 +84,21 @@ } } - private Timer _debounceTimer; - - private void SaveDescription() - { - - Console.WriteLine("saving description"); - _debounceTimer?.Dispose(); - SaveAssignment(Assignment with { Description = currentDescription }); - } - private string? currentDescription { get; set; } = null; private void OnInputChanged(ChangeEventArgs e) { - // Dispose of any existing timer - _debounceTimer?.Dispose(); - - // Create a new timer that waits for 500ms before executing SaveData - _debounceTimer = new Timer( - (_) => SaveDescription(), - null, - 500, - Timeout.Infinite - ); + SaveAssignment(Assignment with { Description = e.Value?.ToString() ?? "" }); } - }
\ No newline at end of file + + + \ No newline at end of file diff --git a/Management.Web/Shared/Module/Assignment/AssignmentDetails.razor b/Management.Web/Shared/Module/Assignment/AssignmentDetails.razor index f5516f1..524f773 100644 --- a/Management.Web/Shared/Module/Assignment/AssignmentDetails.razor +++ b/Management.Web/Shared/Module/Assignment/AssignmentDetails.razor @@ -3,6 +3,7 @@ @inject DragContainer dragContainer @inject CoursePlanner planner +@inject AssignmentEditorContext assignmentContext @code { [Parameter] @@ -128,7 +129,7 @@ draggable="true" @ondragstart="HandleDragStart" @ondragend="HandleDragEnd" - @onclick="@(() => showUpdateForm = true)" + @onclick="@(() => assignmentContext.Assignment = Assignment)" role="button" >
@@ -249,9 +250,3 @@
- - \ No newline at end of file diff --git a/Management.Web/Shared/Semester/AssignmentInDay.razor b/Management.Web/Shared/Semester/AssignmentInDay.razor index 09b99dd..68e4a96 100644 --- a/Management.Web/Shared/Semester/AssignmentInDay.razor +++ b/Management.Web/Shared/Semester/AssignmentInDay.razor @@ -2,6 +2,7 @@ @inject DragContainer dragContainer @inject CoursePlanner planner +@inject AssignmentEditorContext assignmentContext @code { [Parameter] @@ -14,12 +15,12 @@ private void reload() { this.InvokeAsync(this.StateHasChanged); + @* Console.WriteLine("on initialized " + showUpdateForm + " " + Assignment.Name); *@ } public void Dispose() { planner.StateHasChanged -= reload; } - private bool showUpdateForm = false; private void dropOnDate(DateTime dropDate) { @@ -114,14 +115,8 @@ draggable="true" @ondragstart="HandleDragStart" @ondragend="HandleDragEnd" - @onclick="@(() => showUpdateForm = true)" + @onclick="@(() => assignmentContext.Assignment = Assignment)" role="button" > @Assignment.Name - - \ No newline at end of file diff --git a/Management/Features/Configuration/AssignmentEditorContext.cs b/Management/Features/Configuration/AssignmentEditorContext.cs new file mode 100644 index 0000000..ee46532 --- /dev/null +++ b/Management/Features/Configuration/AssignmentEditorContext.cs @@ -0,0 +1,52 @@ +using LocalModels; +using Management.Planner; + +public class AssignmentEditorContext +{ + public event Action? StateHasChanged; + private CoursePlanner planner { get; } + + public AssignmentEditorContext(CoursePlanner planner) + { + this.planner = planner; + } + + private LocalAssignment? _assignment; + public LocalAssignment? Assignment + { + get { return _assignment; } + set + { + _assignment = value; + StateHasChanged?.Invoke(); + } + } + + public void SaveAssignment(LocalAssignment newAssignment) + { + if (planner.LocalCourse != null) + { + Console.WriteLine(newAssignment.Description); + var currentModule = + planner.LocalCourse.Modules.First( + m => m.Assignments.Select(a => a.Id).Contains(newAssignment.Id) + ) ?? throw new Exception("could not find current module in assignment description form"); + + var updatedModules = planner.LocalCourse.Modules + .Select( + m => + m.Name == currentModule.Name + ? currentModule with + { + Assignments = currentModule.Assignments + .Select(a => a.Id == newAssignment.Id ? newAssignment : a) + .ToArray() + } + : m + ) + .ToArray(); + + planner.LocalCourse = planner.LocalCourse with { Modules = updatedModules }; + } + } +} diff --git a/Management/Features/Configuration/CoursePlanner.cs b/Management/Features/Configuration/CoursePlanner.cs index 8abd54c..d51c817 100644 --- a/Management/Features/Configuration/CoursePlanner.cs +++ b/Management/Features/Configuration/CoursePlanner.cs @@ -21,6 +21,8 @@ public class CoursePlanner this.canvas = canvas; } + private Timer _debounceTimer; + private int _debounceInterval = 1000; private LocalCourse? _localCourse { get; set; } public LocalCourse? LocalCourse { @@ -36,15 +38,30 @@ public class CoursePlanner var verifiedCourse = value.GeneralCourseCleanup(); - // ignore initial load of course - if (_localCourse != null) - { - yamlManager.SaveCourse(verifiedCourse); - } + _debounceTimer?.Dispose(); + _debounceTimer = new Timer( + (_) => saveCourseToFile(), + null, + _debounceInterval, + Timeout.Infinite + ); + _localCourse = verifiedCourse; StateHasChanged?.Invoke(); } } + + private void saveCourseToFile() + { + _debounceTimer?.Dispose(); + // ignore initial load of course + if (LocalCourse != null) + { + Console.WriteLine("Saving file"); + yamlManager.SaveCourse(LocalCourse); + } + } + public event Action? StateHasChanged; public IEnumerable? CanvasAssignments { get; internal set; }