From 2255dfbf6e685ed5acd6a6d970b0175040db53b5 Mon Sep 17 00:00:00 2001 From: Moritz Date: Thu, 17 Jul 2025 17:43:07 +0200 Subject: [PATCH] feat: migration to phoenix 1.8 - fix formatting --- lib/mv_web.ex | 2 +- lib/mv_web/live/property_live/form.ex | 64 +++++++++++++------------- test/mv_web/member_live/index_test.exs | 8 ++-- 3 files changed, 37 insertions(+), 37 deletions(-) diff --git a/lib/mv_web.ex b/lib/mv_web.ex index f743377..46e4e8b 100644 --- a/lib/mv_web.ex +++ b/lib/mv_web.ex @@ -51,7 +51,7 @@ defmodule MvWeb do def live_view do quote do use Phoenix.LiveView - + on_mount MvWeb.LiveHelpers unquote(html_helpers()) diff --git a/lib/mv_web/live/property_live/form.ex b/lib/mv_web/live/property_live/form.ex index 3535987..42814a3 100644 --- a/lib/mv_web/live/property_live/form.ex +++ b/lib/mv_web/live/property_live/form.ex @@ -12,29 +12,26 @@ defmodule MvWeb.PropertyLive.Form do <.form for={@form} id="property-form" phx-change="validate" phx-submit="save"> - <.input - field={@form[:property_type_id]} - type="select" + <.input + field={@form[:property_type_id]} + type="select" label={gettext("Property type")} options={property_type_options(@property_types)} prompt={gettext("Choose a property type")} /> - - <.input - field={@form[:member_id]} - type="select" + + <.input + field={@form[:member_id]} + type="select" label={gettext("Member")} options={member_options(@members)} prompt={gettext("Choose a member")} /> - - + + <%= if @selected_property_type do %> - <.union_value_input - form={@form} - property_type={@selected_property_type} - /> + <.union_value_input form={@form} property_type={@selected_property_type} /> <% else %>
{gettext("Please select a property type first")} @@ -55,44 +52,44 @@ defmodule MvWeb.PropertyLive.Form do # Extract the current value from the Property current_value = extract_current_value(assigns.form.data, assigns.property_type.value_type) assigns = assign(assigns, :current_value, current_value) - + ~H"""
- + <%= case @property_type.value_type do %> <% :string -> %> <.inputs_for :let={value_form} field={@form[:value]}> <.input field={value_form[:value]} type="text" label="" value={@current_value} /> - <% :integer -> %> <.inputs_for :let={value_form} field={@form[:value]}> <.input field={value_form[:value]} type="number" label="" value={@current_value} /> - <% :boolean -> %> <.inputs_for :let={value_form} field={@form[:value]}> <.input field={value_form[:value]} type="checkbox" label="" checked={@current_value} /> - <% :date -> %> <.inputs_for :let={value_form} field={@form[:value]}> - <.input field={value_form[:value]} type="date" label="" value={format_date_value(@current_value)} /> + <.input + field={value_form[:value]} + type="date" + label="" + value={format_date_value(@current_value)} + /> - <% :email -> %> <.inputs_for :let={value_form} field={@form[:value]}> <.input field={value_form[:value]} type="email" label="" value={@current_value} /> - <% _ -> %>
{gettext("Unsupported value type: %{type}", type: @property_type.value_type)} @@ -103,10 +100,13 @@ defmodule MvWeb.PropertyLive.Form do end # Helper function to extract the current value from the Property - defp extract_current_value(%Mv.Membership.Property{value: %Ash.Union{value: value}}, _value_type) do + defp extract_current_value( + %Mv.Membership.Property{value: %Ash.Union{value: value}}, + _value_type + ) do value end - + defp extract_current_value(_data, _value_type) do nil end @@ -115,16 +115,16 @@ defmodule MvWeb.PropertyLive.Form do defp format_date_value(%Date{} = date) do Date.to_iso8601(date) end - + defp format_date_value(nil), do: "" - + defp format_date_value(date) when is_binary(date) do case Date.from_iso8601(date) do {:ok, parsed_date} -> Date.to_iso8601(parsed_date) _ -> "" end end - + defp format_date_value(_), do: "" @impl true @@ -159,7 +159,7 @@ defmodule MvWeb.PropertyLive.Form do @impl true def handle_event("validate", %{"property" => property_params}, socket) do # Find the selected PropertyType - selected_property_type = + selected_property_type = case property_params["property_type_id"] do "" -> nil nil -> nil @@ -167,7 +167,7 @@ defmodule MvWeb.PropertyLive.Form do end # Set the Union type based on the selected PropertyType - updated_params = + updated_params = if selected_property_type do union_type = to_string(selected_property_type.value_type) put_in(property_params, ["value", "_union_type"], union_type) @@ -183,7 +183,7 @@ defmodule MvWeb.PropertyLive.Form do def handle_event("save", %{"property" => property_params}, socket) do # Set the Union type based on the selected PropertyType - updated_params = + updated_params = if socket.assigns.selected_property_type do union_type = to_string(socket.assigns.selected_property_type.value_type) put_in(property_params, ["value", "_union_type"], union_type) @@ -221,14 +221,14 @@ defmodule MvWeb.PropertyLive.Form do if property do # Determine the Union type based on the property_type union_type = property.property_type && property.property_type.value_type - - params = + + params = if union_type do %{"value" => %{"_union_type" => to_string(union_type)}} else %{} end - + AshPhoenix.Form.for_update(property, :update, as: "property", params: params) else AshPhoenix.Form.for_create(Mv.Membership.Property, :create, as: "property") diff --git a/test/mv_web/member_live/index_test.exs b/test/mv_web/member_live/index_test.exs index 75d7efb..e3e77dc 100644 --- a/test/mv_web/member_live/index_test.exs +++ b/test/mv_web/member_live/index_test.exs @@ -44,12 +44,12 @@ defmodule MvWeb.MemberLive.IndexTest do } # Submit form and follow the redirect to get the flash message - {:ok, index_view, _html} = + {:ok, index_view, _html} = form_view |> form("#member-form", form_data) |> render_submit() |> follow_redirect(conn, "/members") - + assert has_element?(index_view, "#flash-group", "Mitglied erstellt erfolgreich") end @@ -65,12 +65,12 @@ defmodule MvWeb.MemberLive.IndexTest do } # Submit form and follow the redirect to get the flash message - {:ok, index_view, _html} = + {:ok, index_view, _html} = form_view |> form("#member-form", form_data) |> render_submit() |> follow_redirect(conn, "/members") - + assert has_element?(index_view, "#flash-group", "Member create successfully") end end