add no-JS fallback for cart country selector
All checks were successful
deploy / deploy (push) Successful in 59s
All checks were successful
deploy / deploy (push) Successful in 59s
The delivery country form now has action="/cart/country" with a noscript submit button. Without JS, changing the country and clicking Update POSTs to a new CartController.update_country action that saves the country to session and redirects back to /cart. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -403,7 +403,8 @@ defmodule BerrypodWeb.ShopComponents.Cart do
|
||||
<span class="delivery-line-label">
|
||||
Delivery to
|
||||
<%= if @available_countries != [] and @mode != :preview do %>
|
||||
<form phx-change="change_country">
|
||||
<form action="/cart/country" method="post" phx-change="change_country">
|
||||
<input type="hidden" name="_csrf_token" value={Phoenix.Controller.get_csrf_token()} />
|
||||
<select
|
||||
name="country"
|
||||
class="delivery-select"
|
||||
@@ -413,6 +414,9 @@ defmodule BerrypodWeb.ShopComponents.Cart do
|
||||
<option value={code} selected={code == @country_code}>{name}</option>
|
||||
<% end %>
|
||||
</select>
|
||||
<noscript>
|
||||
<button type="submit" class="themed-button delivery-country-submit">Update</button>
|
||||
</noscript>
|
||||
</form>
|
||||
<% else %>
|
||||
<span>{Berrypod.Shipping.country_name(@country_code)}</span>
|
||||
|
||||
@@ -74,6 +74,19 @@ defmodule BerrypodWeb.CartController do
|
||||
|> redirect(to: ~p"/cart")
|
||||
end
|
||||
|
||||
@doc """
|
||||
Updates delivery country via form POST (no-JS fallback).
|
||||
"""
|
||||
def update_country(conn, %{"country" => code}) when is_binary(code) and code != "" do
|
||||
conn
|
||||
|> put_session("country_code", code)
|
||||
|> redirect(to: ~p"/cart")
|
||||
end
|
||||
|
||||
def update_country(conn, _params) do
|
||||
redirect(conn, to: ~p"/cart")
|
||||
end
|
||||
|
||||
defp parse_quantity(str) when is_binary(str) do
|
||||
case Integer.parse(str) do
|
||||
{qty, _} when qty > 0 -> qty
|
||||
|
||||
@@ -97,6 +97,7 @@ defmodule BerrypodWeb.Router do
|
||||
post "/cart/add", CartController, :add
|
||||
post "/cart/remove", CartController, :remove
|
||||
post "/cart/update", CartController, :update_item
|
||||
post "/cart/country", CartController, :update_country
|
||||
end
|
||||
|
||||
# Health check (no auth, no theme loading — for load balancers and uptime monitors)
|
||||
|
||||
Reference in New Issue
Block a user