Skip to content

Commit

Permalink
Merge pull request #532 from DFE-Digital/SWCD-2457-credits-block-comp…
Browse files Browse the repository at this point in the history
…onent

Swcd 2457 credits block component
  • Loading branch information
mattb-hippo authored Oct 24, 2024
2 parents 2664974 + ccba8ed commit 3daf920
Show file tree
Hide file tree
Showing 15 changed files with 230 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public class EntityResolverTests
[TestCase("contentLink", typeof(ContentLink))]
[TestCase("contentsAnchor", typeof(ContentsAnchor))]
[TestCase("contentSeparator", typeof(ContentSeparator))]
[TestCase("creditBlock", typeof(CreditBlock))]
[TestCase("detailedPathway", typeof(DetailedPathway))]
[TestCase("detailedRole", typeof(DetailedRole))]
[TestCase("details", typeof(Details))]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public partial class PartialsFactoryTests
new object[] { new ContentLink(), "_ContentLink" },
new object[] { new ContentSeparator(), "_ContentSeparator" },
new object[] { new ContentsAnchor(), "_ContentsAnchor" },
new object[] { new CreditBlock(), "_CreditBlock" },
new object[] { new DetailedPathway(), "_DetailedPathway" },
new object[] { new DetailedRole(), "_DetailedRole" },
new object[] { new Details(), "_Details" },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -189,4 +189,34 @@ public void Paragraph_Renders_With_AreaOfPracticeList()
// assert
actual.Should().Be($"<p class=\"HtmlEncode[[govuk-body-m]]\">EEE</p>");
}

[Test]
public void Paragraph_Renders_Inline_If_Options_Set()
{
// arrange
var stringWriter = new StringWriter();
var paragraph = new Paragraph()
{
Content = new List<IContent>
{
new Hyperlink()
}
};

_textRenderer.Render(Arg.Any<Text>()).Returns(new HtmlString("AAA"));
_roleListRenderer.Render(Arg.Any<RoleList>()).Returns(new HtmlString("BBB"));
_hyperlinkRenderer.Render(Arg.Any<Hyperlink>()).Returns(new HtmlString("CCC"));
_contentLinkRenderer.Render(Arg.Any<ContentLink>()).Returns(new HtmlString("DDD"));
_areaOfPracticeListRenderer.Render(Arg.Any<AreaOfPracticeList>()).Returns(new HtmlString("EEE"));

var rendererOptions = new RendererOptions("", "inline");

// act
var htmlContent = _sut.Render(paragraph, rendererOptions);
htmlContent.WriteTo(stringWriter, new HtmlTestEncoder());
var actual = stringWriter.ToString();

// assert
actual.Should().Be($"<span>CCC</span>");
}
}
1 change: 1 addition & 0 deletions Childrens-Social-Care-CPD/Contentful/EntityResolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public Type Resolve(string contentTypeId)
"contentLink" => typeof(ContentLink),
"contentsAnchor" => typeof(ContentsAnchor),
"contentSeparator" => typeof(ContentSeparator),
"creditBlock" => typeof(CreditBlock),
"detailedPathway" => typeof(DetailedPathway),
"detailedRole" => typeof(DetailedRole),
"details" => typeof(Details),
Expand Down
10 changes: 10 additions & 0 deletions Childrens-Social-Care-CPD/Contentful/Models/CreditBlock.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using Contentful.Core.Models;

namespace Childrens_Social_Care_CPD.Contentful.Models;

public class CreditBlock : IContent
{
public Document DeveloperOfResource { get; set; }
public Document SecondaryDevelopersOfResource { get; set; }

}
1 change: 1 addition & 0 deletions Childrens-Social-Care-CPD/Contentful/PartialsFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public static string GetPartialFor(IContent item)
ContentLink => "_ContentLink",
ContentSeparator => "_ContentSeparator",
ContentsAnchor => "_ContentsAnchor",
CreditBlock => "_CreditBlock",
DetailedRole => "_DetailedRole",
DetailedPathway => "_DetailedPathway",
Details => "_Details",
Expand Down
3 changes: 2 additions & 1 deletion Childrens-Social-Care-CPD/Contentful/Renderers/IRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@

namespace Childrens_Social_Care_CPD.Contentful.Renderers;

