Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove empty keys from Layout Resource Links #77

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions lib/beacon/live_admin/live/layout_editor_live/resource_links.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
42 changes: 41 additions & 1 deletion test/beacon/live_admin/live/layout_editor_live/edit_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,21 @@
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
Expand Down Expand Up @@ -36,4 +50,30 @@

assert html =~ "Published"
end

test "simple remove nils from resource_links", %{
conn: conn,

Check warning on line 55 in test/beacon/live_admin/live/layout_editor_live/edit_test.exs

View workflow job for this annotation

GitHub Actions / test (OTP 23 | Elixir 1.13.0)

variable "conn" is unused (if the variable is not meant to be used, prefix it with an underscore)

Check warning on line 55 in test/beacon/live_admin/live/layout_editor_live/edit_test.exs

View workflow job for this annotation

GitHub Actions / test (OTP 26 | Elixir 1.15.1)

variable "conn" is unused (if the variable is not meant to be used, prefix it with an underscore)
resource_links_layout: resource_links_layout

Check warning on line 56 in test/beacon/live_admin/live/layout_editor_live/edit_test.exs

View workflow job for this annotation

GitHub Actions / test (OTP 23 | Elixir 1.13.0)

variable "resource_links_layout" is unused (if the variable is not meant to be used, prefix it with an underscore)

Check warning on line 56 in test/beacon/live_admin/live/layout_editor_live/edit_test.exs

View workflow job for this annotation

GitHub Actions / test (OTP 26 | Elixir 1.15.1)

variable "resource_links_layout" is unused (if the variable is not meant to be used, prefix it with an underscore)
} 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