diff --git a/app/views/admin/organisation_translations/edit.html.erb b/app/views/admin/organisation_translations/edit.html.erb index 91f8cf325c1..62dff781bb2 100644 --- a/app/views/admin/organisation_translations/edit.html.erb +++ b/app/views/admin/organisation_translations/edit.html.erb @@ -60,7 +60,7 @@ }, } %> - <%= render "admin/shared/featured_link_fields", form: form %> + <%= render "admin/shared/featured_links_fields", form: form %>
<%= render "govuk_publishing_components/components/button", { diff --git a/app/views/admin/organisations/_form.html.erb b/app/views/admin/organisations/_form.html.erb index a6f545ff8dd..2170c9474ab 100644 --- a/app/views/admin/organisations/_form.html.erb +++ b/app/views/admin/organisations/_form.html.erb @@ -314,7 +314,7 @@ margin_bottom: 3, } %> - <%= render "admin/shared/featured_link_fields", form: form, remove_banner: true %> + <%= render "admin/shared/featured_links_fields", form: form, remove_banner: true %>
<% end %> diff --git a/app/views/admin/shared/_featured_link_fields.html.erb b/app/views/admin/shared/_featured_link_fields.html.erb index 7fbcfde4c7f..c168dda9e32 100644 --- a/app/views/admin/shared/_featured_link_fields.html.erb +++ b/app/views/admin/shared/_featured_link_fields.html.erb @@ -1,77 +1,53 @@ -<% - featured_links = form.object.featured_links - featured_links = [LocalisedModel.new(FeaturedLink.new, I18n.default_locale)] unless featured_links.any? - model = form.object.model_name.singular +<%= form.fields_for :featured_links, featured_link do |featured_link_form| %> + <% if @translation_locale && @translation_locale.code != I18n.default_locale %> + <%= render "components/translated_input", { + input: { + label: { + text: "Title", + }, + heading_size: "m", + value: featured_link_form.object.title, + name: "#{model}[featured_links_attributes][#{index}][title]", + id: "#{model}_featured_links[#{index}]_title", + }, + details: { + text: featured_link_form.object.title, + }, + } %> - if form.object.respond_to?(:visible_featured_links_count) - visible_links = form.object.visible_featured_links_count - else - visible_links = FeaturedLink::DEFAULT_SET_SIZE - end + <%= render "components/translated_input", { + input: { + label: { + text: "URL", + }, + heading_size: "m", + value: featured_link_form.object.url, + name: "#{model}[featured_links_attributes][#{index}][url]", + id: "#{model}_featured_links[#{index}]_url", + }, + details: { + text: featured_link_form.object.url, + }, + } %> + <% else %> + <%= render "govuk_publishing_components/components/input", { + label: { + text: "Title", + }, + heading_size: "m", + value: featured_link_form.object.title, + name: "#{model}[featured_links_attributes][#{index}][title]", + id: "#{model}_featured_links[#{index}]_title", + } %> - remove_banner ||= false -%> - -<% unless remove_banner %> - <%= render "govuk_publishing_components/components/inset_text", { - text: "Only the first #{visible_links} links will be shown on the public site.", - } %> -<% end %> - -
- <%= form.fields_for :featured_links do |featured_link_form| %> -
- <% if @translation_locale && @translation_locale.code != I18n.default_locale %> - <%= render "components/translated_input", { - input: { - label: { - text: "Title", - }, - heading_size: "m", - value: featured_link_form.object.title, - name: "#{model}[featured_links_attributes][#{featured_link_form.index}][title]", - id: "#{model}_featured_links[#{featured_link_form.index}]_title", - }, - details: { - text: featured_link_form.object.title, - }, - } %> - - <%= render "components/translated_input", { - input: { - label: { - text: "URL", - }, - heading_size: "m", - value: featured_link_form.object.url, - name: "#{model}[featured_links_attributes][#{featured_link_form.index}][url]", - id: "#{model}_featured_links[#{featured_link_form.index}]_url", - }, - details: { - text: featured_link_form.object.url, - }, - } %> - <% else %> - <%= render "govuk_publishing_components/components/input", { - label: { - text: "Title", - }, - heading_size: "m", - value: featured_link_form.object.title, - name: "#{model}[featured_links_attributes][#{featured_link_form.index}][title]", - id: "#{model}_featured_links[#{featured_link_form.index}]_title", - } %> - - <%= render "govuk_publishing_components/components/input", { - label: { - text: "URL", - }, - heading_size: "m", - value: featured_link_form.object.url, - name: "#{model}[featured_links_attributes][#{featured_link_form.index}][url]", - id: "#{model}_featured_links[#{featured_link_form.index}]_url", - } %> - <% end %> -
+ <%= render "govuk_publishing_components/components/input", { + label: { + text: "URL", + }, + heading_size: "m", + value: featured_link_form.object.url, + name: "#{model}[featured_links_attributes][#{index}][url]", + id: "#{model}_featured_links[#{index}]_url", + } %> <% end %> -
+<% end %> diff --git a/app/views/admin/shared/_featured_links_fields.html.erb b/app/views/admin/shared/_featured_links_fields.html.erb new file mode 100644 index 00000000000..a31c8b64e94 --- /dev/null +++ b/app/views/admin/shared/_featured_links_fields.html.erb @@ -0,0 +1,30 @@ +<% + featured_links = form.object.featured_links + new_featured_link = LocalisedModel.new(FeaturedLink.new, I18n.default_locale) + model = form.object.model_name.singular + + if form.object.respond_to?(:visible_featured_links_count) + visible_links = form.object.visible_featured_links_count + else + visible_links = FeaturedLink::DEFAULT_SET_SIZE + end + + remove_banner ||= false +%> + +<% unless remove_banner %> + <%= render "govuk_publishing_components/components/inset_text", { + text: "Only the first #{visible_links} links will be shown on the public site.", + } %> +<% end %> + +<%= render "govuk_publishing_components/components/add_another", { + add_button_text: "Add another featured link", + items: featured_links.each_with_index.map do |featured_link, index| + { + fields: render(partial: "admin/shared/featured_link_fields", locals: { form:, featured_link:, index:, model: }), + destroy_checkbox: render("govuk_publishing_components/components/checkboxes", { name: "#{model}[featured_links_attributes][#{index}[_destroy]", items: [{label: "Delete", value: "1" }]}) + } + end, + empty: render(partial: "admin/shared/featured_link_fields", locals: { form:, featured_link: new_featured_link, index: featured_links.length, model: }), +} %> diff --git a/app/views/admin/world_location_news/edit.html.erb b/app/views/admin/world_location_news/edit.html.erb index bf221309b44..62fa6b5f592 100644 --- a/app/views/admin/world_location_news/edit.html.erb +++ b/app/views/admin/world_location_news/edit.html.erb @@ -71,7 +71,7 @@ font_size: "l", } %> - <%= render "admin/shared/featured_link_fields", form: form %> + <%= render "admin/shared/featured_links_fields", form: form %>
<%= render "govuk_publishing_components/components/button", {