public record RendererOptions(string Css = default)
public record RendererOptions(string Css = default, string RenderStyle = null)
{
public bool HasCss => Css != default;
public bool RenderInline => RenderStyle == "inline";
}

public interface IRenderer<in T>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,19 @@

namespace Childrens_Social_Care_CPD.Contentful.Renderers;

internal class ParagraphRenderer(IRenderer<Text> textRenderer, IRenderer<RoleList> roleListRenderer, IRenderer<Hyperlink> hyperlinkRenderer, IRenderer<ContentLink> contentLinkRenderer, IRenderer<AreaOfPracticeList> areaOfPracticeListRenderer) : IRenderer<Paragraph>
internal class ParagraphRenderer(IRenderer<Text> textRenderer, IRenderer<RoleList> roleListRenderer, IRenderer<Hyperlink> hyperlinkRenderer, IRenderer<ContentLink> contentLinkRenderer, IRenderer<AreaOfPracticeList> areaOfPracticeListRenderer) : IRendererWithOptions<Paragraph>
{
public IHtmlContent Render(Paragraph item)
public IHtmlContent Render(Paragraph item, RendererOptions options = null)
{
var p = new TagBuilder("p");
p.AddCssClass("govuk-body-m");
TagBuilder p;
if (options?.RenderInline ?? false) {
p = new TagBuilder("span");
}
else
{
p = new TagBuilder("p");
p.AddCssClass("govuk-body-m");
}

foreach (var content in item.Content)
{
Expand All @@ -33,4 +40,9 @@ public IHtmlContent Render(Paragraph item)

return p;
}

public IHtmlContent Render(Paragraph item)
{
return Render(item, null);
}
}
6 changes: 5 additions & 1 deletion Childrens-Social-Care-CPD/Controllers/ContentController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,11 @@ public async Task<IActionResult> Index(string pageName = "home", bool preference
PreferenceSet: preferenceSet,
BackLink: content.BackLink,
FeedbackSubmitted: fs,
BreadcrumbTrail: await BuildBreadcrumbTrail(new List<KeyValuePair<string, string>>(), content, pagesVisited, cancellationToken));
BreadcrumbTrail: await BuildBreadcrumbTrail(new List<KeyValuePair<string, string>>(), content, pagesVisited, cancellationToken),
PublishDates: new PublishDates(
FirstPublishedAt: content.Sys?.CreatedAt,
LastPublishedAt: content.Sys?.UpdatedAt
));

ViewData["ContextModel"] = contextModel;
ViewData["StateModel"] = new StateModel();
Expand Down
8 changes: 7 additions & 1 deletion Childrens-Social-Care-CPD/Models/ContextModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

namespace Childrens_Social_Care_CPD.Models;

public record PublishDates (
DateTime ?FirstPublishedAt,
DateTime ?LastPublishedAt
);

public record ContextModel(
string Id,
string Title,
Expand All @@ -12,7 +17,8 @@ public record ContextModel(
bool HideConsent = false,
ContentLink BackLink = null,
bool FeedbackSubmitted = false,
List<KeyValuePair<string, string>> BreadcrumbTrail = null)
List<KeyValuePair<string, string>> BreadcrumbTrail = null,
PublishDates PublishDates = null)
{
public Stack<string> ContentStack { get; } = new Stack<string>();
}
25 changes: 25 additions & 0 deletions Childrens-Social-Care-CPD/Views/Shared/_CreditBlock.cshtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
@using Childrens_Social_Care_CPD.Contentful.Models;

@model CreditBlock

@{
var contextModel = (ContextModel)ViewData["ContextModel"];
var dateFormat = "dd MMMM yyyy";
ViewData["RenderStyle"] = "inline";
}

