Skip to content

Commit

Permalink
improve website listing
Browse files Browse the repository at this point in the history
  • Loading branch information
dereke committed Aug 20, 2021
1 parent 45b5e5a commit d474eaa
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 28 deletions.
2 changes: 2 additions & 0 deletions app/controllers/initiatives_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ def update
@initiative.assign_attributes initiative_params
@initiative.update_location_from_postcode

@initiative.remove_empty_websites
if @initiative.save(validate: publication_status != 'draft')
@initiative.images.attach images if images
if (params[:step] || '').empty?
Expand Down Expand Up @@ -119,6 +120,7 @@ def update_solutions(initiative)

def set_initiative
@initiative = Initiative.find(params[:id])
@initiative.websites << InitiativeWebsite.new if @initiative.websites.empty?

redirect_to initiatives_url unless can_edit_initiative?(@initiative)
end
Expand Down
4 changes: 4 additions & 0 deletions app/models/initiative.rb
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,10 @@ def carbon_saving_quantified?
carbon_saving_amount&.positive?
end

def remove_empty_websites
websites.delete(websites.select { |website| website.url.blank? })
end

private

def set_default_location
Expand Down
28 changes: 9 additions & 19 deletions app/views/initiatives/_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -117,28 +117,18 @@
<%= f.check_box :consent_to_share_phone, 'Make this phone number public. (Please ensure you have permission to share it first)' %>
<% end %>

<h2 class="u-mb">Website & Social Media</h2>
<%= f.label :website, 'Website(s) or social media links for further project info' %>
<div id="initiative_websites" class="CollectionItem-container">
<table>
<thead>
<tr>
<th>Website</th>
<th>Delete?</th>
</tr>
</thead>

<tbody>
<%= f.fields_for :websites do |website_form| %>
<%= render 'partials/website_fields_row', f: website_form %>
<% end %>
</tbody>
</table>
</div>
<div class="FormField">
<%= f.fields_for :websites, InitiativeWebsite.new do |website_form| %>
<%= add_object_link 'Add website', 'initiative_websites', :partial => 'partials/website_fields_row', :locals => { :f => website_form } %>
<%= f.fields_for :websites do |website_form| %>
<%= website_form.form_field :url do %>
<%= website_form.text_field :url, placeholder: 'eg: https://twitter.com/carbon-map' %>
<% end %>
<% end %>
<%= javascript_packs_with_chunks_tag 'initiative_websites', 'data-turbolinks-track': 'reload' %>
</div>
<a href="#" data-content="add_initiative_website">
<span>Add another</span><img src="<%=asset_pack_path("media/icons/right_arrow.svg")%>">
</a>
<% end %>

<%= initiative_step 5, @current_initiative_step, title: 'Help us understand how you got going', next_text: 'Next: project sector and notes' do %>
Expand Down
9 changes: 0 additions & 9 deletions app/views/partials/_initiative_website.html.erb

This file was deleted.

18 changes: 18 additions & 0 deletions frontend/packs/initiative_websites.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
window.addEventListener("load", () => {
document
.querySelector("[data-content=add_initiative_website]")
.addEventListener("click", e => {
e.preventDefault();
let index = 0;
while (
document.querySelector(
`#initiative_websites_attributes_${index}_url`
) != null
) {
index++;
}
document.querySelector(
"#initiative_websites"
).innerHTML += `<div class="FormField"><input class="FormField-input" placeholder="eg: https://twitter.com/carbon-map" type="text" name="initiative[websites_attributes][${index}][url]" id="initiative_websites_attributes_${index}_url"></div>`;
});
});

0 comments on commit d474eaa

Please sign in to comment.