From f45ef68277ee59d2e6ca85cb6333d2e09a9eab12 Mon Sep 17 00:00:00 2001 From: Eduardo Borsa Date: Fri, 26 Jan 2024 12:52:37 -0300 Subject: [PATCH] Feat: Page Builder Featured Pages Component (#389) * Feat: Page-Builder-Featured-Pages-Component Signed-off-by: Eduardo Borsa * published at Signed-off-by: Eduardo Borsa * Generic inner block Signed-off-by: Eduardo Borsa * Update components.ex --------- Signed-off-by: Eduardo Borsa --- lib/beacon_web/components/components.ex | 92 +++++++++++++++++++ .../beacon_web/components/components_test.exs | 41 ++++++++- 2 files changed, 132 insertions(+), 1 deletion(-) diff --git a/lib/beacon_web/components/components.ex b/lib/beacon_web/components/components.ex index 490115d4..57258b99 100644 --- a/lib/beacon_web/components/components.ex +++ b/lib/beacon_web/components/components.ex @@ -52,6 +52,98 @@ defmodule BeaconWeb.Components do """ end + @doc """ + Renders the default item for featured_pages. + + ## Examples + + + """ + + attr :title, :string, required: true + attr :updated_at, :any, required: true + attr :page_path, :string, required: true + + def feature_page_item(assigns) do + ~H""" +
+
+
+

+

+ <%= Calendar.strftime(@updated_at, "%d %B %Y") %> +

+
+ + +
+
+ """ + end + + @doc """ + + Renders a feature pages component. + + ## Examples + + Without pages, A.K.A, default behavior: + + + With pages: + +
+ <%= page.title %> +
+
+ """ + + attr :pages, :list, default: [] + slot :inner_block + + def featured_pages(assigns) do + assigns = + if Enum.empty?(assigns.pages), + do: Map.put(assigns, :pages, Beacon.Content.list_pages(Process.get(:__beacon_site__), per_page: 3)), + else: assigns + + ~H""" +
+
+

+ Our Featured Blog Posts +

+

+ Recommended Reading +

+
+ +
+ <%= if Enum.empty?(@inner_block) do %> +
+ +
+ <% else %> + <%= for page <- @pages do %> + <%= render_slot(@inner_block, page) %> + <% end %> + <% end %> +
+
+ """ + end + @doc """ Renders a image previously uploaded in Admin Media Library with srcset. diff --git a/test/beacon_web/components/components_test.exs b/test/beacon_web/components/components_test.exs index cfff0e84..30ffaf70 100644 --- a/test/beacon_web/components/components_test.exs +++ b/test/beacon_web/components/components_test.exs @@ -10,7 +10,7 @@ defmodule BeaconWeb.ComponentsTest do :ok end - describe "image" do + describe "reading_time/1" do setup context do create_page_with_component("""
@@ -31,6 +31,45 @@ defmodule BeaconWeb.ComponentsTest do end end + describe "featured_pages/1 default" do + setup context do + create_page_with_component(""" +
+ +
+ """) + + context + end + + test "SUCCESS: reading_time should show 1 min to read the page", %{conn: conn} do + {:ok, view, _html} = live(conn, "/home") + + assert render(view) =~ "href=\"home\"" + end + end + + describe "featured_pages/1 with Inner Block" do + setup context do + create_page_with_component(""" +
+ + FOO BAR + +
+ """) + + context + end + + test "SUCCESS: reading_time should show 1 min to read the page", %{conn: conn} do + {:ok, view, _html} = live(conn, "/home") + + refute render(view) =~ "href=\"home\"" + assert render(view) =~ "FOO BAR" + end + end + defp create_page_with_component(template) do layout = published_layout_fixture()