From 57841403705d29e4835c25d72aec9d27a4720e95 Mon Sep 17 00:00:00 2001 From: Arun Kumar Madesh <56151567+arunkumar461@users.noreply.github.com> Date: Sat, 4 Jan 2025 10:40:52 +0000 Subject: [PATCH] CCMSPUI-468 | Refactor and Add tests --- .../dialect/DatePickerAttributes.java | 19 +++++ .../DatePickerElementTagProcessor.java | 77 ++++++++++--------- .../springboot/dialect/MojCustomDialect.java | 6 +- .../MoJDatePickerElementTagProcessorTest.java | 31 +++++++- .../dialect/ThymeleafTestConfig.java | 1 + .../resources/templates/test-datepicker.html | 20 +++++ 6 files changed, 115 insertions(+), 39 deletions(-) create mode 100644 laa-ccms-spring-boot-starters/laa-ccms-spring-boot-starter-govuk-dialect/src/main/java/uk/gov/laa/ccms/springboot/dialect/DatePickerAttributes.java create mode 100644 laa-ccms-spring-boot-starters/laa-ccms-spring-boot-starter-govuk-dialect/src/test/resources/templates/test-datepicker.html diff --git a/laa-ccms-spring-boot-starters/laa-ccms-spring-boot-starter-govuk-dialect/src/main/java/uk/gov/laa/ccms/springboot/dialect/DatePickerAttributes.java b/laa-ccms-spring-boot-starters/laa-ccms-spring-boot-starter-govuk-dialect/src/main/java/uk/gov/laa/ccms/springboot/dialect/DatePickerAttributes.java new file mode 100644 index 0000000..fb07a90 --- /dev/null +++ b/laa-ccms-spring-boot-starters/laa-ccms-spring-boot-starter-govuk-dialect/src/main/java/uk/gov/laa/ccms/springboot/dialect/DatePickerAttributes.java @@ -0,0 +1,19 @@ +package uk.gov.laa.ccms.springboot.dialect; + +/** + * DatePickerAttributes. + */ +public record DatePickerAttributes( + String id, + String name, + String label, + String hint, + String errorMessage, + String value, + String minDate, + String maxDate) { + + public boolean hasError() { + return !errorMessage.isEmpty(); + } +} diff --git a/laa-ccms-spring-boot-starters/laa-ccms-spring-boot-starter-govuk-dialect/src/main/java/uk/gov/laa/ccms/springboot/dialect/DatePickerElementTagProcessor.java b/laa-ccms-spring-boot-starters/laa-ccms-spring-boot-starter-govuk-dialect/src/main/java/uk/gov/laa/ccms/springboot/dialect/DatePickerElementTagProcessor.java index d610ece..095b762 100644 --- a/laa-ccms-spring-boot-starters/laa-ccms-spring-boot-starter-govuk-dialect/src/main/java/uk/gov/laa/ccms/springboot/dialect/DatePickerElementTagProcessor.java +++ b/laa-ccms-spring-boot-starters/laa-ccms-spring-boot-starter-govuk-dialect/src/main/java/uk/gov/laa/ccms/springboot/dialect/DatePickerElementTagProcessor.java @@ -10,7 +10,7 @@ import org.thymeleaf.templatemode.TemplateMode; /** - * hello. + * Transforms elements into standard HTML button elements. */ public class DatePickerElementTagProcessor extends AbstractElementTagProcessor { @@ -26,74 +26,77 @@ protected void doProcess(ITemplateContext context, IProcessableElementTag tag, IElementTagStructureHandler structureHandler) { Map attributes = ProcessorUtils.parseAttributes(context, tag); - - String id = attributes.getOrDefault("id", "date"); - String name = attributes.getOrDefault("name", "date"); - String value = attributes.get("value"); - String label = attributes.getOrDefault("label", "Date"); - String hint = attributes.getOrDefault("hint", "For example, 17/5/2024."); - String errorMessage = attributes.getOrDefault("errorMessage", "Enter or select a date"); - String minDate = attributes.get("minDate"); - String maxDate = attributes.get("maxDate"); - - boolean hasError = !errorMessage.isEmpty(); - - String datePickerHtml = - buildDatePickerHtml(id, name, label, hint, errorMessage, hasError, minDate, maxDate); - + DatePickerAttributes datePickerAttributes = new DatePickerAttributes( + attributes.getOrDefault("id", "date"), + attributes.getOrDefault("name", "date"), + attributes.getOrDefault("label", "Date"), + attributes.get("hint"), + attributes.get("errorMessage"), + attributes.get("value"), + attributes.get("minDate"), + attributes.get("maxDate") + ); + + String datePickerHtml = buildDatePickerHtml(datePickerAttributes); final IModelFactory modelFactory = context.getModelFactory(); final IModel model = modelFactory.parse(context.getTemplateData(), datePickerHtml); structureHandler.replaceWith(model, false); } - private String buildDatePickerHtml(String id, String name, String label, String hint, - String errorMessage, boolean hasError, String minDate, - String maxDate) { + private String buildDatePickerHtml(DatePickerAttributes datePickerAttributes) { StringBuilder html = new StringBuilder(); html.append("
") .append("
") - .append("