got debounced saving working. some of the assignment saving logic is real time

This commit is contained in:
2023-08-11 15:19:15 -06:00
parent ea087faa1a
commit b1d5567902
9 changed files with 170 additions and 130 deletions

View File

@@ -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<CanvasService, CanvasService>();
builder.Services.AddScoped<YamlManager>();
builder.Services.AddScoped<CoursePlanner>();
builder.Services.AddScoped<AssignmentEditorContext>();
builder.Services.AddScoped<DragContainer>();
var app = builder.Build();

View File

@@ -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)
}
@@ -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() ?? "" });
}
}
<div class="form-check form-switch">
<input
class="form-check-input"
type="checkbox"
role="switch"
id="useTemplateForDescription"
@bind="UseTemplate"
class="form-check-input"
type="checkbox"
role="switch"
id="useTemplateForDescription"
@bind="UseTemplate"
/>
<label class="form-check-label" for="useTemplateForDescription">
use template for description

View File

@@ -2,32 +2,30 @@
@inject CoursePlanner planner
@inject CanvasService canvas
@inject AssignmentEditorContext assignmentContext
@code {
protected override void OnInitialized()
{
planner.StateHasChanged += reload;
assignmentContext.StateHasChanged += reload;
}
private void reload()
{
if (assignmentContext.Assignment != null)
{
AssignmentModal?.Show();
name = assignmentContext.Assignment.Name;
lockAtDueDate = assignmentContext.Assignment.LockAtDueDate;
rubric = assignmentContext.Assignment.Rubric;
submissionTypes = assignmentContext.Assignment.SubmissionTypes;
}
this.InvokeAsync(this.StateHasChanged);
}
public void Dispose()
{
planner.StateHasChanged -= reload;
assignmentContext.StateHasChanged -= reload;
}
[Parameter]
[EditorRequired]
public LocalAssignment Assignment
{
get;
set;
} = default!;
[Parameter]
[EditorRequired]
public bool Show { get; set; }
[Parameter]
public Action OnHide { get; set; } = () => { };
@@ -37,26 +35,16 @@
private IEnumerable<RubricItem> rubric { get; set; } = Enumerable.Empty<RubricItem>();
private IEnumerable<string> submissionTypes { get; set; } = Enumerable.Empty<string>();
protected override void OnParametersSet()
{
if (Show)
{
AssignmentModal?.Show();
}
name = Assignment.Name;
lockAtDueDate = Assignment.LockAtDueDate;
rubric = Assignment.Rubric;
submissionTypes = Assignment.SubmissionTypes;
}
private void submitHandler()
{
var totalRubricPoints = rubric
.Where(r => !r.Label.Contains(RubricItem.extraCredit))
.Select(s => s.Points)
.Sum();
if(assignmentContext.Assignment != null)
{
var totalRubricPoints = rubric
.Where(r => !r.Label.Contains(RubricItem.extraCredit))
.Select(s => s.Points)
.Sum();
var newAssignment = Assignment with
var newAssignment = assignmentContext.Assignment with
{
Name = name,
LockAtDueDate = lockAtDueDate,
@@ -65,34 +53,36 @@
SubmissionTypes = submissionTypes,
};
if (planner.LocalCourse != null)
{
var currentModule = planner
.LocalCourse
.Modules
.First(m =>
m.Assignments
.Select(a => a.Id)
.Contains(Assignment.Id)
) ?? throw new Exception("could not find current module in assignment 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
if (planner.LocalCourse != null)
{
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 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
};
}
AssignmentModal?.Hide();
assignmentContext.Assignment = null;
}
AssignmentModal?.Hide();
}
private void updateRubric(IEnumerable<RubricItem> newRubric)
@@ -108,16 +98,17 @@
private async Task HandleDelete()
{
if (planner.LocalCourse != null)
if (planner.LocalCourse != null && assignmentContext.Assignment != null)
{
var assignment = Assignment;
var assignment = assignmentContext.Assignment;
var currentModule = planner
.LocalCourse
.Modules
.First(m =>
m.Assignments
.Select(a => a.Id)
.Contains(Assignment.Id)
.Contains(assignment.Id)
) ?? throw new Exception("handling assignment delete, could not find module");
var newModules = planner.LocalCourse.Modules.Select(m =>
m.Name == currentModule.Name
@@ -143,34 +134,40 @@
<Modal @ref="AssignmentModal" OnHide="@(() => OnHide())">
<Title>
@Assignment.Name
@assignmentContext.Assignment?.Name
</Title>
<Body>
<form @onsubmit:preventDefault="true" @onsubmit="submitHandler">
<div class="m-1">
<label class="form-label">
Name
</label>
<input class="form-control" @bind="name" />
</div>
<div class="m-1">
<AssignmentDescriptionEditor @key="Assignment" Assignment="Assignment" />
</div>
@if(assignmentContext.Assignment != null)
{
<form @onsubmit:preventDefault="true">
<div class="m-1">
<label class="form-label">
Name
</label>
<input class="form-control" @bind="name" />
</div>
<div class="m-1">
<AssignmentDescriptionEditor Assignment="assignmentContext.Assignment" />
</div>
<div class="form-check m-1">
<input class="form-check-input" id="lockAtDueDate" type="checkbox" />
<label class="form-check-label" for="lockAtDueDate" @bind="lockAtDueDate">
Lock At Due Date
</label>
</div>
<RubricEditor Rubric="rubric" SetRubric="updateRubric" />
<SubmissionTypeSelector Types="submissionTypes" SetTypes="SetTypes" />
</form>
<div class="form-check m-1">
<input class="form-check-input" id="lockAtDueDate" type="checkbox" />
<label class="form-check-label" for="lockAtDueDate" @bind="lockAtDueDate">
Lock At Due Date
</label>
</div>
<RubricEditor Rubric="rubric" SetRubric="updateRubric" />
<SubmissionTypeSelector Types="submissionTypes" SetTypes="SetTypes" />
</form>
}
</Body>
<Footer>
<ConfirmationModal Label="Delete" Class="btn btn-danger" OnConfirmAsync="HandleDelete" />
<button class="btn btn-primary" @onclick="@(() => AssignmentModal?.Hide())">
<button class="btn btn-primary" @onclick="@(() => {
AssignmentModal?.Hide();
assignmentContext.Assignment = null;
})">
Save
</button>
</Footer>

View File

@@ -18,6 +18,7 @@
private bool showBackdrop = false;
public void Show()
{
modalClass = "show-modal";
showBackdrop = true;
OnShow();

View File

@@ -2,6 +2,7 @@
@using CanvasModel.EnrollmentTerms
@using Management.Web.Shared.Module
@using Management.Web.Shared.Semester
@using Management.Web.Shared.Components.AssignmentForm
@inject CanvasService canvas
@inject CoursePlanner planner
@@ -56,3 +57,5 @@
<Modules />
</div>
</div>
<AssignmentForm />

View File

@@ -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"
>
<div class="card">
@@ -249,9 +250,3 @@
</div>
</div>
</div>
<AssignmentForm
Assignment="Assignment"
Show="showUpdateForm"
OnHide="@(() => showUpdateForm = false)"
/>

View File

@@ -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
</div>
<AssignmentForm
Assignment="Assignment"
Show="showUpdateForm"
OnHide="@(() => showUpdateForm = false)"
/>

View File

@@ -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 };
}
}
}

View File

@@ -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<CanvasAssignment>? CanvasAssignments { get; internal set; }