Fix amount change warning and form value preservation

Add phx-debounce to amount input and preserve form values on confirm
This commit is contained in:
Moritz 2025-12-22 17:13:51 +01:00
parent 562d7d6ab4
commit 75dc7056ae

View file

@ -44,6 +44,7 @@ defmodule MvWeb.MembershipFeeTypeLive.Form do
field={@form[:amount]} field={@form[:amount]}
label={gettext("Amount")} label={gettext("Amount")}
required required
phx-debounce="blur"
/> />
<div class="form-control"> <div class="form-control">
@ -257,11 +258,15 @@ defmodule MvWeb.MembershipFeeTypeLive.Form do
def handle_event("confirm_amount_change", _params, socket) do def handle_event("confirm_amount_change", _params, socket) do
# Update form with pending amount and hide warning # Update form with pending amount and hide warning
# Preserve all existing form values (name, description, etc.)
form = socket.assigns.form form = socket.assigns.form
existing_values = get_existing_form_values(form)
updated_form = updated_form =
if socket.assigns.pending_amount do if socket.assigns.pending_amount do
AshPhoenix.Form.validate(form, %{"amount" => socket.assigns.pending_amount}) # Merge existing values with confirmed amount to preserve all fields
merged_params = Map.put(existing_values, "amount", socket.assigns.pending_amount)
AshPhoenix.Form.validate(form, merged_params)
else else
form form
end end
@ -357,7 +362,20 @@ defmodule MvWeb.MembershipFeeTypeLive.Form do
# Checks if amount changed and updates socket assigns accordingly # Checks if amount changed and updates socket assigns accordingly
defp check_amount_change(socket, params) do defp check_amount_change(socket, params) do
if socket.assigns.membership_fee_type && Map.has_key?(params, "amount") do if socket.assigns.membership_fee_type && Map.has_key?(params, "amount") do
handle_amount_change(socket, params["amount"], socket.assigns.membership_fee_type.amount) # Get current amount from form and new amount from params
current_form_amount = get_existing_form_values(socket.assigns.form)["amount"]
new_amount_str = params["amount"]
# Only check amount change if amount field is actually being changed in this validation
# This prevents re-triggering the warning when other fields (name, description) are edited
if current_form_amount != new_amount_str do
handle_amount_change(socket, new_amount_str, socket.assigns.membership_fee_type.amount)
else
# Amount didn't change in this validation - keep current warning state
# If warning was already confirmed (pending_amount is nil and show_amount_warning is false), keep it hidden
# If warning is shown but not confirmed, keep it shown
socket
end
else else
socket socket
end end
@ -379,8 +397,17 @@ defmodule MvWeb.MembershipFeeTypeLive.Form do
end end
# Shows amount change warning with affected member count # Shows amount change warning with affected member count
# Only calculates count if warning is being shown for the first time (false -> true)
defp show_amount_warning(socket, old_amount, new_amount, new_amount_str) do defp show_amount_warning(socket, old_amount, new_amount, new_amount_str) do
affected_count = get_affected_member_count(socket.assigns.membership_fee_type.id) # Only calculate count if warning is not already shown (optimization)
affected_count =
if socket.assigns.show_amount_warning do
# Warning already shown, reuse existing count
socket.assigns.affected_member_count
else
# Warning being shown for first time, calculate count
get_affected_member_count(socket.assigns.membership_fee_type.id)
end
socket socket
|> assign(:show_amount_warning, true) |> assign(:show_amount_warning, true)