refactor: fix credo warnings and format code
- Replace Enum.map/2 |> Enum.join/2 with Enum.map_join/3 for efficiency - Refactor get_existing_form_values to reduce cyclomatic complexity - Replace length/1 with Enum.empty?/1 for better performance - Update gettext translations
This commit is contained in:
parent
97c9ef670b
commit
98dc73ee37
10 changed files with 1180 additions and 159 deletions
|
|
@ -28,6 +28,7 @@ defmodule MvWeb.Helpers.MembershipFeeHelpers do
|
|||
# Use German format: comma as decimal separator, always 2 decimal places
|
||||
# Normalize to 2 decimal places
|
||||
normalized = Decimal.round(amount, 2)
|
||||
|
||||
normalized_str =
|
||||
normalized
|
||||
|> Decimal.to_string(:normal)
|
||||
|
|
|
|||
|
|
@ -248,6 +248,7 @@ defmodule MvWeb.MemberLive.Show do
|
|||
# Calculate last and current cycle status from loaded cycles
|
||||
last_cycle_status = get_last_cycle_status(member)
|
||||
current_cycle_status = get_current_cycle_status(member)
|
||||
|
||||
member =
|
||||
member
|
||||
|> Map.put(:last_cycle_status, last_cycle_status)
|
||||
|
|
|
|||
|
|
@ -39,7 +39,9 @@ defmodule MvWeb.MemberLive.Show.MembershipFeesComponent do
|
|||
</span>
|
||||
</div>
|
||||
<% else %>
|
||||
<span class="text-base-content/60 italic">{gettext("No membership fee type assigned")}</span>
|
||||
<span class="text-base-content/60 italic">
|
||||
{gettext("No membership fee type assigned")}
|
||||
</span>
|
||||
<% end %>
|
||||
</div>
|
||||
|
||||
|
|
@ -369,9 +371,7 @@ defmodule MvWeb.MemberLive.Show.MembershipFeesComponent do
|
|||
|
||||
{:error, %Ash.Error.Invalid{} = error} ->
|
||||
error_msg =
|
||||
error.errors
|
||||
|> Enum.map(fn e -> e.message end)
|
||||
|> Enum.join(", ")
|
||||
Enum.map_join(error.errors, ", ", fn e -> e.message end)
|
||||
|
||||
{:noreply,
|
||||
socket
|
||||
|
|
@ -424,7 +424,6 @@ defmodule MvWeb.MemberLive.Show.MembershipFeesComponent do
|
|||
end
|
||||
end
|
||||
|
||||
|
||||
def handle_event("edit_cycle_amount", %{"cycle_id" => cycle_id}, socket) do
|
||||
cycle = find_cycle(socket.assigns.cycles, cycle_id)
|
||||
|
||||
|
|
|
|||
|
|
@ -44,7 +44,8 @@ defmodule MvWeb.MembershipFeeSettingsLive do
|
|||
Map.put(params, "include_joining_cycle", false)
|
||||
end
|
||||
|
||||
{:noreply, assign(socket, form: AshPhoenix.Form.validate(socket.assigns.form, normalized_params))}
|
||||
{:noreply,
|
||||
assign(socket, form: AshPhoenix.Form.validate(socket.assigns.form, normalized_params))}
|
||||
end
|
||||
|
||||
def handle_event("save", %{"settings" => params}, socket) do
|
||||
|
|
|
|||
|
|
@ -324,55 +324,32 @@ defmodule MvWeb.MembershipFeeTypeLive.Form do
|
|||
defp get_existing_form_values(form) do
|
||||
# Extract values directly from form fields to get current state
|
||||
# This ensures we get the actual current values, not just initial params
|
||||
existing_values = %{}
|
||||
|
||||
existing_values =
|
||||
if form[:name] && form[:name].value do
|
||||
Map.put(existing_values, "name", to_string(form[:name].value))
|
||||
else
|
||||
existing_values
|
||||
end
|
||||
|
||||
existing_values =
|
||||
if form[:amount] && form[:amount].value do
|
||||
# Convert Decimal to string for form
|
||||
amount_str =
|
||||
case form[:amount].value do
|
||||
%Decimal{} = amount -> Decimal.to_string(amount, :normal)
|
||||
value when is_binary(value) -> value
|
||||
value -> to_string(value)
|
||||
end
|
||||
|
||||
Map.put(existing_values, "amount", amount_str)
|
||||
else
|
||||
existing_values
|
||||
end
|
||||
|
||||
existing_values =
|
||||
if form[:interval] && form[:interval].value do
|
||||
# Convert atom to string for form
|
||||
interval_str =
|
||||
case form[:interval].value do
|
||||
value when is_atom(value) -> Atom.to_string(value)
|
||||
value when is_binary(value) -> value
|
||||
value -> to_string(value)
|
||||
end
|
||||
|
||||
Map.put(existing_values, "interval", interval_str)
|
||||
else
|
||||
existing_values
|
||||
end
|
||||
|
||||
existing_values =
|
||||
if form[:description] && form[:description].value do
|
||||
Map.put(existing_values, "description", to_string(form[:description].value))
|
||||
else
|
||||
existing_values
|
||||
end
|
||||
|
||||
existing_values
|
||||
%{}
|
||||
|> extract_form_value(form, :name, &to_string/1)
|
||||
|> extract_form_value(form, :amount, &format_amount_value/1)
|
||||
|> extract_form_value(form, :interval, &format_interval_value/1)
|
||||
|> extract_form_value(form, :description, &to_string/1)
|
||||
end
|
||||
|
||||
# Helper to extract a single form field value
|
||||
defp extract_form_value(acc, form, field, formatter) do
|
||||
if form[field] && form[field].value do
|
||||
Map.put(acc, to_string(field), formatter.(form[field].value))
|
||||
else
|
||||
acc
|
||||
end
|
||||
end
|
||||
|
||||
# Formats amount value (Decimal or string) to string
|
||||
defp format_amount_value(%Decimal{} = amount), do: Decimal.to_string(amount, :normal)
|
||||
defp format_amount_value(value) when is_binary(value), do: value
|
||||
defp format_amount_value(value), do: to_string(value)
|
||||
|
||||
# Formats interval value (atom or string) to string
|
||||
defp format_interval_value(value) when is_atom(value), do: Atom.to_string(value)
|
||||
defp format_interval_value(value) when is_binary(value), do: value
|
||||
defp format_interval_value(value), do: to_string(value)
|
||||
|
||||
@spec return_path(String.t(), MembershipFeeType.t() | nil) :: String.t()
|
||||
defp return_path("index", _membership_fee_type), do: ~p"/membership_fee_types"
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue