Skip to content

Commit 76ad5dd

Browse files
Bug/collection flow uploads hidden (#123)
* Set FlowId, InstanceId and FlowPageId for collection flow to fix hidden upload files * Merge in progress data with accumulated data to keep summary for collection flow in sync * Fixed errors not saving on upload file pages
1 parent 23debf7 commit 76ad5dd

1 file changed

Lines changed: 50 additions & 22 deletions

File tree

src/DfE.ExternalApplications.Web/Pages/FormEngine/RenderForm.cshtml.cs

Lines changed: 50 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,11 @@ public async Task OnGetAsync()
156156
{
157157
if (TryParseFlowRoute(CurrentPageId, out var flowId, out var instanceId, out var flowPageId))
158158
{
159+
160+
FlowId = flowId;
161+
InstanceId = instanceId;
162+
FlowPageId = flowPageId;
163+
159164
// Sub-flow: initialize task and resolve page from task's pages
160165
var (group, task) = InitializeCurrentTask(TaskId);
161166
CurrentGroup = group;
@@ -265,6 +270,7 @@ public async Task OnGetAsync()
265270
CheckAndClearSessionForNewApplication();
266271

267272
await LoadAccumulatedDataFromSessionAsync();
273+
MergeFlowProgressIntoFormDataForSummary();
268274

269275
// For upload fields, populate Data from session so they display on GET
270276
// This ensures files appear in the list after upload
@@ -2685,28 +2691,19 @@ public async Task<IActionResult> OnPostUploadFileAsync()
26852691
var file = Request.Form.Files["UploadFile"];
26862692
// Read any existing file IDs posted by the view to preserve list
26872693
var existingFileIds = Request.Form["ExistingFileIds"].ToArray();
2688-
26892694

26902695
if (file == null || file.Length == 0)
26912696
{
26922697

2693-
2694-
ErrorMessage = "Please select a file to upload.";
2698+
ErrorMessage = "Please select a file to upload";
26952699
ModelState.AddModelError("UploadFile", ErrorMessage);
2696-
2697-
26982700

2699-
2700-
2701-
2702-
2703-
2704-
2705-
2706-
2701+
if (!string.IsNullOrEmpty(fieldId))
2702+
{
2703+
_formErrorStore.Save(fieldId, ModelState);
2704+
}
27072705

27082706
Files = await GetFilesForFieldAsync(appId, fieldId);
2709-
27102707

27112708
// Check if we have return URL
27122709
if (!string.IsNullOrEmpty(returnUrl))
@@ -2715,17 +2712,9 @@ public async Task<IActionResult> OnPostUploadFileAsync()
27152712
return Redirect(returnUrl);
27162713
}
27172714

2718-
2719-
2720-
2721-
27222715
return Page();
27232716
}
27242717

2725-
2726-
2727-
2728-
27292718
using var stream = file.OpenReadStream();
27302719
var fileParam = new FileParameter(stream, file.FileName, file.ContentType);
27312720

@@ -3314,6 +3303,45 @@ private async Task PopulateUploadFieldsFromSessionAsync()
33143303
}
33153304
}
33163305

3306+
private void MergeFlowProgressIntoFormDataForSummary()
3307+
{
3308+
if (CurrentTask?.Summary?.Mode?.Equals("multiCollectionFlow", StringComparison.OrdinalIgnoreCase) != true
3309+
|| CurrentTask.Summary?.Flows == null)
3310+
return;
3311+
3312+
foreach (var flow in CurrentTask.Summary.Flows)
3313+
{
3314+
if (!FormData.TryGetValue(flow.FieldId, out var val) || string.IsNullOrWhiteSpace(val?.ToString()))
3315+
continue;
3316+
3317+
var items = JsonSerializer.Deserialize<List<Dictionary<string, object>>>(val.ToString()!) ?? new();
3318+
var changed = false;
3319+
3320+
foreach (var item in items)
3321+
{
3322+
if (!item.TryGetValue("id", out var idObj)) continue;
3323+
var instanceId = idObj?.ToString();
3324+
if (string.IsNullOrWhiteSpace(instanceId)) continue;
3325+
3326+
var progress = LoadFlowProgress(flow.FlowId, instanceId);
3327+
if (!progress.Any()) continue;
3328+
3329+
foreach (var kv in progress)
3330+
{
3331+
item[kv.Key] = kv.Value;
3332+
}
3333+
changed = true;
3334+
}
3335+
3336+
if (changed)
3337+
{
3338+
var updatedJson = JsonSerializer.Serialize(items);
3339+
FormData[flow.FieldId] = updatedJson;
3340+
Data[flow.FieldId] = updatedJson; // keep Data in sync for views
3341+
}
3342+
}
3343+
}
3344+
33173345
#endregion
33183346

33193347
#region Form Error Store Helper Methods

0 commit comments

Comments
 (0)