Skip to content

Commit

Permalink
Fetch cshtml from app to render correct app-frontend version in preview
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrea Standeren authored and Andrea Standeren committed Jan 30, 2024
1 parent ea75b66 commit c7453ab
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 3 deletions.
28 changes: 28 additions & 0 deletions backend/src/Designer/Controllers/PreviewController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,24 @@ public IActionResult Index(string org, string app)
{
return View();
}

/// <summary>
/// Endpoint to fetch the cshtml to render app-frontend specific to what is defined in the app-repo
/// </summary>
/// <param name="org">Unique identifier of the organisation responsible for the app.</param>
/// <param name="app">Application identifier which is unique within an organisation.</param>
/// <returns>The cshtml modified to ignore this route path added in the iframe.</returns>
[HttpGet]
[Route("/app-specific-preview/{org}/{app:regex(^(?!datamodels$)[[a-z]][[a-z0-9-]]{{1,28}}[[a-z0-9]]$)}")]
public async Task<IActionResult> AppFrontendSpecificPreview(string org, string app)
{
string developer = AuthenticationHelper.GetDeveloperUserName(_httpContextAccessor.HttpContext);
AltinnAppGitRepository altinnAppGitRepository = _altinnGitRepositoryFactory.GetAltinnAppGitRepository(org, app, developer);
var appFrontendCshtml = await altinnAppGitRepository.GetAppFrontendCshtml();
var modifiedContent = ReplaceIndexToFetchCorrectOrgAppInCshtml(appFrontendCshtml);

return Content(modifiedContent, "text/html");
}

/// <summary>
/// Action for getting local app-images
Expand Down Expand Up @@ -877,5 +895,15 @@ private string GetSelectedLayoutSetInEditorFromRefererHeader(string refererHeade

return string.IsNullOrEmpty(layoutSetName) ? null : layoutSetName;
}

private string ReplaceIndexToFetchCorrectOrgAppInCshtml(string originalContent)
{
// Replace the array indexes in the script in the cshtml that retrieves the org and app name since
// /app-specific-preview/ is added when fetching the cshtml file from endpoint instead of designer wwwroot
string modifiedContent = originalContent.Replace("window.org = appId[1];", "window.org = appId[2];");
modifiedContent = modifiedContent.Replace("window.app = appId[2];", "window.app = appId[3];");

return modifiedContent;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ public class AltinnAppGitRepository : AltinnGitRepository
private const string LANGUAGE_RESOURCE_FOLDER_NAME = "texts/";
private const string MARKDOWN_TEXTS_FOLDER_NAME = "md/";
private const string PROCESS_DEFINITION_FOLDER_PATH = "App/config/process/";
private const string CSHTML_PATH = "App/views/Home/index.cshtml";

private const string SERVICE_CONFIG_FILENAME = "config.json";
private const string LAYOUT_SETTINGS_FILENAME = "Settings.json";
Expand Down Expand Up @@ -700,6 +701,23 @@ public async Task<LayoutSets> CreateLayoutSetFile(string layoutSetName)
await WriteTextByRelativePathAsync(pathToLayOutSets, layoutSetsString);
return layoutSets;
}

/// <summary>
/// Gets the cshtml file for the app
/// </summary>
/// <param name="cancellationToken">A <see cref="CancellationToken"/> that observes if operation is cancelled.</param>
/// <returns>The content of Index.cshtml</returns>
public async Task<string> GetAppFrontendCshtml(CancellationToken cancellationToken = default)
{
cancellationToken.ThrowIfCancellationRequested();
if (FileExistsByRelativePath(CSHTML_PATH))
{
string cshtml = await ReadTextByRelativePathAsync(CSHTML_PATH, cancellationToken);
return cshtml;
}

throw new FileNotFoundException("Index.cshtml was not found.");
}

/// <summary>
/// Gets the options list with the provided id.
Expand Down Expand Up @@ -854,8 +872,6 @@ private static string GetPathToRuleConfiguration(string layoutSetName)
Path.Combine(LAYOUTS_FOLDER_NAME, layoutSetName, RULE_CONFIGURATION_FILENAME);
}



/// <summary>
/// String writer that ensures UTF8 is used.
/// </summary>
Expand Down
2 changes: 1 addition & 1 deletion frontend/packages/shared/src/api/paths.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ export const orgsListPath = () => `${basePath}/orgs`; // Get

// Preview
export const instanceIdForPreviewPath = (org, app) => `${basePath}/${org}/${app}/mock-instance-id`; // Get
export const previewPage = (org, app, selectedLayoutSet) => `/designer/html/preview.html?${s({ org, app, selectedLayoutSet })}`;
export const previewPage = (org, app, selectedLayoutSet) => `/app-specific-preview/${org}/${app}?${s({ selectedLayoutSet })}`;

// Preview - SignalR Hub
export const previewSignalRHubSubPath = () => `/previewHub`;
Expand Down

0 comments on commit c7453ab

Please sign in to comment.