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:
parent
562d7d6ab4
commit
75dc7056ae
1 changed files with 30 additions and 3 deletions
|
|
@ -44,6 +44,7 @@ defmodule MvWeb.MembershipFeeTypeLive.Form do
|
|||
field={@form[:amount]}
|
||||
label={gettext("Amount")}
|
||||
required
|
||||
phx-debounce="blur"
|
||||
/>
|
||||
|
||||
<div class="form-control">
|
||||
|
|
@ -257,11 +258,15 @@ defmodule MvWeb.MembershipFeeTypeLive.Form do
|
|||
|
||||
def handle_event("confirm_amount_change", _params, socket) do
|
||||
# Update form with pending amount and hide warning
|
||||
# Preserve all existing form values (name, description, etc.)
|
||||
form = socket.assigns.form
|
||||
existing_values = get_existing_form_values(form)
|
||||
|
||||
updated_form =
|
||||
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
|
||||
form
|
||||
end
|
||||
|
|
@ -357,7 +362,20 @@ defmodule MvWeb.MembershipFeeTypeLive.Form do
|
|||
# Checks if amount changed and updates socket assigns accordingly
|
||||
defp check_amount_change(socket, params) 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
|
||||
socket
|
||||
end
|
||||
|
|
@ -379,8 +397,17 @@ defmodule MvWeb.MembershipFeeTypeLive.Form do
|
|||
end
|
||||
|
||||
# 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
|
||||
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
|
||||
|> assign(:show_amount_warning, true)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue