Replace support investment link with a button

Using links combined with JavaScript to generate POST requests to the
browser has a few issues.

An obvious one is that it doesn't work for users without JavaScript
enabled (which lately I've noticed are more common than I thought, even
though I've been one of them for years). These users will reach a 404
page.

Since CONSUL isn't currently designed to work without JavaScript
enabled, let's ignore this one for now.

A not so obvious issue is screen reader users might expect the link to
take them somewhere instead of performing an action (in this case,
sending a form to the server).

There might be more issues I'm unaware of. Quoting DHH [1]:

"Turning ahrefs into POSTs is a bit of an anti-pattern, especially for
a11y reasons. Better to use button_to with a styling."

So we're using a button instead. This way we can also simplify the code
and make the button disabled for unidentified users, which automatically
makes it impossible to focus it using the keyboard.

A possible disadvantage of using `button_to` is it will create a form
tag which will be announced to screen readers as a form landmark. I've
tested it with my screen reader and everything worked fine for me, but
some screen reader users might interact with these forms in a different
way and find it confusing, particularly in the case where the button is
disabled.

With this change, we're only changing links for buttons in one place.
There are other places where we should do similar changes.

[1] See issue 33 in https://github.com/hotwired/turbo-rails/
This commit is contained in:
Javi Martín
2021-06-12 19:24:59 +02:00
parent 0b287e4a78
commit bb958daf05
6 changed files with 50 additions and 40 deletions

View File

@@ -1113,7 +1113,9 @@ describe "Budget Investments" do
visit budget_investments_path(budget, heading_id: carabanchel.id)
within(".budget-investment", text: "In Carabanchel") do
expect(page).to have_css(".in-favor a[data-confirm]")
expect(page).to have_button count: 1
expect(page).to have_button "Support"
expect(page).to have_css "[type='submit'][data-confirm]"
end
end
@@ -1129,8 +1131,8 @@ describe "Budget Investments" do
visit budget_investments_path(budget, heading_id: carabanchel.id)
within(".budget-investment", text: "Unsupported in Carabanchel") do
expect(page).to have_link "Support"
expect(page).not_to have_css(".in-favor a[data-confirm]")
expect(page).to have_button "Support"
expect(page).not_to have_css "[data-confirm]"
end
end
@@ -1148,7 +1150,9 @@ describe "Budget Investments" do
visit budget_investments_path(budget, heading_id: another_heading1.id)
within(".budget-investment", text: "Another investment") do
expect(page).to have_css(".in-favor a[data-confirm]")
expect(page).to have_button count: 1
expect(page).to have_button "Support"
expect(page).to have_css "[type='submit'][data-confirm]"
end
end
@@ -1159,7 +1163,8 @@ describe "Budget Investments" do
visit budget_investments_path(budget, heading_id: heading.id)
within("#budget_investment_#{all_city_investment.id}") do
expect(page).not_to have_css(".in-favor a[data-confirm]")
expect(page).to have_button "Support"
expect(page).not_to have_css "[data-confirm]"
end
end
end