Skip to content
Permalink
Browse files

Request confirmation to exit budgets component (#5765)

* Warn when trying to leave budgets component before completing the vote

* Add test for the alert before exiting the budgets component

* fix: base URL used to detect component URLs was including locale

* Component base URL based on current request instead of EngineRouter

It was working OK when using EngineRouter, but it was very complicated to get the tests passing. EngineRouter uses organization's host attribute, that can't store a port in tests, while the browser adds the host and port to the link's `href` attribute. On the other hand, Capybara uses random ports for the tested application, and #2187 adds a `base` tag to the pages to improve the debugging of failed tests HTML screenshots.

* docs: changelog entry added
  • Loading branch information
leio10 committed Mar 18, 2020
1 parent 3e8640e commit 928284518114d6435b60f0fea1de4468e5df3d8a
@@ -21,6 +21,7 @@ After this, `Decidim::Proposals::ProposalEndorsement` and the corresponding coun
- **decidim-proposals**, **decidim-core**, **decidim-blogs**: Extract proposals' endorsements into a polymorphic concern that can now be applied no any resource. It has, in turn, been aplied to blog posts. [\#5542](https://github.com/decidim/decidim/pull/5542)
- **decidim-proposals**, **decidim-core**, **decidim-blogs**: Apply generalized endorsements to the GraphQL API and add it to the blog posts query. [\#5847](https://github.com/decidim/decidim/pull/5847)
- **decidim-budgets**: Allow projects to be sorted by different criteria [\#5808](https://github.com/decidim/decidim/pull/5808)
- **decidim-budgets**: Request confirmation to exit budgets component [\#5765](https://github.com/decidim/decidim/pull/5765)

### Changed

@@ -5,7 +5,7 @@ $(() => {
const $projects = $("#projects, #project");
const $budgetSummaryTotal = $(".budget-summary__total");
const $budgetExceedModal = $("#budget-excess");

const $budgetSummary = $(".budget-summary__progressbox");
const totalBudget = parseInt($budgetSummaryTotal.attr("data-total-budget"), 10);

const cancelEvent = (event) => {
@@ -14,7 +14,7 @@ $(() => {
};

$projects.on("click", ".budget--list__action", (event) => {
const currentBudget = parseInt($(".budget-summary__progressbox").attr("data-current-budget"), 10);
const currentBudget = parseInt($budgetSummary.attr("data-current-budget"), 10);
const $currentTarget = $(event.currentTarget);
const projectBudget = parseInt($currentTarget.attr("data-budget"), 10);

@@ -26,4 +26,26 @@ $(() => {
cancelEvent(event);
}
});

if ($("#order-progress [data-toggle=budget-confirm]").length > 0) {
const safeUrl = $(".budget-summary").attr("data-safe-url");
$(document).on("click", "a", (event) => {
window.exitUrl = event.currentTarget.href;
});
$(document).on("submit", "form", (event) => {
window.exitUrl = event.currentTarget.action;
});

window.onbeforeunload = () => {
const currentBudget = parseInt($budgetSummary.attr("data-current-budget"), 10);
const exitUrl = window.exitUrl;
window.exitUrl = null;

if (currentBudget === 0 || (exitUrl && exitUrl.startsWith(safeUrl))) {
return null;
}

return "";
}
}
});
@@ -23,6 +23,10 @@ def current_order_budget_percent
def current_order_can_be_checked_out?
current_order&.can_checkout?
end

def projects_base_url
URI.parse(root_url).tap { |uri| uri.query = uri.fragment = nil } .to_s
end
end
end
end
@@ -1,4 +1,4 @@
<div class="card budget-summary" data-progress-reference>
<div class="card budget-summary" data-progress-reference data-safe-url="<%= projects_base_url %>">
<div class="card__content">
<% if include_heading %>
<% if current_order_checked_out? %>
@@ -0,0 +1,23 @@
# frozen_string_literal: true

require "spec_helper"

module Decidim
module Budgets
describe ProjectsHelper do
describe "#projects_base_url" do
subject { helper.projects_base_url }

before { allow(helper).to receive(:root_url).and_return("http://localhost:3000/processes/similique-odit/f/13/") }

it { is_expected.to eq("http://localhost:3000/processes/similique-odit/f/13/") }

context "when query string is present" do
before { allow(helper).to receive(:root_url).and_return("http://localhost:3000/processes/similique-odit/f/13/?locale=es&extra=1") }

it { is_expected.to eq("http://localhost:3000/processes/similique-odit/f/13/") }
end
end
end
end
end
@@ -110,6 +110,18 @@
expect(page).to have_no_selector ".budget-list__data--added"
end

it "is alerted when trying to leave the component before completing" do
visit_component

expect(page).to have_content "ASSIGNED: €25,000,000"

dismiss_confirm do
page.find(".logo-wrapper a").click
end

expect(page).to have_current_path main_component_path(component)
end

context "and try to vote a project that exceed the total budget" do
let!(:expensive_project) { create(:project, component: component, budget: 250_000_000) }

@@ -181,6 +193,16 @@
expect(page).to have_no_selector(".cancel-order")
end
end

it "is not alerted when trying to leave the component" do
visit_component

expect(page).to have_content("Budget vote completed")

page.find(".logo-wrapper a").click

expect(page).to have_current_path decidim.root_path
end
end

context "and votes are disabled" do

0 comments on commit 9282845

Please sign in to comment.
You can’t perform that action at this time.