diff --git a/caseworker/advice/forms.py b/caseworker/advice/forms.py index 2c661349da..d7b09cac5f 100644 --- a/caseworker/advice/forms.py +++ b/caseworker/advice/forms.py @@ -6,7 +6,13 @@ from crispy_forms_gds.layout import Field, Layout, Submit from crispy_forms_gds.choices import Choice -from core.forms.layouts import ConditionalRadios, ConditionalRadiosQuestion, ExpandingFieldset, RadioTextArea +from core.forms.layouts import ( + ConditionalRadios, + ConditionalRadiosQuestion, + ExpandingFieldset, + RadioTextArea, + AdditiveTextArea, +) from core.forms.utils import coerce_str_to_bool from caseworker.tau.summaries import get_good_on_application_tau_summary from caseworker.tau.widgets import GoodsMultipleSelect @@ -84,7 +90,7 @@ def __init__(self, team_name, *args, **kwargs): class PicklistAdviceForm(forms.Form): - def _picklist_to_choices(self, picklist_data): + def _picklist_to_choices(self, picklist_data, include_other=True): reasons_choices = [] reasons_text = {"other": ""} @@ -95,7 +101,8 @@ def _picklist_to_choices(self, picklist_data): choice = Choice(key, result.get("name"), divider="or") reasons_choices.append(choice) reasons_text[key] = result.get("text") - reasons_choices.append(Choice("other", "Other")) + if include_other: + reasons_choices.append(Choice("other", "Other")) return reasons_choices, reasons_text @@ -152,7 +159,7 @@ def __init__(self, *args, **kwargs): approval_choices, approval_text = self._picklist_to_choices(approval_reason) self.approval_text = approval_text - proviso_choices, proviso_text = self._picklist_to_choices(proviso) + proviso_choices, proviso_text = self._picklist_to_choices(proviso, include_other=False) self.proviso_text = proviso_text footnote_details_choices, footnote_text = self._picklist_to_choices(footnote_details) @@ -166,7 +173,7 @@ def __init__(self, *args, **kwargs): self.helper.layout = Layout( RadioTextArea("approval_radios", "approval_reasons", self.approval_text), ExpandingFieldset( - RadioTextArea("proviso_radios", "proviso", self.proviso_text), + AdditiveTextArea("proviso_radios", "proviso", self.proviso_text), "instructions_to_exporter", RadioTextArea("footnote_details_radios", "footnote_details", self.footnote_text), legend="Add a licence condition, instruction to exporter or footnote", diff --git a/caseworker/assets/javascripts/additive-populate-textarea.js b/caseworker/assets/javascripts/additive-populate-textarea.js new file mode 100644 index 0000000000..3e6e1a83e0 --- /dev/null +++ b/caseworker/assets/javascripts/additive-populate-textarea.js @@ -0,0 +1,31 @@ +class PopulateTextOnRadioInput { + constructor($el) { + this.$addButtons = $el.querySelectorAll("a[data-additive-key]"); + this.$textArea = $el.querySelector("textarea"); + this.$lookup = JSON.parse( + $el.querySelector(`#${this.$textArea.name}`).textContent, + ); + } + + init() { + this.$addButtons.forEach((input) => { + input.addEventListener("click", (event) => { + event.preventDefault(); + const text = this.$lookup[input.getAttribute("data-additive-key")]; + if (this.$textArea.value == "") { + this.$textArea.value = text; + } else { + this.$textArea.value = this.$textArea.value + "\n\n\n" + text; + } + }); + }); + } +} + +export default function initAdditiveTextArea() { + document + .querySelectorAll("[data-module=additive-textarea]") + .forEach(($el) => new PopulateTextOnRadioInput($el).init()); +} + +export { PopulateTextOnRadioInput, initAdditiveTextArea }; diff --git a/caseworker/assets/javascripts/main.js b/caseworker/assets/javascripts/main.js index 05f70f97a0..9a9ea80974 100644 --- a/caseworker/assets/javascripts/main.js +++ b/caseworker/assets/javascripts/main.js @@ -21,6 +21,7 @@ import { initCaseNotes, initMentionUsers } from "./case-notes"; import { initExpanders } from "./list-expander"; import { initCustomisers } from "./customiser"; import { initRadioTextArea } from "./radio-populate-textarea.js"; +import { initAdditiveTextArea } from "./additive-populate-textarea.js"; import initSelectAllTables from "./select-all-tables.js"; import { initTableExpanders } from "./table-expander.js"; @@ -45,6 +46,7 @@ initMentionUsers(); initExpanders(); initCustomisers(); initRadioTextArea(); +initAdditiveTextArea(); initSelectAllTables(); initTableExpanders(); diff --git a/core/forms/layouts.py b/core/forms/layouts.py index ee717f692e..77b4d7b241 100644 --- a/core/forms/layouts.py +++ b/core/forms/layouts.py @@ -187,6 +187,43 @@ def render(self, form, form_style, context, template_pack=TEMPLATE_PACK, **kwarg return render_to_string(template, context.flatten()) +class AdditiveTextArea(TemplateNameMixin): + template = "%s/layout/additive_textarea.html" + + def __init__(self, radio_field, field, json_choices): + if not isinstance(field, str): + raise TypeError(f"{self.__class__.__name__} only accepts field as a string parameter") + if not isinstance(radio_field, str): + raise TypeError(f"{self.__class__.__name__} only accepts radio_field as a string parameter") + self.field = field + self.radio_field = radio_field + self.json_choices = self.sanitise_json_choices(json_choices) + + def sanitise_json_choices(self, choices): + if not isinstance(choices, dict): + raise TypeError(f"{self.__class__.__name__} only accepts json_choices as a dict parameter") + + for key in choices: + if not isinstance(choices[key], str): + raise TypeError(f"{self.__class__.__name__} only accepts json_choices with string values") + return choices + + def render(self, form, form_style, context, template_pack=TEMPLATE_PACK, **kwargs): + template = self.get_template_name(template_pack) + + bound_field = form[self.field] + radio_field = form[self.radio_field] + context.update( + { + "field": bound_field, + "radio_field": radio_field, + "json_choices": self.json_choices, + } + ) + + return render_to_string(template, context.flatten()) + + class StarRadioSelect(TemplateNameMixin): template = "%s/layout/star_radio_select.html" diff --git a/core/forms/templates/gds/layout/additive_snippets.html b/core/forms/templates/gds/layout/additive_snippets.html new file mode 100644 index 0000000000..8ff08e6ac4 --- /dev/null +++ b/core/forms/templates/gds/layout/additive_snippets.html @@ -0,0 +1,27 @@ + + +{% load l10n crispy_forms_gds %} +
+ + {% if field.label %} + + {% if legend_tag %}<{{ legend_tag }} class="govuk-fieldset__heading">{% endif %} + {{ field.label|safe }} + {% if legend_tag %}{% endif %} + + {% endif %} + + {% include 'gds/layout/help_text_and_errors.html' %} + +
+ {% for choice in field.field.choices %} +

+ {{ choice.1|unlocalize }} Add +

+ {% endfor %} +
+ +
diff --git a/core/forms/templates/gds/layout/additive_textarea.html b/core/forms/templates/gds/layout/additive_textarea.html new file mode 100644 index 0000000000..21a31f0349 --- /dev/null +++ b/core/forms/templates/gds/layout/additive_textarea.html @@ -0,0 +1,10 @@ +{% load l10n crispy_forms_gds %} +
+
+ {% include 'gds/layout/additive_snippets.html' with field=radio_field %} +
+
+ {% include 'gds/layout/multifield.html' with field=field %} +
+ {{ json_choices | json_script:field.html_name }} +