Skip to content

Commit

Permalink
feat: add support for navigation link references to content
Browse files Browse the repository at this point in the history
  • Loading branch information
jimwashbrook committed Nov 21, 2024
1 parent 810ac05 commit 32adb19
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public interface INavigationLink
/// <summary>
/// Href value (i.e. <a href="{Href}"></a>)
/// </summary>
public string Href { get; set; }
public string? Href { get; set; }

/// <summary>
/// Should this link open in a new tab?
Expand All @@ -27,4 +27,9 @@ public interface INavigationLink
/// Does this link contain all necessary information (Href + DisplayText)?
/// </summary>
public bool IsValid => !string.IsNullOrEmpty(DisplayText) && !string.IsNullOrEmpty(Href);

/// <summary>
/// The content to link to.
/// </summary>
public IContentComponent? ContentToLinkTo { get; set; }
}
7 changes: 6 additions & 1 deletion src/Dfe.PlanTech.Domain/Content/Models/NavigationLink.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,15 @@ public class NavigationLink : ContentComponent, INavigationLink
/// <summary>
/// Href value (i.e. <a href="{Href}"></a>)
/// </summary>
public string Href { get; set; } = null!;
public string? Href { get; set; } = null;

/// <summary>
/// Should this link open in a new tab?
/// </summary>
public bool OpenInNewTab { get; set; } = false;

/// <summary>
/// The content to link to.
/// </summary>
public IContentComponent? ContentToLinkTo { get; set; }
}
4 changes: 1 addition & 3 deletions src/Dfe.PlanTech.Web/Content/ContentService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,7 @@ public async Task<List<CsPage>> GetCsPages(bool isPreview = true)
return pages.ToList();
}

public async Task<List<CsPage>> GetContentSupportPages(
string field, string value, bool isPreview)
public async Task<List<CsPage>> GetContentSupportPages(string field, string value, bool isPreview)
{
var key = $"{field}_{value}";
if (!isPreview)
Expand All @@ -58,7 +57,6 @@ public async Task<List<CsPage>> GetContentSupportPages(
return fromCache;
}


var result = await contentfulService.GetContentSupportPages(field, value);
var pages = modelMapper.MapToCsPages(result);

Expand Down
56 changes: 43 additions & 13 deletions src/Dfe.PlanTech.Web/TagHelpers/FooterLinkTagHelper.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -9,23 +11,15 @@ namespace Dfe.PlanTech.Web.TagHelpers;
/// Renders a single navigation link in the footer.
/// </summary>
/// <remarks>Should be refactored in future to be any <see cref="NavigationLink"/>, and pass in HTML class used</remarks>
public class FooterLinkTagHelper : TagHelper
public class FooterLinkTagHelper(ILogger<FooterLinkTagHelper> logger) : TagHelper
{
public const string FOOTER_CLASS = "\"govuk-footer__link\"";
private readonly ILogger<FooterLinkTagHelper> _logger;

public INavigationLink? Link { get; set; }

public FooterLinkTagHelper(ILogger<FooterLinkTagHelper> 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;
}

Expand Down Expand Up @@ -53,14 +47,50 @@ private static void AppendCloseTag(StringBuilder stringBuilder)

private void AppendOpenTag(StringBuilder stringBuilder)
{
stringBuilder.Append("<a class=").Append(FOOTER_CLASS).Append(" href=\"");
stringBuilder.Append(Link!.Href);
stringBuilder.Append("""<a class="govuk-footer__link" """).Append(" href=\"");
stringBuilder.Append(GetHref());
stringBuilder.Append('"');

if (Link.OpenInNewTab)
if (Link!.OpenInNewTab)
{
stringBuilder.Append(" target=\"_blank\"");
}

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;
}
}
12 changes: 3 additions & 9 deletions src/Dfe.PlanTech.Web/ViewComponents/FooterLinksViewComponent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,10 @@ namespace Dfe.PlanTech.Web.ViewComponents;
/// <summary>
/// View component that retrieves and displays links in the layout's footer
/// </summary>
public class FooterLinksViewComponent : ViewComponent
public class FooterLinksViewComponent(IGetNavigationQuery getNavQuery, ILogger<FooterLinksViewComponent> logger) : ViewComponent
{
private readonly IGetNavigationQuery _getNavQuery;
private readonly ILogger<FooterLinksViewComponent> _logger;

public FooterLinksViewComponent(IGetNavigationQuery getNavQuery, ILogger<FooterLinksViewComponent> logger)
{
_getNavQuery = getNavQuery;
_logger = logger;
}
private readonly IGetNavigationQuery _getNavQuery = getNavQuery;
private readonly ILogger<FooterLinksViewComponent> _logger = logger;

/// <summary>
/// Retrieve the navigation links using <see cref="IGetNavigationQuery"/> then return the view
Expand Down

0 comments on commit 32adb19

Please sign in to comment.