From 13e7adf31ab6d6b33dc129f71e16167885cfba45 Mon Sep 17 00:00:00 2001 From: Keith Gautreaux Date: Tue, 19 Sep 2023 10:44:45 -0700 Subject: [PATCH 1/3] Filter out nil or empty string values from the map. --- .../live_admin/live/layout_editor_live/resource_links.ex | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/lib/beacon/live_admin/live/layout_editor_live/resource_links.ex b/lib/beacon/live_admin/live/layout_editor_live/resource_links.ex index e91ef614..5a98611c 100644 --- a/lib/beacon/live_admin/live/layout_editor_live/resource_links.ex +++ b/lib/beacon/live_admin/live/layout_editor_live/resource_links.ex @@ -185,7 +185,10 @@ defmodule Beacon.LiveAdmin.LayoutEditorLive.ResourceLinks do case Map.fetch(params, field) do {:ok, map} -> - list = Enum.sort_by(map, fn {key, _value} -> String.to_integer(key) end) + list = + Enum.filter(map, fn {_key, value} -> value != "" and value != nil end) + |> Enum.sort_by(map, fn {key, _value} -> String.to_integer(key) end) + Map.put(params, field, Keyword.values(list)) :error -> From 84276fd075153b9049eb0e2accf8f8045e92916b Mon Sep 17 00:00:00 2001 From: Keith Gautreaux Date: Wed, 20 Sep 2023 05:47:33 -0700 Subject: [PATCH 2/3] Remove nil or empty attributes from the resource_links map. --- .../live/layout_editor_live/resource_links.ex | 20 +++++++++--- .../live/layout_editor_live/edit_test.exs | 32 ++++++++++++++++++- 2 files changed, 46 insertions(+), 6 deletions(-) diff --git a/lib/beacon/live_admin/live/layout_editor_live/resource_links.ex b/lib/beacon/live_admin/live/layout_editor_live/resource_links.ex index 5a98611c..1e7c051a 100644 --- a/lib/beacon/live_admin/live/layout_editor_live/resource_links.ex +++ b/lib/beacon/live_admin/live/layout_editor_live/resource_links.ex @@ -185,11 +185,21 @@ defmodule Beacon.LiveAdmin.LayoutEditorLive.ResourceLinks do case Map.fetch(params, field) do {:ok, map} -> - list = - Enum.filter(map, fn {_key, value} -> value != "" and value != nil end) - |> Enum.sort_by(map, fn {key, _value} -> String.to_integer(key) end) - - Map.put(params, field, Keyword.values(list)) + list = + Enum.sort_by(map, &String.to_integer(elem(&1, 0))) + |> Enum.map(fn {position, map} -> + Enum.reduce(map, [], fn {key, value}, acc -> + case value do + nil -> acc + "" -> acc + _ -> [{key, value} | acc] # only add non-empty values to the list + end + end) + end) + |> List.flatten() + |> Enum.into(%{}) + + Map.put(params, field, list) :error -> params diff --git a/test/beacon/live_admin/live/layout_editor_live/edit_test.exs b/test/beacon/live_admin/live/layout_editor_live/edit_test.exs index e5e0ba9f..2df05fdb 100644 --- a/test/beacon/live_admin/live/layout_editor_live/edit_test.exs +++ b/test/beacon/live_admin/live/layout_editor_live/edit_test.exs @@ -7,7 +7,16 @@ defmodule Beacon.LiveAdmin.LayoutEditorLive.EditTest do rpc(node1(), Beacon.Repo, :delete_all, [Beacon.Content.Layout, [log: false]]) end) - [layout: layout_fixture()] + [layout: layout_fixture(), + resource_links_layout: layout_fixture(node1(), %{resource_links: [ + %{ + "crossorigin" => "", + "href" => "https://example.com", + "rel" => "preload", + "type" => "", + "as" => "" + } + ]})] end test "save changes", %{conn: conn, layout: layout} do @@ -36,4 +45,25 @@ defmodule Beacon.LiveAdmin.LayoutEditorLive.EditTest do assert html =~ "Published" end + + test "resource_links don't render nil or empty attributes", %{conn: conn, resource_links_layout: resource_links_layout} do + content = rpc(node1(), Beacon.Content, :get_layout!, [resource_links_layout.id]) + IO.inspect(content.resource_links) + + {:ok, live, _html} = live(conn, "/admin/site_a/layouts/#{resource_links_layout.id}/resource_links") + + + live + |> form("#resource-links-form") + |> render_submit(%{layout: %{resource_links: %{"1" => %{ + "crossorigin" => "", + "href" => "https://example.com", + "rel" => "preload", + "type" => "foo", + "as" => "" + }}}}) + |> element("button", "type") + + assert content.resource_links == %{} + end end From c16e850e93a2a9ffa9ca49081487b2689176d585 Mon Sep 17 00:00:00 2001 From: Keith Gautreaux Date: Wed, 20 Sep 2023 13:09:59 -0700 Subject: [PATCH 3/3] Second, simplified attempt at removing nils and empty strings from resource_links. --- .../live/layout_editor_live/resource_links.ex | 5 +- .../live/layout_editor_live/edit_test.exs | 68 +++++++++++-------- 2 files changed, 42 insertions(+), 31 deletions(-) diff --git a/lib/beacon/live_admin/live/layout_editor_live/resource_links.ex b/lib/beacon/live_admin/live/layout_editor_live/resource_links.ex index 1e7c051a..5b58f178 100644 --- a/lib/beacon/live_admin/live/layout_editor_live/resource_links.ex +++ b/lib/beacon/live_admin/live/layout_editor_live/resource_links.ex @@ -187,12 +187,13 @@ defmodule Beacon.LiveAdmin.LayoutEditorLive.ResourceLinks do {:ok, map} -> list = Enum.sort_by(map, &String.to_integer(elem(&1, 0))) - |> Enum.map(fn {position, map} -> + |> Enum.map(fn {_position, map} -> Enum.reduce(map, [], fn {key, value}, acc -> case value do nil -> acc "" -> acc - _ -> [{key, value} | acc] # only add non-empty values to the list + # only add non-empty values to the list + _ -> [{key, value} | acc] end end) end) diff --git a/test/beacon/live_admin/live/layout_editor_live/edit_test.exs b/test/beacon/live_admin/live/layout_editor_live/edit_test.exs index 2df05fdb..3325aa8a 100644 --- a/test/beacon/live_admin/live/layout_editor_live/edit_test.exs +++ b/test/beacon/live_admin/live/layout_editor_live/edit_test.exs @@ -7,16 +7,21 @@ defmodule Beacon.LiveAdmin.LayoutEditorLive.EditTest do rpc(node1(), Beacon.Repo, :delete_all, [Beacon.Content.Layout, [log: false]]) end) - [layout: layout_fixture(), - resource_links_layout: layout_fixture(node1(), %{resource_links: [ - %{ - "crossorigin" => "", - "href" => "https://example.com", - "rel" => "preload", - "type" => "", - "as" => "" - } - ]})] + [ + layout: layout_fixture(), + resource_links_layout: + layout_fixture(node1(), %{ + resource_links: [ + %{ + "crossorigin" => "", + "href" => "https://example.com", + "rel" => "preload", + "type" => "", + "as" => "" + } + ] + }) + ] end test "save changes", %{conn: conn, layout: layout} do @@ -46,24 +51,29 @@ defmodule Beacon.LiveAdmin.LayoutEditorLive.EditTest do assert html =~ "Published" end - test "resource_links don't render nil or empty attributes", %{conn: conn, resource_links_layout: resource_links_layout} do - content = rpc(node1(), Beacon.Content, :get_layout!, [resource_links_layout.id]) - IO.inspect(content.resource_links) - - {:ok, live, _html} = live(conn, "/admin/site_a/layouts/#{resource_links_layout.id}/resource_links") - - - live - |> form("#resource-links-form") - |> render_submit(%{layout: %{resource_links: %{"1" => %{ - "crossorigin" => "", - "href" => "https://example.com", - "rel" => "preload", - "type" => "foo", - "as" => "" - }}}}) - |> element("button", "type") - - assert content.resource_links == %{} + test "simple remove nils from resource_links", %{ + conn: conn, + resource_links_layout: resource_links_layout + } do + map = + Beacon.LiveAdmin.LayoutEditorLive.ResourceLinks.coerce_resource_link_params(%{ + "resource_links" => %{ + "0" => %{ + "crossorigin" => nil, + "href" => "https://example.com", + "rel" => "preload", + "type" => "foo", + "as" => "" + } + } + }) + + assert map == %{ + "resource_links" => %{ + "href" => "https://example.com", + "rel" => "preload", + "type" => "foo" + } + } end end