Skip to content

Commit

Permalink
fix remove hidden row
Browse files Browse the repository at this point in the history
  • Loading branch information
bjosttveit committed Sep 11, 2023
1 parent a3de74c commit 8affc03
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 12 deletions.
25 changes: 17 additions & 8 deletions src/Altinn.App.Core/Helpers/DataModel/DataModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -213,19 +213,14 @@ public void RemoveField(string key)
var keys = keys_split[0..^1];
var (lastKey, lastGroupIndex) = ParseKeyPart(keys_split[^1]);

if (lastGroupIndex is not null)
{
// TODO: Consider implementing. Would be required for rowHidden on groups
throw new NotImplementedException($"Deleting elements in List is not implemented {key}");
}

var containingObject = GetModelDataRecursive(keys, 0, _serviceModel, default);
if (containingObject is null)
{
// Already empty field
return;
}


if (containingObject is System.Collections.IEnumerable)
{
throw new NotImplementedException($"Tried to remove field {key}, ended in an enumerable");
Expand All @@ -238,9 +233,23 @@ public void RemoveField(string key)
return;
}

var nullValue = property.PropertyType.GetTypeInfo().IsValueType ? Activator.CreateInstance(property.PropertyType) : null;
if (lastGroupIndex is not null)
{
// Remove row from list
var propertyValue = property.GetValue(containingObject);
if (propertyValue is not System.Collections.IList listValue)
{
throw new ArgumentException($"Tried to remove row {key}, ended in a non-list ({propertyValue?.GetType().ToString()})");

Check notice

Code scanning / CodeQL

Redundant ToString() call Note

Redundant call to 'ToString' on a String object.
}

property.SetValue(containingObject, nullValue);
listValue.RemoveAt(lastGroupIndex.Value);
}
else
{
// Set property to null
var nullValue = property.PropertyType.GetTypeInfo().IsValueType ? Activator.CreateInstance(property.PropertyType) : null;
property.SetValue(containingObject, nullValue);
}
}

/// <inheritdoc />
Expand Down
27 changes: 23 additions & 4 deletions src/Altinn.App.Core/Internal/Expressions/LayoutEvaluator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ private static void HiddenFieldsForRemovalRecurs(LayoutEvaluatorState state, Has

// Hidden row for repeating group
var hiddenRow = new Dictionary<int, bool>();
if (context.Component is RepeatingGroupComponent && context.RowLength is not null)
if (context.Component is RepeatingGroupComponent repGroup && context.RowLength is not null && repGroup.HiddenRow is not null)
{
foreach (var index in Enumerable.Range(0, context.RowLength.Value))
{
Expand All @@ -42,14 +42,33 @@ private static void HiddenFieldsForRemovalRecurs(LayoutEvaluatorState state, Has
var rowContext = new ComponentContext(context.Component, rowIndices, null, childContexts);
var rowHidden = ExpressionEvaluator.EvaluateBooleanExpression(state, rowContext, "hiddenRow", false);
hiddenRow.Add(index, rowHidden);

var indexedBinding = state.AddInidicies(repGroup.DataModelBindings["group"], rowContext);
if (rowHidden)
{
hiddenModelBindings.Add(indexedBinding);
}
else
{
nonHiddenModelBindings.Add(indexedBinding);
}
}
}

foreach (var childContext in context.ChildContexts)
{
var currentRow = childContext.RowIndices?.Last();
var rowIsHidden = currentRow is not null && hiddenRow.GetValueOrDefault(currentRow.Value) == true;
HiddenFieldsForRemovalRecurs(state, hiddenModelBindings, nonHiddenModelBindings, childContext, hidden || rowIsHidden);
// Check if row is already hidden
if (context.Component is RepeatingGroupComponent)
{
var currentRow = childContext.RowIndices?.Last();
var rowIsHidden = currentRow is not null && hiddenRow.GetValueOrDefault(currentRow.Value) == true;

Check notice

Code scanning / CodeQL

Unnecessarily complex Boolean expression Note

The expression 'A == true' can be simplified to 'A'.
if (rowIsHidden)
{
continue;
}
}

HiddenFieldsForRemovalRecurs(state, hiddenModelBindings, nonHiddenModelBindings, childContext, hidden);
}

foreach (var (bindingName, binding) in context.Component.DataModelBindings)
Expand Down

0 comments on commit 8affc03

Please sign in to comment.