<div class="govuk-grid-row">
<div class="credit-block govuk-details">
The resources have been developed by <partial name="_RichText" model="Model.DeveloperOfResource" view-data="ViewData" />
@if (@Model.SecondaryDevelopersOfResource != null)
{
@:in collaboration with
<partial name="_RichText" model="Model.SecondaryDevelopersOfResource" view-data="ViewData" />
}
<br />
Published: @contextModel.PublishDates.FirstPublishedAt.Value.ToString(dateFormat)
<br />
Last updated: @contextModel.PublishDates.LastPublishedAt.Value.ToString(dateFormat)
<br />
</div>
</div>
4 changes: 2 additions & 2 deletions Childrens-Social-Care-CPD/Views/Shared/_RichText.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
@inject IRenderer<Heading4> _heading4Renderer
@inject IRenderer<Heading5> _heading5Renderer
@inject IRenderer<Heading6> _heading6Renderer
@inject IRenderer<Paragraph> _paragraphRenderer
@inject IRendererWithOptions<Paragraph> _paragraphRenderer
@inject IRenderer<Quote> _quoteRenderer
@inject IRenderer<HorizontalRuler> _horizontalRulerRenderer
@inject IRenderer<Table> _tableRenderer
Expand All @@ -28,7 +28,7 @@
switch (node)
{
case HorizontalRuler hr: @_horizontalRulerRenderer.Render(hr) break;
case Paragraph paragraph: @_paragraphRenderer.Render(paragraph) break;
case Paragraph paragraph: @_paragraphRenderer.Render(paragraph, new RendererOptions("", (string)ViewData["RenderStyle"] ?? null)) break;
case List list: @_listRenderer.Render(list) break;
case Heading1 heading1: @_heading1Renderer.Render(heading1) break;
case Heading2 heading2: @_heading2Renderer.Render(heading2) break;
Expand Down
127 changes: 127 additions & 0 deletions Contentful-Schema/migrations/0012-credit-block-component.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
module.exports = async function (migration, { makeRequest }) {
const creditBlock = migration
.createContentType("creditBlock")
.name("Credit Block")
.description(
"Shows who developed a resource, the date it was published and the date it was last updated."
)
.displayField("name");

creditBlock
.createField("name")
.name("Name")
.type("Symbol")
.localized(false)
.required(true)
.validations([
{
unique: true,
},
])
.disabled(false)
.omitted(false);

creditBlock
.createField("developerOfResource")
.name("Developer of resource")
.type("RichText")
.localized(false)
.required(true)
.validations([
{
enabledMarks: [],
message: "Marks are not allowed",
},
{
enabledNodeTypes: ["hyperlink"],
message: "Only link to Url nodes are allowed",
},
{
nodes: {},
},
])
.disabled(false)
.omitted(false);

creditBlock
.createField("secondaryDevelopersOfResource")
.name("Secondary developer(s) of resource")
.type("RichText")
.localized(false)
.required(false)
.validations([
{
enabledMarks: [],
message: "Marks are not allowed",
},
{
enabledNodeTypes: ["hyperlink"],
message: "Only link to Url nodes are allowed",
},
{
nodes: {},
},
])
.disabled(false)
.omitted(false);

creditBlock.changeFieldControl(
"name",
"builtin",
"singleLine",
{
helpText:
"Name is only for internal reference, and will not display on website",
}
);

creditBlock.changeFieldControl(
"developerOfResource",
"builtin",
"richTextEditor",
{
helpText:
"Who created this resource? You can make their name a hyperlink to their website if appropriate.",
}
);

creditBlock.changeFieldControl(
"secondaryDevelopersOfResource",
"builtin",
"richTextEditor",
{
helpText:
"Optional: Include anyone that this resource was created in collaboration with. E.g. 'DfE'.",
}
);

/*
* Add creditBlock to list of content types allowed in content pages
*/
const contentTypeId = "content",
linkingFieldId = "items",
creditBlockTypeId = "creditBlock";

const response = await makeRequest({
method: "GET",
url: `/content_types?sys.id[in]=${contentTypeId}`,
});

const validations = response.items[0].fields
.filter((field) => field.id == linkingFieldId)[0]
.items.validations.map((rule) => {
if (
rule.linkContentType &&
!rule.linkContentType.includes(creditBlockTypeId)
) {
rule.linkContentType.push(creditBlockTypeId);
}
return rule;
});

migration.editContentType(contentTypeId).editField(linkingFieldId).items({
type: "Link",
linkType: "Entry",
validations: validations,
});
};
3 changes: 2 additions & 1 deletion Contentful-Schema/migrations/manifest.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@
0008-details-component.cjs
0009-asset-download-component.cjs
0010-breadcrumbs.cjs
0011-create-agency-standards-page-category.cjs
0011-create-agency-standards-page-category.cjs
0012-credit-block-component.cjs
Binary file modified Contentful-Schema/migrations/migrations.tar.gz
Binary file not shown.

0 comments on commit 3daf920

Please sign in to comment.