refactor
This commit is contained in:
parent
4fb5b12ea7
commit
baa288bff3
11 changed files with 401 additions and 780 deletions
|
|
@ -171,11 +171,21 @@ defmodule MvWeb.CoreComponents do
|
|||
attr :phx_target, :any, required: true, doc: "The LiveView/LiveComponent target for events"
|
||||
attr :menu_class, :string, default: nil, doc: "Additional CSS classes for the menu"
|
||||
attr :menu_width, :string, default: "w-64", doc: "Width class for the menu (default: w-64)"
|
||||
attr :button_class, :string, default: nil, doc: "Additional CSS classes for the button (e.g., btn-secondary)"
|
||||
attr :menu_align, :string, default: "right", doc: "Menu alignment: 'left' or 'right' (default: right)"
|
||||
|
||||
attr :button_class, :string,
|
||||
default: nil,
|
||||
doc: "Additional CSS classes for the button (e.g., btn-secondary)"
|
||||
|
||||
attr :menu_align, :string,
|
||||
default: "right",
|
||||
doc: "Menu alignment: 'left' or 'right' (default: right)"
|
||||
|
||||
attr :testid, :string, default: "dropdown-menu", doc: "data-testid for the dropdown container"
|
||||
attr :button_testid, :string, default: "dropdown-button", doc: "data-testid for the button"
|
||||
attr :menu_testid, :string, default: nil, doc: "data-testid for the menu (defaults to testid + '-menu')"
|
||||
|
||||
attr :menu_testid, :string,
|
||||
default: nil,
|
||||
doc: "data-testid for the menu (defaults to testid + '-menu')"
|
||||
|
||||
slot :inner_block, doc: "Custom content for the dropdown menu (e.g., forms)"
|
||||
|
||||
|
|
@ -200,7 +210,14 @@ defmodule MvWeb.CoreComponents do
|
|||
aria-expanded={@open}
|
||||
aria-controls={@id}
|
||||
aria-label={@button_label}
|
||||
class={["btn", "focus:outline-none", "focus-visible:ring-2", "focus-visible:ring-offset-2", "focus-visible:ring-base-content/20", @button_class]}
|
||||
class={[
|
||||
"btn",
|
||||
"focus:outline-none",
|
||||
"focus-visible:ring-2",
|
||||
"focus-visible:ring-offset-2",
|
||||
"focus-visible:ring-base-content/20",
|
||||
@button_class
|
||||
]}
|
||||
phx-click="toggle_dropdown"
|
||||
phx-target={@phx_target}
|
||||
data-testid={@button_testid}
|
||||
|
|
|
|||
|
|
@ -29,7 +29,10 @@ defmodule MvWeb.Components.ExportDropdown do
|
|||
button_label =
|
||||
gettext("Export") <>
|
||||
" (" <>
|
||||
(if assigns.selected_count == 0, do: gettext("all"), else: to_string(assigns.selected_count)) <>
|
||||
if(assigns.selected_count == 0,
|
||||
do: gettext("all"),
|
||||
else: to_string(assigns.selected_count)
|
||||
) <>
|
||||
")"
|
||||
|
||||
assigns = assign(assigns, :button_label, button_label)
|
||||
|
|
|
|||
|
|
@ -25,50 +25,52 @@ defmodule MvWeb.MemberPdfExportController do
|
|||
def export(conn, %{"payload" => payload}) when is_binary(payload) do
|
||||
actor = current_actor(conn)
|
||||
|
||||
cond do
|
||||
is_nil(actor) ->
|
||||
forbidden(conn)
|
||||
if is_nil(actor) do
|
||||
forbidden(conn)
|
||||
else
|
||||
locale = get_locale(conn)
|
||||
club_name = get_club_name()
|
||||
|
||||
true ->
|
||||
with {:ok, decoded} <- decode_json_map(payload),
|
||||
parsed <- MemberExport.parse_params(decoded),
|
||||
{:ok, export_data} <- Build.build(actor, parsed, &label_for_column/1),
|
||||
{:ok, pdf_binary} <- MembersPDF.render(export_data) do
|
||||
filename = "members-#{Date.utc_today()}.pdf"
|
||||
with {:ok, decoded} <- decode_json_map(payload),
|
||||
parsed <- MemberExport.parse_params(decoded),
|
||||
{:ok, export_data} <- Build.build(actor, parsed, &label_for_column/1),
|
||||
{:ok, pdf_binary} <-
|
||||
MembersPDF.render(export_data, locale: locale, club_name: club_name) do
|
||||
filename = "members-#{Date.utc_today()}.pdf"
|
||||
|
||||
send_download(
|
||||
conn,
|
||||
{:binary, pdf_binary},
|
||||
filename: filename,
|
||||
content_type: "application/pdf"
|
||||
)
|
||||
else
|
||||
{:error, :invalid_json} ->
|
||||
bad_request(conn, @invalid_json_message)
|
||||
send_download(
|
||||
conn,
|
||||
{:binary, pdf_binary},
|
||||
filename: filename,
|
||||
content_type: "application/pdf"
|
||||
)
|
||||
else
|
||||
{:error, :invalid_json} ->
|
||||
bad_request(conn, @invalid_json_message)
|
||||
|
||||
{:error, :forbidden} ->
|
||||
forbidden(conn)
|
||||
{:error, :forbidden} ->
|
||||
forbidden(conn)
|
||||
|
||||
{:error, {:row_limit_exceeded, row_count, max_rows}} ->
|
||||
unprocessable_entity(conn, %{
|
||||
error: "row_limit_exceeded",
|
||||
message:
|
||||
gettext("Export contains %{count} rows, maximum is %{max}",
|
||||
count: row_count,
|
||||
max: max_rows
|
||||
),
|
||||
row_count: row_count,
|
||||
max_rows: max_rows
|
||||
})
|
||||
{:error, {:row_limit_exceeded, row_count, max_rows}} ->
|
||||
unprocessable_entity(conn, %{
|
||||
error: "row_limit_exceeded",
|
||||
message:
|
||||
gettext("Export contains %{count} rows, maximum is %{max}",
|
||||
count: row_count,
|
||||
max: max_rows
|
||||
),
|
||||
row_count: row_count,
|
||||
max_rows: max_rows
|
||||
})
|
||||
|
||||
{:error, reason} ->
|
||||
Logger.warning("PDF export failed: #{inspect(reason)}")
|
||||
{:error, reason} ->
|
||||
Logger.warning("PDF export failed: #{inspect(reason)}")
|
||||
|
||||
internal_error(conn, %{
|
||||
error: "export_failed",
|
||||
message: gettext(@export_failed_message)
|
||||
})
|
||||
end
|
||||
internal_error(conn, %{
|
||||
error: "export_failed",
|
||||
message: gettext(@export_failed_message)
|
||||
})
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
|
|
@ -122,6 +124,19 @@ defmodule MvWeb.MemberPdfExportController do
|
|||
to_string(key)
|
||||
end
|
||||
|
||||
# --- Locale and club name ---
|
||||
|
||||
defp get_locale(conn) do
|
||||
conn.assigns[:locale] || Gettext.get_locale(MvWeb.Gettext) || "en"
|
||||
end
|
||||
|
||||
defp get_club_name do
|
||||
case Mv.Membership.get_settings() do
|
||||
{:ok, settings} -> settings.club_name
|
||||
_ -> "Club"
|
||||
end
|
||||
end
|
||||
|
||||
# --- JSON responses ---
|
||||
|
||||
defp bad_request(conn, message) when is_binary(message) do
|
||||
|
|
|
|||
|
|
@ -747,45 +747,49 @@ defmodule MvWeb.MemberLive.Index do
|
|||
show_current_cycle,
|
||||
boolean_filters
|
||||
) do
|
||||
field_str =
|
||||
if is_atom(sort_field) do
|
||||
Atom.to_string(sort_field)
|
||||
else
|
||||
sort_field
|
||||
end
|
||||
base_params = build_base_params(query, sort_field, sort_order)
|
||||
base_params = add_cycle_status_filter(base_params, cycle_status_filter)
|
||||
base_params = add_show_current_cycle(base_params, show_current_cycle)
|
||||
add_boolean_filters(base_params, boolean_filters)
|
||||
end
|
||||
|
||||
order_str =
|
||||
if is_atom(sort_order) do
|
||||
Atom.to_string(sort_order)
|
||||
else
|
||||
sort_order
|
||||
end
|
||||
|
||||
base_params = %{
|
||||
"query" => query,
|
||||
"sort_field" => field_str,
|
||||
"sort_order" => order_str
|
||||
defp build_base_params(query, sort_field, sort_order) do
|
||||
%{
|
||||
"query" => query || "",
|
||||
"sort_field" => normalize_sort_field(sort_field),
|
||||
"sort_order" => normalize_sort_order(sort_order)
|
||||
}
|
||||
end
|
||||
|
||||
base_params =
|
||||
case cycle_status_filter do
|
||||
nil -> base_params
|
||||
:paid -> Map.put(base_params, "cycle_status_filter", "paid")
|
||||
:unpaid -> Map.put(base_params, "cycle_status_filter", "unpaid")
|
||||
end
|
||||
defp normalize_sort_field(nil), do: ""
|
||||
defp normalize_sort_field(field) when is_atom(field), do: Atom.to_string(field)
|
||||
defp normalize_sort_field(field) when is_binary(field), do: field
|
||||
defp normalize_sort_field(_), do: ""
|
||||
|
||||
base_params =
|
||||
if show_current_cycle do
|
||||
Map.put(base_params, "show_current_cycle", "true")
|
||||
else
|
||||
base_params
|
||||
end
|
||||
defp normalize_sort_order(nil), do: ""
|
||||
defp normalize_sort_order(order) when is_atom(order), do: Atom.to_string(order)
|
||||
defp normalize_sort_order(order) when is_binary(order), do: order
|
||||
defp normalize_sort_order(_), do: ""
|
||||
|
||||
Enum.reduce(boolean_filters, base_params, fn {custom_field_id, filter_value}, acc ->
|
||||
param_key = "#{@boolean_filter_prefix}#{custom_field_id}"
|
||||
param_value = if filter_value == true, do: "true", else: "false"
|
||||
Map.put(acc, param_key, param_value)
|
||||
end)
|
||||
defp add_cycle_status_filter(params, nil), do: params
|
||||
defp add_cycle_status_filter(params, :paid), do: Map.put(params, "cycle_status_filter", "paid")
|
||||
|
||||
defp add_cycle_status_filter(params, :unpaid),
|
||||
do: Map.put(params, "cycle_status_filter", "unpaid")
|
||||
|
||||
defp add_cycle_status_filter(params, _), do: params
|
||||
|
||||
defp add_show_current_cycle(params, true), do: Map.put(params, "show_current_cycle", "true")
|
||||
defp add_show_current_cycle(params, _), do: params
|
||||
|
||||
defp add_boolean_filters(params, boolean_filters) do
|
||||
Enum.reduce(boolean_filters, params, &add_boolean_filter/2)
|
||||
end
|
||||
|
||||
defp add_boolean_filter({custom_field_id, filter_value}, acc) do
|
||||
param_key = "#{@boolean_filter_prefix}#{custom_field_id}"
|
||||
param_value = if filter_value == true, do: "true", else: "false"
|
||||
Map.put(acc, param_key, param_value)
|
||||
end
|
||||
|
||||
# -------------------------------------------------------------
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue