Skip to content

Commit

Permalink
feat: vertical navigation changes (#161)
Browse files Browse the repository at this point in the history
* feat: made urls use new slug value instead of internal name

* chore: cleanedup lint issues

* chore: linter issue

* fix: updated tests for slug change

* fix: updated tests for slug change

* feat: now uses csPage heading when no page available or when new useParentHero option is selected in contentful

* feat: added unsupported element when item type isn't recognised

* Removed tests for removed endpoints

* Update src/Dfe.ContentSupport.Web/Views/Shared/_VerticalNavigation.cshtml

Co-authored-by: jimwashbrook <[email protected]>

* chore: fixed formatting in test

* chore: removed redundant case

* changes method back to not static and removed Privacy.cshtml

---------

Co-authored-by: Tom Whittington <[email protected]>
Co-authored-by: jimwashbrook <[email protected]>
  • Loading branch information
3 people authored Oct 1, 2024
1 parent c482ff4 commit 4075583
Show file tree
Hide file tree
Showing 21 changed files with 171 additions and 180 deletions.
28 changes: 2 additions & 26 deletions src/Dfe.ContentSupport.Web/Controllers/ContentController.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.Diagnostics;
using Dfe.ContentSupport.Web.Models.Mapped;
using Dfe.ContentSupport.Web.Services;
using Dfe.ContentSupport.Web.ViewModels;
using Microsoft.AspNetCore.Authorization;
Expand All @@ -13,25 +12,7 @@ public class ContentController(IContentService contentService, ILayoutService la
: Controller
{
public const string ErrorActionName = "error";

public async Task<IActionResult> Home()
{
var defaultModel = new CsPage
{
Heading = new Models.Heading
{
Title = "Department for Education",
Subtitle = "Content and Support"
}
};

ViewBag.pages = await contentService.GetCsPages();

return View(defaultModel);
}




[HttpGet("{slug}/{page?}")]
public async Task<IActionResult> Index(string slug, string page = "", bool isPreview = false, [FromQuery] List<string>? tags = null)
{
Expand Down Expand Up @@ -67,12 +48,7 @@ public async Task<IActionResult> Index(string slug, string page = "", bool isPre
return RedirectToAction(ErrorActionName);
}
}


public IActionResult Privacy()
{
return View();
}


[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
public IActionResult Error()
Expand Down
8 changes: 4 additions & 4 deletions src/Dfe.ContentSupport.Web/Models/ContentBase.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
using System.Diagnostics.CodeAnalysis;

namespace Dfe.ContentSupport.Web.Models;

[ExcludeFromCodeCoverage]
public class ContentBase : Contentful.Core.Models.Entry<ContentBase>
{
public string InternalName { get; set; } = null!;

public string? Title { get; set; } = null;

public string? Subtitle { get; set; } = null;
public string Slug { get; set; } = null!;
public string? Title { get; set; }
public string? Subtitle { get; set; }
}
1 change: 1 addition & 0 deletions src/Dfe.ContentSupport.Web/Models/Entry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ public class Entry : ContentBase
{
public string JumpIdentifier { get; set; } = null!;
public ContentItemBase RichText { get; set; } = null!;
public bool UseParentHero { get; set; }
}
6 changes: 1 addition & 5 deletions src/Dfe.ContentSupport.Web/Models/Heading.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,4 @@
namespace Dfe.ContentSupport.Web.Models;

[ExcludeFromCodeCoverage]
public class Heading : ContentBase
{
public string Title { get; init; } = null!;
public string Subtitle { get; init; } = null!;
}
public class Heading : ContentBase;
3 changes: 3 additions & 0 deletions src/Dfe.ContentSupport.Web/Models/Mapped/CsContentItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ namespace Dfe.ContentSupport.Web.Models.Mapped;
public class CsContentItem
{
public string InternalName { get; set; } = null!;
public string Slug { get; set; } = null!;
public string? Title { get; set; } = null;
public string? Subtitle { get; set; } = null;
public bool UseParentHero { get; set; }

}
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,4 @@ public CustomAccordion()
public List<CustomAccordion> Accordions { get; set; } = null!;
public RichTextContentItem? Body { get; set; }
public string SummaryLine { get; set; } = null!;
public string Title { get; set; } = null!;
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Dfe.ContentSupport.Web.Models.Mapped.Types;
using Dfe.ContentSupport.Web.Models.Mapped.Types;
using System.Diagnostics.CodeAnalysis;

namespace Dfe.ContentSupport.Web.Models.Mapped.Custom;
Expand All @@ -13,7 +13,6 @@ public CustomAttachment()

public string ContentType { get; set; } = null!;
public long Size { get; set; }
public string Title { get; set; } = null!;
public string Uri { get; set; } = null!;
public DateTime? UpdatedAt { get; set; } = null!;
public DateTime? UpdatedAt { get; set; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,5 @@ public CustomCard()
public string ImageAlt { get; set; } = null!;
public string ImageUri { get; set; } = null!;
public string Meta { get; set; } = null!;
public string Title { get; set; } = null!;
public string Uri { get; set; } = null!;
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,5 @@ public EmbeddedAsset()
public AssetContentType AssetContentType { get; set; } = AssetContentType.Unknown;

public string Description { get; set; } = null!;
public string Title { get; set; } = null!;
public string Uri { get; set; } = null!;
}
4 changes: 2 additions & 2 deletions src/Dfe.ContentSupport.Web/Models/Target.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ namespace Dfe.ContentSupport.Web.Models;
[ExcludeFromCodeCoverage]
public class Target : Entry
{
public Fields Fields { get; set; } = null!;
public string Title { get; set; } = null!;
public new Fields Fields { get; set; } = null!;
public Asset Asset { get; set; } = null!;
public string SummaryLine { get; set; } = null!;
public string Description { get; set; } = null!;
Expand All @@ -16,4 +15,5 @@ public class Target : Entry
public string Uri { get; set; } = null!;
public Image Image { get; set; } = null!;
public List<Target> Content { get; set; } = [];
public new string Title { get; set; } = null!;
}
12 changes: 5 additions & 7 deletions src/Dfe.ContentSupport.Web/Services/ILayoutService.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
using Dfe.ContentSupport.Web.Models.Mapped;

namespace Dfe.ContentSupport.Web.Services
{
public interface ILayoutService
{
CsPage GenerateLayout(CsPage page, HttpRequest request, string pageName);
namespace Dfe.ContentSupport.Web.Services;

}
}
public interface ILayoutService
{
CsPage GenerateLayout(CsPage page, HttpRequest request, string pageSlug);
}
111 changes: 52 additions & 59 deletions src/Dfe.ContentSupport.Web/Services/LayoutService.cs
Original file line number Diff line number Diff line change
@@ -1,85 +1,78 @@
using Dfe.ContentSupport.Web.Models;
using Dfe.ContentSupport.Web.Models.Mapped;

namespace Dfe.ContentSupport.Web.Services;

namespace Dfe.ContentSupport.Web.Services
public class LayoutService : ILayoutService
{
public class LayoutService : ILayoutService
public CsPage GenerateLayout(CsPage page, HttpRequest request, string pageSlug)
{
public CsPage GenerateLayout(CsPage page, HttpRequest request, string pageName)
{
if (!page.ShowVerticalNavigation) return page;

return new()
{
Heading = GetHeading(page, pageName),
MenuItems = GenerateVerticalNavigation(page, request, pageName),
Content = GetVisiblePageList(page, pageName),
UpdatedAt = page.UpdatedAt,
CreatedAt = page.CreatedAt,
HasCitation = page.HasCitation,
HasBackToTop = page.HasBackToTop,
IsSitemap = page.IsSitemap,
ShowVerticalNavigation = page.ShowVerticalNavigation,
Slug = page.Slug,
};
}

if (!page.ShowVerticalNavigation) return page;

public Heading GetHeading(CsPage page, string pageName)
return new CsPage
{
var selectedPage = page.Content.Find(o => o.InternalName == pageName);
Heading = GetHeading(page, pageSlug),
MenuItems = GenerateVerticalNavigation(page, request, pageSlug),
Content = GetVisiblePageList(page, pageSlug),
UpdatedAt = page.UpdatedAt,
CreatedAt = page.CreatedAt,
HasCitation = page.HasCitation,
HasBackToTop = page.HasBackToTop,
IsSitemap = page.IsSitemap,
ShowVerticalNavigation = page.ShowVerticalNavigation,
Slug = page.Slug
};
}

if (selectedPage != null)
return new()
{
Title = selectedPage.Title ?? "",
Subtitle = selectedPage.Subtitle ?? ""
};

public Heading GetHeading(CsPage page, string pageSlug)
{
var selectedPage = page.Content.Find(o => o.Slug == pageSlug);

return new()
if (selectedPage is { UseParentHero: false })
return new Heading
{
Title = page.Content[0]?.Title ?? "",
Subtitle = page.Content[0]?.Subtitle ?? ""
Title = selectedPage.Title ?? string.Empty,
Subtitle = selectedPage.Subtitle ?? string.Empty
};
}

return page.Heading;
}

public List<PageLink> GenerateVerticalNavigation(CsPage page, HttpRequest request, string pageName)
{
var baseUrl = GetNavigationUrl(request);

var menuItems = page.Content.Select(o => new PageLink()
{
Title = o.Title ?? "",
Subtitle = o.Subtitle ?? "",
Url = $"{baseUrl}/{o.InternalName}",
IsActive = pageName == o.InternalName
}).ToList();
public List<PageLink> GenerateVerticalNavigation(CsPage page, HttpRequest request,
string pageSlug)
{
var baseUrl = GetNavigationUrl(request);

if (string.IsNullOrEmpty(pageName) && menuItems.Count > 0)
menuItems[0].IsActive = true;
var menuItems = page.Content.Select(o => new PageLink
{
Title = o.Title ?? "",
Subtitle = o.Subtitle ?? "",
Url = $"{baseUrl}/{o.Slug}",
IsActive = pageSlug == o.Slug
}).ToList();

return menuItems;
}
if (string.IsNullOrEmpty(pageSlug) && menuItems.Count > 0)
menuItems[0].IsActive = true;

return menuItems;
}

public List<CsContentItem> GetVisiblePageList(CsPage page, string pageName)
{
if (!string.IsNullOrEmpty(pageName))
return page.Content.Where(o => o.InternalName == pageName).ToList();

public List<CsContentItem> GetVisiblePageList(CsPage page, string pageSlug)

Check warning on line 63 in src/Dfe.ContentSupport.Web/Services/LayoutService.cs

View workflow job for this annotation

GitHub Actions / Build and run unit tests

Make 'GetVisiblePageList' a static method. (https://rules.sonarsource.com/csharp/RSPEC-2325)
{
if (!string.IsNullOrEmpty(pageSlug))
return page.Content.Where(o => o.Slug == pageSlug).ToList();

return page.Content.GetRange(0, 1);
}

return page.Content.GetRange(0, 1);
}

public string GetNavigationUrl(HttpRequest request)
{
var splitUrl = request.Path.ToString().Split("/");
return string.Join("/", splitUrl.Take(3));
}

public string GetNavigationUrl(HttpRequest request)

Check warning on line 73 in src/Dfe.ContentSupport.Web/Services/LayoutService.cs

View workflow job for this annotation

GitHub Actions / Build and run unit tests

Make 'GetNavigationUrl' a static method. (https://rules.sonarsource.com/csharp/RSPEC-2325)
{
var splitUrl = request.Path.ToString().Split("/");
return string.Join("/", splitUrl.Take(3));
}
}
}
12 changes: 9 additions & 3 deletions src/Dfe.ContentSupport.Web/Services/ModelMapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,11 @@ public CsContentItem ConvertEntryToContentItem(Entry entry)
{
CsContentItem item = entry.RichText is not null
? MapRichTextContent(entry.RichText, entry)!
: new CsContentItem { InternalName = entry.InternalName, Title = entry.Title, Subtitle = entry.Subtitle };
: new CsContentItem
{
InternalName = entry.InternalName, Slug = entry.Slug, Title = entry.Title,
Subtitle = entry.Subtitle, UseParentHero = entry.UseParentHero
};
return item;
}

Expand All @@ -64,8 +68,10 @@ public CsContentItem ConvertEntryToContentItem(Entry entry)
new RichTextContentItem
{
InternalName = entry.InternalName,
Slug = entry.Slug,
Title = entry.Title,
Subtitle = entry.Subtitle,
UseParentHero = entry.UseParentHero,
NodeType = ConvertToRichTextNodeType(richText.NodeType),
Content = MapRichTextNodes(richText.Content),
Tags = FlattenMetadata(entry.Metadata)
Expand All @@ -76,7 +82,7 @@ public CsContentItem ConvertEntryToContentItem(Entry entry)
public List<RichTextContentItem> MapRichTextNodes(List<ContentItem> nodes)
{
return nodes.Select(node => MapContent(node) ?? new RichTextContentItem
{ NodeType = RichTextNodeType.Unknown, InternalName = node.InternalName }).ToList();
{ NodeType = RichTextNodeType.Unknown, InternalName = node.InternalName }).ToList();
}


Expand All @@ -85,7 +91,7 @@ public List<RichTextContentItem> MapRichTextNodes(List<ContentItem> nodes)
RichTextContentItem? item;
var nodeType = ConvertToRichTextNodeType(contentItem.NodeType);
var internalName = contentItem.InternalName;


switch (nodeType)
{
Expand Down
6 changes: 0 additions & 6 deletions src/Dfe.ContentSupport.Web/ViewModels/ContentSupportPage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,8 @@ namespace Dfe.ContentSupport.Web.ViewModels;
[ExcludeFromCodeCoverage]
public class ContentSupportPage : ContentBase
{
public string Slug { get; init; } = null!;

public List<dynamic> BeforeTitleContent { get; init; } = [];

public Heading Heading { get; init; } = null!;
public List<Entry> Content { get; init; } = [];

public bool DisplayBackButton { get; init; }
public bool IsSitemap { get; init; }
public bool HasCitation { get; init; }
public bool HasBackToTop { get; init; }
Expand Down
Loading

0 comments on commit 4075583

Please sign in to comment.