I was trying to edit a row. After clicking the save button on the modal window that hits BatchUpdateAsync in the CustomAdaptor and saves it to the database. But that only happens for the first time. After that, it never happens when I edit a row until I restart the whole application. I am providing the snippets here:
public class CustomDataAdaptor: DataAdaptor
{
#pragma warning disable CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider declaring as nullable.
[Inject] private ILogger<CustomDataAdaptor> Logger { get; set; }
[Inject] private ProjectState ProjectState { get; set; }
[Inject] private IContentService<Phase> PhaseService { get; set; }
[Inject] private IToastService ToastService { get; set; }
[Inject] private IContentService<Activity> ActivityService { get; set; }
#pragma warning restore CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider declaring as nullable.
private CancellationTokenSource TokenSource { get; set; } = new();
public override async Task<object> UpdateAsync(DataManager dataManager, object data, string keyField, string key)
{
var content = data as TaskData;
if(content is not null)
{
if (content.ParentId is null)
{
try
{
var phase = ProjectState?.Phases?.Find(p => p.Id == content.DataId);
phase = phase ?? throw new Exception("No related phase found");
phase.Name = content.Name;
phase.PhaseDuration = new PhaseDuration
{
Start = content.StartDate,
End = content.EndDate
};
phase.Progress = content.Progress;
phase = await PhaseService.UpdateAsync(phase, TokenSource.Token);
ProjectState?.LoadPhases(new[] { phase }, this);
}
catch (Exception ex)
{
Logger.LogError("{file} exception while updating phase {ex}", this, ex);
ToastService.ShowError("Error while updating phase", "ERROR!!!!");
}
}
else
{
try
{
var activity = ProjectState?.Activities?.Find(p => p.Id == content.DataId);
activity = activity ?? throw new Exception("No related activity found");
activity.Name = content.Name;
activity.StartDate = content.StartDate;
activity.EndDate = content.EndDate;
activity.Progress = content.Progress;
activity = await ActivityService.UpdateAsync(activity, TokenSource.Token);
ProjectState?.LoadActivities(new[] { activity }, this);
}
catch (Exception ex)
{
Logger.LogError("{file} exception while updating activity {ex}", this, ex);
ToastService.ShowError("Error while updating activity", "ERROR!!!!");
}
}
}
return base.UpdateAsync(dataManager, data, keyField, key);
}
public override async Task<object> BatchUpdateAsync(DataManager dataManager, object changedRecords, object addedRecords, object deletedRecords, string keyField, string key, int? dropIndex)
{
List<TaskData> addRecord = addedRecords as List<TaskData> ?? new();
List<TaskData> changed = changedRecords as List<TaskData> ?? new();
List<TaskData> deleteRecord = deletedRecords as List<TaskData> ?? new();
if(changed is not null && changed.Count > 0)
{
Logger.LogDebug("{file} edited records before edit : {changed}", this, JsonSerializer.Serialize(changed));
List<Task<Phase>> phaseTasks = new();
List<Task<Activity>> activityTasks = new();
foreach(var content in changed)
{
if (content is null)
continue;
if (content.ParentId is null)
{
try
{
var phase = ProjectState?.Phases?.Find(p => p.Id == content.DataId);
phase = phase ?? throw new Exception("No related phase found");
phase.Name = content.Name;
phase.PhaseDuration = new PhaseDuration
{
Start = content.StartDate,
End = content.EndDate
};
phase.Progress = content.Progress;
var phaseTask = PhaseService.UpdateAsync(phase, TokenSource.Token);
phaseTasks.Add(phaseTask);
}
catch (Exception ex)
{
Logger.LogError("{file} exception while updating phase {ex}", this, ex);
ToastService.ShowError("Error while updating phase", "ERROR!!!!");
}
}
else
{
try
{
var activity = ProjectState?.Activities?.Find(p => p.Id == content.DataId);
activity = activity ?? throw new Exception("No related activity found");
activity.Name = content.Name;
activity.StartDate = content.StartDate;
activity.EndDate = content.EndDate;
activity.Progress = content.Progress;
var activityTask = ActivityService.UpdateAsync(activity, TokenSource.Token);
activityTasks.Add(activityTask);
}
catch (Exception ex)
{
Logger.LogError("{file} exception while updating activity {ex}", this, ex);
ToastService.ShowError("Error while updating activity", "ERROR!!!!");
}
}
}
var phases = await Task.WhenAll(phaseTasks);
ProjectState.LoadPhases(phases, this);
var activities = await Task.WhenAll(activityTasks);
ProjectState.LoadActivities(activities, this);
Logger.LogDebug("{file} edited phases after edit : {changed}",this, JsonSerializer.Serialize(phases));
Logger.LogDebug("{file} edited activities after edit : {changed}",this, JsonSerializer.Serialize(activities));
}
return base.BatchUpdateAsync(dataManager, changedRecords, addedRecords, deletedRecords, keyField, key, dropIndex);
}
}