Skip to content

Commit

Permalink
refactor: change parameter for DeleteLayout to just use layout name
Browse files Browse the repository at this point in the history
Remove file extension formatting on layoutName parameter of DeleteLayout
in AltinnAppGitRepository. This makes the method more consistent with
the other methods and follows layer responsibilities.

commit-id:cf35ff23
  • Loading branch information
Jondyr committed Feb 17, 2025
1 parent 96177ba commit 891dd77
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,10 @@ await _fileSyncHandlerExecutor.ExecuteWithExceptionHandlingAndConditionalNotific
{
bool hasChanges = false;
string[] layoutNames = repository.GetLayoutNames(notification.LayoutSetName);
foreach (var layoutName in layoutNames)
foreach (var layoutFileName in layoutNames)
{

string layoutName = layoutFileName.Replace(".json", "");
var layout = await repository.GetLayout(notification.LayoutSetName, layoutName, cancellationToken);
if (TryChangeComponentId(layout, notification.OldComponentId, notification.NewComponentId))
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,11 @@ public class AltinnAppGitRepository : AltinnGitRepository
private const string SchemaFilePatternJson = "*.schema.json";
private const string SchemaFilePatternXsd = "*.xsd";

public static readonly string InitialLayoutFileName = "Side1.json";
public static readonly string InitialLayoutFileName = "Side1";

public readonly JsonNode InitialLayout = new JsonObject { ["$schema"] = LayoutSchemaUrl, ["data"] = new JsonObject { ["layout"] = new JsonArray([]) } };

public readonly JsonNode InitialLayoutSettings = new JsonObject { ["$schema"] = LayoutSettingsSchemaUrl, ["pages"] = new JsonObject { ["order"] = new JsonArray([InitialLayoutFileName.Replace(".json", "")]) } };
public readonly JsonNode InitialLayoutSettings = new JsonObject { ["$schema"] = LayoutSettingsSchemaUrl, ["pages"] = new JsonObject { ["order"] = new JsonArray([InitialLayoutFileName]) } };

private static readonly JsonSerializerOptions JsonOptions = new()
{
Expand Down Expand Up @@ -370,10 +370,11 @@ public async Task<Dictionary<string, JsonNode>> GetFormLayouts(string layoutSetN
cancellationToken.ThrowIfCancellationRequested();
Dictionary<string, JsonNode> formLayouts = new();
string[] layoutNames = GetLayoutNames(layoutSetName);
foreach (string layoutName in layoutNames)
foreach (string layoutFileName in layoutNames)
{
string layoutName = layoutFileName.Replace(".json", "");
JsonNode layout = await GetLayout(layoutSetName, layoutName, cancellationToken);
formLayouts[layoutName.Replace(".json", "")] = layout;
formLayouts[layoutName] = layout;
}

Check notice

Code scanning / CodeQL

Missed opportunity to use Select Note

This foreach loop immediately
maps its iteration variable to another variable
- consider mapping the sequence explicitly using '.Select(...)'.

return formLayouts;
Expand Down Expand Up @@ -479,7 +480,7 @@ public string[] GetLayoutNames(string layoutSetName)
{
foreach (string layoutPath in GetFilesByRelativeDirectory(layoutSetPath))
{
layoutNames.Add(Path.GetFileName(layoutPath));
layoutNames.Add(Path.GetFileNameWithoutExtension(layoutPath));
}
}

Expand Down Expand Up @@ -652,11 +653,11 @@ public async Task SaveLayout(string layoutSetName, string layoutFileName, JsonNo
await WriteTextByRelativePathAsync(layoutFilePath, serializedLayout, true, cancellationToken);
}

public void UpdateFormLayoutName(string layoutSetName, string layoutFileName, string newFileName)
public void UpdateFormLayoutName(string layoutSetName, string layoutName, string newLayoutName)
{
string currentFilePath = GetPathToLayoutFile(layoutSetName, layoutFileName);
string newFilePath = GetPathToLayoutFile(layoutSetName, newFileName);
MoveFileByRelativePath(currentFilePath, newFilePath, newFileName);
string currentFilePath = GetPathToLayoutFile(layoutSetName, layoutName);
string newFilePath = GetPathToLayoutFile(layoutSetName, newLayoutName);
MoveFileByRelativePath(currentFilePath, newFilePath, newLayoutName);
}

public async Task<LayoutSets> GetLayoutSetsFile(CancellationToken cancellationToken = default)
Expand Down Expand Up @@ -1054,11 +1055,11 @@ private static string GetPathToLayoutSet(string layoutSetName, bool excludeLayou
}

// can be null if app does not use layout set
private static string GetPathToLayoutFile(string layoutSetName, string fileName)
private static string GetPathToLayoutFile(string layoutSetName, string layoutName)
{
return string.IsNullOrEmpty(layoutSetName) ?
Path.Combine(LayoutsFolderName, LayoutsInSetFolderName, fileName) :
Path.Combine(LayoutsFolderName, layoutSetName, LayoutsInSetFolderName, fileName);
Path.Combine(LayoutsFolderName, LayoutsInSetFolderName, $"{layoutName}.json") :
Path.Combine(LayoutsFolderName, layoutSetName, LayoutsInSetFolderName, $"{layoutName}.json");
}

// can be null if app does not use layout set
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@ public async Task SaveFormLayout(AltinnRepoEditingContext altinnRepoEditingConte
string layoutName, JsonNode formLayout, CancellationToken cancellationToken = default)
{
cancellationToken.ThrowIfCancellationRequested();
string layoutFileName = $"{layoutName}.json";
AltinnAppGitRepository altinnAppGitRepository =
_altinnGitRepositoryFactory.GetAltinnAppGitRepository(altinnRepoEditingContext.Org,
altinnRepoEditingContext.Repo, altinnRepoEditingContext.Developer);
Expand All @@ -79,14 +78,13 @@ public async Task SaveFormLayout(AltinnRepoEditingContext altinnRepoEditingConte
"This app uses layout sets, but no layout set name was provided for this request");
}

await altinnAppGitRepository.SaveLayout(layoutSetName, layoutFileName, formLayout, cancellationToken);
await altinnAppGitRepository.SaveLayout(layoutSetName, layoutName, formLayout, cancellationToken);
}

/// <inheritdoc />
public void DeleteFormLayout(AltinnRepoEditingContext altinnRepoEditingContext, string layoutSetName,
string layoutName)
{
string layoutFileName = $"{layoutName}.json";
AltinnAppGitRepository altinnAppGitRepository =
_altinnGitRepositoryFactory.GetAltinnAppGitRepository(altinnRepoEditingContext.Org,
altinnRepoEditingContext.Repo, altinnRepoEditingContext.Developer);
Expand All @@ -97,7 +95,7 @@ public void DeleteFormLayout(AltinnRepoEditingContext altinnRepoEditingContext,
"This app uses layout sets, but no layout set name was provided for this request");
}

altinnAppGitRepository.DeleteLayout(layoutSetName, layoutFileName);
altinnAppGitRepository.DeleteLayout(layoutSetName, layoutName);
}

/// <inheritdoc />
Expand All @@ -114,7 +112,7 @@ public void UpdateFormLayoutName(AltinnRepoEditingContext altinnRepoEditingConte
"This app uses layout sets, but no layout set name was provided for this request");
}

altinnAppGitRepository.UpdateFormLayoutName(layoutSetName, $"{layoutName}.json", $"{newName}.json");
altinnAppGitRepository.UpdateFormLayoutName(layoutSetName, layoutName, newName);
}

