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..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 @@ -185,8 +185,22 @@ 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) - 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 + # only add non-empty values to the list + _ -> [{key, value} | acc] + 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..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,7 +7,21 @@ 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 +50,30 @@ defmodule Beacon.LiveAdmin.LayoutEditorLive.EditTest do assert html =~ "Published" end + + 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