Only re-render my ballot after voting

We were rendering the whole sidebar again, which wasn't necessary since
most of it remains unchanged. This resulted in complicated code to pass
the necessary information to render the same map that was already
rendered. Furthermore, when users had been moving the map around or
zooming out, we were resetting the map to its default settings after
voting, which was potentially annoying.

This also fixes the wrong investments being displayed in the map after
voting; only the investments on the current page were displayed in this
case while the index displayed all of them.
This commit is contained in:
Javi Martín
2023-03-07 16:15:34 +01:00
parent d0be5c4850
commit 4954038076
7 changed files with 43 additions and 20 deletions

View File

@@ -1,14 +1,18 @@
class Budgets::Investments::MyBallotComponent < ApplicationComponent class Budgets::Investments::MyBallotComponent < ApplicationComponent
attr_reader :ballot, :heading, :investment_ids, :assigned_heading attr_reader :ballot, :heading, :investment_ids, :assigned_heading
delegate :heading_link, to: :helpers delegate :can?, :heading_link, to: :helpers
def initialize(ballot:, heading:, investment_ids:, assigned_heading:) def initialize(ballot:, heading:, investment_ids:, assigned_heading: nil)
@ballot = ballot @ballot = ballot
@heading = heading @heading = heading
@investment_ids = investment_ids @investment_ids = investment_ids
@assigned_heading = assigned_heading @assigned_heading = assigned_heading
end end
def render?
heading && can?(:show, ballot)
end
private private
def budget def budget

View File

@@ -15,7 +15,6 @@ module Budgets
def create def create
load_investment load_investment
load_heading load_heading
load_map
@ballot.add_investment(@investment) @ballot.add_investment(@investment)
end end
@@ -23,7 +22,6 @@ module Budgets
def destroy def destroy
@investment = @line.investment @investment = @line.investment
load_heading load_heading
load_map
@line.destroy! @line.destroy!
load_investments load_investments
@@ -69,12 +67,6 @@ module Budgets
def load_categories def load_categories
@categories = Tag.category.order(:name) @categories = Tag.category.order(:name)
end end
def load_map
@investments ||= []
@investments_map_coordinates = MapLocation.where(investment: @investments).map(&:json_data)
@map_location = MapLocation.load_from_heading(@heading)
end
end end
end end
end end

View File

@@ -1,8 +1,12 @@
$("#progress_bar").html("<%= j render("/budgets/ballot/progress_bar", ballot: @ballot, heading: @heading) %>"); $("#progress_bar").html("<%= j render("/budgets/ballot/progress_bar", ballot: @ballot, heading: @heading) %>");
$("#sidebar").html("<%= j render("/budgets/investments/sidebar") %>"); $("#my_ballot").html("<%= j render(
Budgets::Investments::MyBallotComponent.new(
ballot: @ballot,
heading: @heading,
investment_ids: @investment_ids
)
) %>");
<%= render "refresh_ballots", <%= render "refresh_ballots",
investment_ids: @investment_ids, investment_ids: @investment_ids,
ballot: @ballot %> ballot: @ballot %>
App.Map.initialize();

View File

@@ -1,9 +1,13 @@
$("#progress_bar").html("<%= j render("budgets/ballot/progress_bar", ballot: @ballot, heading: @heading) %>"); $("#progress_bar").html("<%= j render("budgets/ballot/progress_bar", ballot: @ballot, heading: @heading) %>");
$("#sidebar").html("<%= j render("budgets/investments/sidebar") %>"); $("#my_ballot").html("<%= j render(
Budgets::Investments::MyBallotComponent.new(
ballot: @ballot,
heading: @heading,
investment_ids: @investment_ids
)
) %>");
$("#ballot").html("<%= j render("budgets/ballot/ballot") %>") $("#ballot").html("<%= j render("budgets/ballot/ballot") %>")
<%= render "refresh_ballots", <%= render "refresh_ballots",
investment_ids: @investment_ids, investment_ids: @investment_ids,
ballot: @ballot %> ballot: @ballot %>
App.Map.initialize();

View File

@@ -12,14 +12,14 @@
<% end %> <% end %>
<% end %> <% end %>
<% if @heading && can?(:show, @ballot) %> <div id="my_ballot">
<%= render Budgets::Investments::MyBallotComponent.new( <%= render Budgets::Investments::MyBallotComponent.new(
ballot: @ballot, ballot: @ballot,
heading: @heading, heading: @heading,
investment_ids: @investment_ids, investment_ids: @investment_ids,
assigned_heading: @assigned_heading assigned_heading: @assigned_heading
) %> ) %>
<% end %> </div>
<%= render Budgets::Investments::ContentBlocksComponent.new(@heading) %> <%= render Budgets::Investments::ContentBlocksComponent.new(@heading) %>

View File

@@ -193,6 +193,10 @@ FactoryBot.define do
valuators { [create(:valuator)] } valuators { [create(:valuator)] }
end end
trait :with_map_location do
map_location
end
trait :flagged do trait :flagged do
after :create do |investment| after :create do |investment|
Flag.flag(create(:user), investment) Flag.flag(create(:user), investment)

View File

@@ -161,15 +161,28 @@ describe "Ballots" do
end end
scenario "map and content block shoud be visible before and after" do scenario "map and content block shoud be visible before and after" do
create(:budget_investment, :selected, heading: new_york, price: 10000, title: "More bridges") stub_const("#{Budgets::InvestmentsController}::PER_PAGE", 1)
create(:budget_investment, :selected, :with_map_location,
heading: new_york,
price: 10000,
title: "More bridges",
)
create(:budget_investment, :selected, :with_map_location,
heading: new_york,
price: 5000,
title: "Less bridges"
)
create(:heading_content_block, heading: new_york, body: "<li>New Block</li>") create(:heading_content_block, heading: new_york, body: "<li>New Block</li>")
new_york.update!(allow_custom_content: true) new_york.update!(allow_custom_content: true)
visit budget_investments_path(budget, heading_id: new_york) visit budget_investments_path(budget, heading_id: new_york, order: :price)
within("#sidebar") do within("#sidebar") do
expect(page).to have_content "OpenStreetMap" expect(page).to have_content "OpenStreetMap"
expect(page).to have_content "New Block" expect(page).to have_content "New Block"
expect(page).to have_css ".map-icon", visible: :all, count: 2
end end
add_to_ballot("More bridges") add_to_ballot("More bridges")
@@ -178,6 +191,7 @@ describe "Ballots" do
expect(page).to have_content "More bridges" expect(page).to have_content "More bridges"
expect(page).to have_content "OpenStreetMap" expect(page).to have_content "OpenStreetMap"
expect(page).to have_content "New Block" expect(page).to have_content "New Block"
expect(page).to have_css ".map-icon", visible: :all, count: 2
end end
within(".budget-investment", text: "More bridges") do within(".budget-investment", text: "More bridges") do
@@ -188,6 +202,7 @@ describe "Ballots" do
expect(page).not_to have_content "More bridges" expect(page).not_to have_content "More bridges"
expect(page).to have_content "OpenStreetMap" expect(page).to have_content "OpenStreetMap"
expect(page).to have_content "New Block" expect(page).to have_content "New Block"
expect(page).to have_css ".map-icon", visible: :all, count: 2
end end
end end
end end