Skip to content

Commit

Permalink
Fix componentId
Browse files Browse the repository at this point in the history
  • Loading branch information
mlqn committed Nov 11, 2024
1 parent de6fd66 commit 4b6ccae
Showing 1 changed file with 21 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -58,17 +58,22 @@ public bool TryChangeComponentId(JsonNode layout, string oldComponentId, string
{
JsonNode originalLayout = layout.DeepClone();

FindIdOccurrencesRecursive(layout, oldComponentId, newComponentId);
FindIdOccurrencesRecursive(null, layout, oldComponentId, newComponentId);

return !layout.ToJsonString().Equals(originalLayout.ToJsonString());
}

private void FindIdOccurrencesRecursive(JsonNode node, string oldComponentId, string newComponentId)
private void FindIdOccurrencesRecursive(string parentKey, JsonNode node, string oldComponentId, string newComponentId)
{
// Should we check if node is string to avoid unnecessary upcoming checks?
if (node is JsonObject jsonObject)
{
if (jsonObject["component"] is not null)
if (parentKey == "tableColumns" && jsonObject[oldComponentId] is not null)
{
// When componentId is the propertyName
UpdateComponentIdActingAsPropertyName(jsonObject, oldComponentId, newComponentId);
}
else if (jsonObject["component"] is not null)
{
// Objects that references components i.e. in `rowsAfter` in RepeatingGroup
UpdateComponentIdActingAsCellMemberInRepeatingGroup(jsonObject, oldComponentId, newComponentId);
Expand All @@ -85,7 +90,7 @@ private void FindIdOccurrencesRecursive(JsonNode node, string oldComponentId, st
}
foreach (var property in jsonObject)
{
FindIdOccurrencesRecursive(property.Value, oldComponentId, newComponentId);
FindIdOccurrencesRecursive(property.Key, property.Value, oldComponentId, newComponentId);
}
}
else if (node is JsonArray jsonArray)
Expand All @@ -99,12 +104,23 @@ private void FindIdOccurrencesRecursive(JsonNode node, string oldComponentId, st
}
else
{
FindIdOccurrencesRecursive(item, oldComponentId, newComponentId);
FindIdOccurrencesRecursive(parentKey, item, oldComponentId, newComponentId);
}
}
}
}

private void UpdateComponentIdActingAsPropertyName(JsonObject jsonObject, string oldComponentId, string newComponentId)
{
// Might need to make this stricter in case app-dev are calling components keys that already are used in the schema
JsonNode value = jsonObject[oldComponentId];
jsonObject.Remove(oldComponentId);
if (!string.IsNullOrEmpty(newComponentId))
{
jsonObject[newComponentId] = value;
}
}

private void UpdateComponentIdActingAsExpressionMember(JsonArray jsonArray, string newComponentId)
{
jsonArray[1] = newComponentId;
Expand Down

0 comments on commit 4b6ccae

Please sign in to comment.