diff --git a/src/Dfe.PlanTech.Domain/Content/Interfaces/INavigationLink.cs b/src/Dfe.PlanTech.Domain/Content/Interfaces/INavigationLink.cs index ec8dfe15d..57724d526 100644 --- a/src/Dfe.PlanTech.Domain/Content/Interfaces/INavigationLink.cs +++ b/src/Dfe.PlanTech.Domain/Content/Interfaces/INavigationLink.cs @@ -16,7 +16,7 @@ public interface INavigationLink /// /// Href value (i.e. ) /// - public string Href { get; set; } + public string? Href { get; set; } /// /// Should this link open in a new tab? @@ -27,4 +27,9 @@ public interface INavigationLink /// Does this link contain all necessary information (Href + DisplayText)? /// public bool IsValid => !string.IsNullOrEmpty(DisplayText) && !string.IsNullOrEmpty(Href); + + /// + /// The content to link to. + /// + public IContentComponent? ContentToLinkTo { get; set; } } diff --git a/src/Dfe.PlanTech.Domain/Content/Models/NavigationLink.cs b/src/Dfe.PlanTech.Domain/Content/Models/NavigationLink.cs index fc97a30d9..b32195fae 100644 --- a/src/Dfe.PlanTech.Domain/Content/Models/NavigationLink.cs +++ b/src/Dfe.PlanTech.Domain/Content/Models/NavigationLink.cs @@ -18,10 +18,15 @@ public class NavigationLink : ContentComponent, INavigationLink /// /// Href value (i.e. ) /// - public string Href { get; set; } = null!; + public string? Href { get; set; } = null; /// /// Should this link open in a new tab? /// public bool OpenInNewTab { get; set; } = false; + + /// + /// The content to link to. + /// + public IContentComponent? ContentToLinkTo { get; set; } } diff --git a/src/Dfe.PlanTech.Web/Content/ContentService.cs b/src/Dfe.PlanTech.Web/Content/ContentService.cs index 8d344a634..d11578060 100644 --- a/src/Dfe.PlanTech.Web/Content/ContentService.cs +++ b/src/Dfe.PlanTech.Web/Content/ContentService.cs @@ -47,8 +47,7 @@ public async Task> GetCsPages(bool isPreview = true) return pages.ToList(); } - public async Task> GetContentSupportPages( - string field, string value, bool isPreview) + public async Task> GetContentSupportPages(string field, string value, bool isPreview) { var key = $"{field}_{value}"; if (!isPreview) @@ -58,7 +57,6 @@ public async Task> GetContentSupportPages( return fromCache; } - var result = await contentfulService.GetContentSupportPages(field, value); var pages = modelMapper.MapToCsPages(result); diff --git a/src/Dfe.PlanTech.Web/TagHelpers/FooterLinkTagHelper.cs b/src/Dfe.PlanTech.Web/TagHelpers/FooterLinkTagHelper.cs index 4da79bef8..46e6e44e6 100644 --- a/src/Dfe.PlanTech.Web/TagHelpers/FooterLinkTagHelper.cs +++ b/src/Dfe.PlanTech.Web/TagHelpers/FooterLinkTagHelper.cs @@ -1,6 +1,8 @@ using System.Text; using Dfe.PlanTech.Domain.Content.Interfaces; using Dfe.PlanTech.Domain.Content.Models; +using Dfe.PlanTech.Web.Models.Content; +using Dfe.PlanTech.Web.Models.Content.Mapped; using Microsoft.AspNetCore.Razor.TagHelpers; namespace Dfe.PlanTech.Web.TagHelpers; @@ -9,23 +11,15 @@ namespace Dfe.PlanTech.Web.TagHelpers; /// Renders a single navigation link in the footer. /// /// Should be refactored in future to be any , and pass in HTML class used -public class FooterLinkTagHelper : TagHelper +public class FooterLinkTagHelper(ILogger logger) : TagHelper { - public const string FOOTER_CLASS = "\"govuk-footer__link\""; - private readonly ILogger _logger; - public INavigationLink? Link { get; set; } - public FooterLinkTagHelper(ILogger logger) - { - _logger = logger; - } - public override void Process(TagHelperContext context, TagHelperOutput output) { if (Link == null || !Link.IsValid) { - _logger.LogWarning("Missing {link}", nameof(Link)); + logger.LogWarning("Missing {link}", nameof(Link)); return; } @@ -53,14 +47,50 @@ private static void AppendCloseTag(StringBuilder stringBuilder) private void AppendOpenTag(StringBuilder stringBuilder) { - stringBuilder.Append("'); } + + private string GetHref() + { + if (Link!.ContentToLinkTo == null && string.IsNullOrEmpty(Link.Href)) + { + logger.LogError("No href or content to link to for {LinkType}", nameof(NavigationLink)); + return string.Empty; + } + + return GetUrlForContent() ?? Link.Href ?? string.Empty; + } + + private string? GetUrlForContent() + { + if (Link?.ContentToLinkTo == null) + { + return null; + } + + return Link.ContentToLinkTo switch + { + IPage page => $"/{page.Slug}", + CsPage csPage => $"/content/{csPage.Slug}", + ContentSupportPage contentSupportPage => $"/content/{contentSupportPage.Slug}", + _ => LogInvalidContentTypeAndReturnNull(Link.ContentToLinkTo) + }; + } + + private string? LogInvalidContentTypeAndReturnNull(object content) + { + logger.LogError("Unsupported content type {ContentType} in {TagHelper}", + content.GetType().Name, + nameof(FooterLinkTagHelper)); + return null; + } } diff --git a/src/Dfe.PlanTech.Web/ViewComponents/FooterLinksViewComponent.cs b/src/Dfe.PlanTech.Web/ViewComponents/FooterLinksViewComponent.cs index 8e6372169..1e7290791 100644 --- a/src/Dfe.PlanTech.Web/ViewComponents/FooterLinksViewComponent.cs +++ b/src/Dfe.PlanTech.Web/ViewComponents/FooterLinksViewComponent.cs @@ -7,16 +7,10 @@ namespace Dfe.PlanTech.Web.ViewComponents; /// /// View component that retrieves and displays links in the layout's footer /// -public class FooterLinksViewComponent : ViewComponent +public class FooterLinksViewComponent(IGetNavigationQuery getNavQuery, ILogger logger) : ViewComponent { - private readonly IGetNavigationQuery _getNavQuery; - private readonly ILogger _logger; - - public FooterLinksViewComponent(IGetNavigationQuery getNavQuery, ILogger logger) - { - _getNavQuery = getNavQuery; - _logger = logger; - } + private readonly IGetNavigationQuery _getNavQuery = getNavQuery; + private readonly ILogger _logger = logger; /// /// Retrieve the navigation links using then return the view