/// <inheritdoc />
Expand Down Expand Up @@ -586,7 +584,7 @@ public async Task AddComponentToLayout(
editingContext.Repo,
editingContext.Developer
);
JsonNode formLayout = await altinnAppGitRepository.GetLayout(layoutSetName, $"{layoutName}.json", cancellationToken);
JsonNode formLayout = await altinnAppGitRepository.GetLayout(layoutSetName, layoutName, cancellationToken);
if (formLayout["data"] is not JsonObject data || data["layout"] is not JsonArray layoutArray)
{
throw new InvalidOperationException("Invalid form layout structure");
Expand Down Expand Up @@ -802,7 +800,7 @@ private async Task<bool> UpdateLayoutReferences(AltinnAppGitRepository altinnApp

if (hasLayoutChanges)
{
await altinnAppGitRepository.SaveLayout(layoutSet.Id, $"{layout.Key}.json", layout.Value, cancellationToken);
await altinnAppGitRepository.SaveLayout(layoutSet.Id, layout.Key, layout.Value, cancellationToken);
hasChanges = true;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ private async Task<List<string>> UpdateKeysInLayoutsInLayoutSet(string org, stri
if (hasMutated)
{
await altinnAppGitRepository.SaveLayout(layoutSetName, layoutName, layout);
updatedFiles.Add($"App/ui/{layoutSetName}/{layoutName}");
updatedFiles.Add($"App/ui/{layoutSetName}/{layoutName}.json");
}
}
return updatedFiles.Count > 0 ? ["App/ui/layouts"] : [];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ public async Task GetLayout_WithAppThatUsesLayoutSet_ShouldReturnLayout()
string repository = "app-with-layoutsets";
string developer = "testUser";
string layoutSetName = "layoutSet1";
string layoutName = "layoutFile1InSet1.json";
string layoutName = "layoutFile1InSet1";
AltinnAppGitRepository altinnAppGitRepository = PrepareRepositoryForTest(org, repository, developer);

JsonNode formLayout = await altinnAppGitRepository.GetLayout(layoutSetName, layoutName);
Expand All @@ -216,7 +216,7 @@ public async Task GetLayout_WithAppThatNotUsesLayoutSet_ShouldReturnLayout()
string org = "ttd";
string repository = "app-without-layoutsets";
string developer = "testUser";
string layoutName = "layoutFile1.json";
string layoutName = "layoutFile1";
AltinnAppGitRepository altinnAppGitRepository = PrepareRepositoryForTest(org, repository, developer);

JsonNode formLayout = await altinnAppGitRepository.GetLayout(null, layoutName);
Expand All @@ -233,7 +233,7 @@ public async Task SaveLayout_ShouldUpdateLayoutInApp()
string repository = "app-with-layoutsets";
string developer = "testUser";
string layoutSetName = "layoutSet1";
string layoutName = "layoutFile2InSet1.json";
string layoutName = "layoutFile2InSet1";
string targetRepository = TestDataHelper.GenerateTestRepoName();

try
Expand Down
4 changes: 2 additions & 2 deletions backend/tests/Designer.Tests/Services/TextsServiceTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ public class TextsServiceTest : IDisposable
private const string developer = "testUser";
private const string layoutSetName1 = "layoutSet1";
private const string layoutSetName2 = "layoutSet2";
private const string layoutName1 = "layoutFile1InSet1.json";
private const string layoutName2 = "layoutFile1InSet2.json";
private const string layoutName1 = "layoutFile1InSet1";
private const string layoutName2 = "layoutFile1InSet2";

private async Task<(string targetRepository, AltinnGitRepositoryFactory altinnGitRepositoryFactory, TextsService textsService)>
SetupRepository()
Expand Down
15 changes: 15 additions & 0 deletions frontend/packages/ux-editor/src/hooks/queries/usePagesQuery.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { useQuery } from '@tanstack/react-query';
import { useServicesContext } from 'app-shared/contexts/ServicesContext';
import { QueryKey } from 'app-shared/types/QueryKey';

export const usePagesQuery = ({ org, app, layoutSet }): UseQueryResult<Pages> => {
const { getPages } = useServicesContext();

return useQuery<Pages>({
queryKey: [QueryKey.Pages, org, app, layoutSet],
queryFn: () =>
getPages(org, app, layoutSet).then((pages) => {
return pages;
}),
});
};

0 comments on commit 891dd77

Please sign in to comment.