Skip to content

Commit

Permalink
Migrate featured links to new add-another
Browse files Browse the repository at this point in the history
Call the new publishing components add another template for featured links
  • Loading branch information
dnkrj committed Nov 22, 2024
1 parent 46e776f commit 19e7785
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 77 deletions.
2 changes: 1 addition & 1 deletion app/views/admin/organisation_translations/edit.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@
},
} %>
<%= render "admin/shared/featured_link_fields", form: form %>
<%= render "admin/shared/featured_links_fields", form: form %>

<div class="govuk-button-group govuk-!-margin-top-8">
<%= render "govuk_publishing_components/components/button", {
Expand Down
2 changes: 1 addition & 1 deletion app/views/admin/organisations/_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -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 %>
</div>
<% end %>

Expand Down
124 changes: 50 additions & 74 deletions app/views/admin/shared/_featured_link_fields.html.erb
Original file line number Diff line number Diff line change
@@ -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 %>

<div data-module="AddAnother" data-add-text="Add another featured link">
<%= form.fields_for :featured_links do |featured_link_form| %>
<div class="js-duplicate-fields-set govuk-!-margin-bottom-6">
<% 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 %>
</div>
<%= 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 %>
</div>
<% end %>
30 changes: 30 additions & 0 deletions app/views/admin/shared/_featured_links_fields.html.erb
Original file line number Diff line number Diff line change
@@ -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: }),
} %>
2 changes: 1 addition & 1 deletion app/views/admin/world_location_news/edit.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@
font_size: "l",
} %>
<%= render "admin/shared/featured_link_fields", form: form %>
<%= render "admin/shared/featured_links_fields", form: form %>

<div class="govuk-button-group govuk-!-margin-top-8">
<%= render "govuk_publishing_components/components/button", {
Expand Down

0 comments on commit 19e7785

Please sign in to comment.