From 4a6e7cf51a58cf5d03e18c8c57e948a617b68d8c Mon Sep 17 00:00:00 2001 From: carla Date: Wed, 7 Jan 2026 18:11:07 +0100 Subject: [PATCH] feat: show only edit or list view in settings --- .../live/custom_field_live/index_component.ex | 20 +++++++++++++++++++ lib/mv_web/live/global_settings_live.ex | 13 +++++++++++- 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/lib/mv_web/live/custom_field_live/index_component.ex b/lib/mv_web/live/custom_field_live/index_component.ex index ee8e573..a11cc57 100644 --- a/lib/mv_web/live/custom_field_live/index_component.ex +++ b/lib/mv_web/live/custom_field_live/index_component.ex @@ -178,6 +178,9 @@ defmodule MvWeb.CustomFieldLive.IndexComponent do @impl true def update(assigns, socket) do + # Track previous show_form state to detect when form is closed + previous_show_form = Map.get(socket.assigns, :show_form, false) + # If show_form is explicitly provided in assigns, reset editing state socket = if Map.has_key?(assigns, :show_form) and assigns.show_form == false do @@ -188,6 +191,13 @@ defmodule MvWeb.CustomFieldLive.IndexComponent do socket end + # Detect when form is closed (show_form changes from true to false) + new_show_form = Map.get(assigns, :show_form, false) + + if previous_show_form and not new_show_form do + send(self(), {:editing_section_changed, nil}) + end + {:ok, socket |> assign(assigns) @@ -202,6 +212,11 @@ defmodule MvWeb.CustomFieldLive.IndexComponent do @impl true def handle_event("new_custom_field", _params, socket) do + # Only send event if form was not already open + if not socket.assigns[:show_form] do + send(self(), {:editing_section_changed, :custom_fields}) + end + {:noreply, socket |> assign(:show_form, true) @@ -213,6 +228,11 @@ defmodule MvWeb.CustomFieldLive.IndexComponent do def handle_event("edit_custom_field", %{"id" => id}, socket) do custom_field = Ash.get!(Mv.Membership.CustomField, id) + # Only send event if form was not already open + if not socket.assigns[:show_form] do + send(self(), {:editing_section_changed, :custom_fields}) + end + {:noreply, socket |> assign(:show_form, true) diff --git a/lib/mv_web/live/global_settings_live.ex b/lib/mv_web/live/global_settings_live.ex index 6f7bb54..2798412 100644 --- a/lib/mv_web/live/global_settings_live.ex +++ b/lib/mv_web/live/global_settings_live.ex @@ -31,6 +31,7 @@ defmodule MvWeb.GlobalSettingsLive do socket |> assign(:page_title, gettext("Settings")) |> assign(:settings, settings) + |> assign(:active_editing_section, nil) |> assign_form()} end @@ -65,12 +66,14 @@ defmodule MvWeb.GlobalSettingsLive do <%!-- Memberdata Section --%> <.form_section title={gettext("Memberdata")}> <.live_component + :if={@active_editing_section != :custom_fields} module={MvWeb.MemberFieldLive.IndexComponent} id="member-fields-component" settings={@settings} /> <%!-- Custom Fields Section --%> <.live_component + :if={@active_editing_section != :member_fields} module={MvWeb.CustomFieldLive.IndexComponent} id="custom-fields-component" /> @@ -113,7 +116,9 @@ defmodule MvWeb.GlobalSettingsLive do ) {:noreply, - put_flash(socket, :info, gettext("Custom field %{action} successfully", action: action))} + socket + |> assign(:active_editing_section, nil) + |> put_flash(:info, gettext("Custom field %{action} successfully", action: action))} end @impl true @@ -163,6 +168,11 @@ defmodule MvWeb.GlobalSettingsLive do {:noreply, put_flash(socket, :error, error_message)} end + @impl true + def handle_info({:editing_section_changed, section}, socket) do + {:noreply, assign(socket, :active_editing_section, section)} + end + @impl true def handle_info({:member_field_saved, _member_field, action}, socket) do # Reload settings to get updated member_field_visibility @@ -178,6 +188,7 @@ defmodule MvWeb.GlobalSettingsLive do {:noreply, socket |> assign(:settings, updated_settings) + |> assign(:active_editing_section, nil) |> put_flash(:info, gettext("Member field %{action} successfully", action: action))} end