Skip to content

Commit

Permalink
fluentcms#2166 Support url slug and add parent page to page response (f…
Browse files Browse the repository at this point in the history
…luentcms#2168)

Co-authored-by: Amir Pournasserian <[email protected]>
  • Loading branch information
TheHadiAhmadi and pournasserian authored Oct 22, 2024
1 parent 6e5f576 commit 64cabfe
Show file tree
Hide file tree
Showing 8 changed files with 332 additions and 240 deletions.
26 changes: 21 additions & 5 deletions src/Backend/FluentCMS.Web.Api/Controllers/PageController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,32 @@ public async Task<IApiResult<PageDetailResponse>> GetById([FromRoute] Guid id, C
[HttpGet]
[DecodeQueryParam]
[Policy(AREA, READ)]
public async Task<IApiResult<PageFullDetailResponse>> GetByUrl([FromQuery] string url, CancellationToken cancellationToken = default)
public async Task<IApiResult<PageParentDetailResponse>> GetByUrl([FromQuery] string url, CancellationToken cancellationToken = default)
{
var uri = new Uri(url);
var domain = uri.Authority;
var path = uri.AbsolutePath;

if (!await setupService.IsInitialized(cancellationToken))
return Ok(GetSetupPage());
return Ok(new PageParentDetailResponse { Current = GetSetupPage() });

return await GetPageResponse(domain, path, cancellationToken);
var response = new PageParentDetailResponse {};

try {
var current = await GetPageResponse(domain, path, cancellationToken);

response.Current = current;
}
catch(Exception ex)
{
var pathParts = path.Split('/');
var parentPath = string.Join('/', pathParts.Take(pathParts.Length - 1));

response.Slug = pathParts.Last();
response.Parent = await GetPageResponse(domain, parentPath, cancellationToken);
}

return Ok(response);
}

[HttpPost]
Expand Down Expand Up @@ -93,7 +109,7 @@ public async Task<IApiResult<bool>> Delete([FromRoute] Guid id, CancellationToke

#region Private Methods

private async Task<IApiResult<PageFullDetailResponse>> GetPageResponse(string domain, string path, CancellationToken cancellationToken = default)
private async Task<PageFullDetailResponse> GetPageResponse(string domain, string path, CancellationToken cancellationToken = default)
{
var site = await siteService.GetByUrl(domain, cancellationToken);
var page = await pageService.GetByFullPath(site.Id, path, cancellationToken);
Expand Down Expand Up @@ -153,7 +169,7 @@ private async Task<IApiResult<PageFullDetailResponse>> GetPageResponse(string do
pageResponse.Sections[plugin.Section].Add(pluginResponse);
}

return Ok(pageResponse);
return pageResponse;
}

private static PageFullDetailResponse GetSetupPage()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace FluentCMS.Web.Api.Models;

public class PageParentDetailResponse : BaseSiteAssociatedResponse
{
public PageFullDetailResponse? Parent { get; set; }
public PageFullDetailResponse? Current { get; set; }
public string Slug { get; set; } = string.Empty;
}
471 changes: 254 additions & 217 deletions src/Frontend/FluentCMS.Web.ApiClients/GeneratedApiClients.cs

Large diffs are not rendered by default.

22 changes: 21 additions & 1 deletion src/Frontend/FluentCMS.Web.ApiClients/config.nswag

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@
Add Page
</button>

<a href="@(NavigationManager.Uri + "?pageEdit=true")" class="f-toolbar-button f-toolbar-button-outline f-toolbar-button-icon">
<button @onclick="@OpenDesignMode" class="f-toolbar-button f-toolbar-button-outline f-toolbar-button-icon">
<svg width="16" height="16" viewBox="0 0 16 16" fill="none" xmlns="http://www.w3.org/2000/svg">
<path d="M15.7001 3.24175L12.7788 0.30041C12.5858 0.108008 12.3245 0 12.0522 0C11.7799 0 11.5187 0.108008 11.3256 0.30041L0.969226 10.6599L0.0236625 14.7479C-0.0089563 14.8973 -0.0078381 15.0522 0.0269353 15.2011C0.0617087 15.3501 0.129259 15.4894 0.224651 15.6088C0.320043 15.7283 0.440868 15.8249 0.578299 15.8915C0.715729 15.9582 0.866293 15.9933 1.01899 15.9942C1.09012 16.0019 1.16186 16.0019 1.23299 15.9942L5.35863 15.047L15.7001 4.69747C15.8922 4.50409 16 4.2424 16 3.96961C16 3.69682 15.8922 3.43513 15.7001 3.24175ZM4.86097 14.1497L0.99411 14.9623L1.87498 11.1634L9.62362 3.4312L12.6096 6.42239L4.86097 14.1497ZM13.2765 5.69952L10.2905 2.70832L12.0224 0.983399L14.9586 3.9746L13.2765 5.69952Z"
fill="currentColor" />
</svg>
</a>
</button>

<button @onclick="OpenPageSettings" class="f-toolbar-button f-toolbar-button-outline f-toolbar-button-icon">
<svg width="16" height="16" viewBox="0 0 18 18" fill="none" xmlns="http://www.w3.org/2000/svg">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,14 @@ public partial class SiteBuilderDefaultToolbar
[Inject]
private ViewState ViewState { get; set; } = default!;

#region Add Page
private async Task OpenDesignMode()
{
var url = ViewState.Page.FullPath.EndsWith("/") ? ViewState.Page.FullPath + "?pageEdit=true" : ViewState.Page.FullPath + "/?pageEdit=true";

NavigationManager.NavigateTo(url, true);
}

#region Add Page
private bool AddPageModalOpen { get; set; } = false;

private async Task OpenAddPage()
Expand Down
32 changes: 18 additions & 14 deletions src/Frontend/FluentCMS.Web.UI/ServiceExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,20 +79,24 @@ private static IServiceCollection AddViewState(this IServiceCollection services)
if (pageResponse?.Data == null)
throw new Exception("Error while loading ViewState");

viewState.Page = mapper.Map<PageViewState>(pageResponse.Data);
viewState.Layout = mapper.Map<LayoutViewState>(pageResponse.Data.Layout);
viewState.DetailLayout = mapper.Map<LayoutViewState>(pageResponse.Data.DetailLayout);
viewState.EditLayout = mapper.Map<LayoutViewState>(pageResponse.Data.EditLayout);
viewState.Site = mapper.Map<SiteViewState>(pageResponse.Data.Site);
viewState.Plugins = pageResponse.Data.Sections!.Values.SelectMany(x => x).Select(p => mapper.Map<PluginViewState>(p)).ToList();
viewState.User = mapper.Map<UserViewState>(pageResponse.Data.User);
viewState.User.Id = pageResponse.Data.User.UserId;

viewState.Site.HasAdminAccess = viewState.User.IsSuperAdmin || (pageResponse.Data.Site.AdminRoleIds ?? []).Any(role => viewState.User?.Roles.Select(x => x.Id).Contains(role) ?? false);
viewState.Site.HasContributorAccess = viewState.Site.HasAdminAccess || (pageResponse.Data.Site.ContributorRoleIds ?? []).Any(role => viewState.User?.Roles.Select(x => x.Id).Contains(role) ?? false);

viewState.Page.HasAdminAccess = viewState.Site.HasContributorAccess || (pageResponse.Data.AdminRoleIds ?? []).Any(role => viewState.User?.Roles.Select(x => x.Id).Contains(role) ?? false);
viewState.Page.HasViewAccess = viewState.Page.HasAdminAccess || (pageResponse.Data.ViewRoleIds ?? []).Any(role => viewState.User?.Roles.Select(x => x.Id).Contains(role) ?? false);
var page = pageResponse.Data.Current;
page ??= pageResponse.Data.Parent;

viewState.Page = mapper.Map<PageViewState>(page);
viewState.Page.Slug = pageResponse.Data.Slug;
viewState.Layout = mapper.Map<LayoutViewState>(page.Layout);
viewState.DetailLayout = mapper.Map<LayoutViewState>(page.DetailLayout);
viewState.EditLayout = mapper.Map<LayoutViewState>(page.EditLayout);
viewState.Site = mapper.Map<SiteViewState>(page.Site);
viewState.Plugins = page.Sections!.Values.SelectMany(x => x).Select(p => mapper.Map<PluginViewState>(p)).ToList();
viewState.User = mapper.Map<UserViewState>(page.User);
viewState.User.Id = page.User.UserId;

viewState.Site.HasAdminAccess = viewState.User.IsSuperAdmin || (page.Site.AdminRoleIds ?? []).Any(role => viewState.User?.Roles.Select(x => x.Id).Contains(role) ?? false);
viewState.Site.HasContributorAccess = viewState.Site.HasAdminAccess || (page.Site.ContributorRoleIds ?? []).Any(role => viewState.User?.Roles.Select(x => x.Id).Contains(role) ?? false);

viewState.Page.HasAdminAccess = viewState.Site.HasContributorAccess || (page.AdminRoleIds ?? []).Any(role => viewState.User?.Roles.Select(x => x.Id).Contains(role) ?? false);
viewState.Page.HasViewAccess = viewState.Page.HasAdminAccess || (page.ViewRoleIds ?? []).Any(role => viewState.User?.Roles.Select(x => x.Id).Contains(role) ?? false);

// check if the page is in edit mode
// it should have pluginId and pluginViewName query strings
Expand Down
1 change: 1 addition & 0 deletions src/Shared/ViewState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ public class PageViewState
public Dictionary<string, string> Settings { get; set; } = [];
public bool HasAdminAccess { get; set; }
public bool HasViewAccess { get; set; }
public string? Slug { get; set; }
}

public class PluginViewState
Expand Down

0 comments on commit 64cabfe

Please sign in to comment.