Stage 4 of the page builder: all shop pages now render via PageRenderer instead of inline templates or PageTemplates. - Collection: full filter bar moved to renderer (category pills, sort dropdown, CollectionFilters hook, empty state) - PDP: related_products and reviews loaded via block data loaders instead of manual queries - Cart: page definition loaded in mount, subtotal computed in render - Search: page definition loaded in mount, handle_params unchanged - Added Phoenix.VerifiedRoutes to PageRenderer for ~p sigil - Net -55 lines (128 added, 183 removed) Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
94 lines
2.6 KiB
Elixir
94 lines
2.6 KiB
Elixir
defmodule BerrypodWeb.Shop.Collection do
|
|
use BerrypodWeb, :live_view
|
|
|
|
alias Berrypod.{Pages, Products}
|
|
|
|
@sort_options [
|
|
{"featured", "Featured"},
|
|
{"newest", "Newest"},
|
|
{"price_asc", "Price: Low to High"},
|
|
{"price_desc", "Price: High to Low"},
|
|
{"name_asc", "Name: A-Z"},
|
|
{"name_desc", "Name: Z-A"}
|
|
]
|
|
|
|
@impl true
|
|
def mount(_params, _session, socket) do
|
|
page = Pages.get_page("collection")
|
|
|
|
socket =
|
|
socket
|
|
|> assign(:page, page)
|
|
|> assign(:sort_options, @sort_options)
|
|
|> assign(:current_sort, "featured")
|
|
|
|
{:ok, socket}
|
|
end
|
|
|
|
@impl true
|
|
def handle_params(%{"slug" => slug} = params, _uri, socket) do
|
|
sort = params["sort"] || "featured"
|
|
|
|
case load_collection(slug, sort) do
|
|
{:ok, title, category, products} ->
|
|
{:noreply,
|
|
socket
|
|
|> assign(:page_title, title)
|
|
|> assign(:page_description, collection_description(title))
|
|
|> assign(:og_url, BerrypodWeb.Endpoint.url() <> "/collections/#{slug}")
|
|
|> assign(:collection_title, title)
|
|
|> assign(:current_category, category)
|
|
|> assign(:current_sort, sort)
|
|
|> assign(:products, products)}
|
|
|
|
:not_found ->
|
|
{:noreply,
|
|
socket
|
|
|> put_flash(:error, "Collection not found")
|
|
|> push_navigate(to: ~p"/collections/all")}
|
|
end
|
|
end
|
|
|
|
defp load_collection("all", sort) do
|
|
{:ok, "All Products", nil, Products.list_visible_products(sort: sort)}
|
|
end
|
|
|
|
defp load_collection("sale", sort) do
|
|
{:ok, "Sale", :sale, Products.list_visible_products(on_sale: true, sort: sort)}
|
|
end
|
|
|
|
defp load_collection(slug, sort) do
|
|
case Enum.find(Products.list_categories(), &(&1.slug == slug)) do
|
|
nil ->
|
|
:not_found
|
|
|
|
category ->
|
|
products = Products.list_visible_products(category: category.name, sort: sort)
|
|
{:ok, category.name, category, products}
|
|
end
|
|
end
|
|
|
|
@impl true
|
|
def handle_event("sort_changed", %{"sort" => sort}, socket) do
|
|
slug =
|
|
case socket.assigns.current_category do
|
|
nil -> "all"
|
|
:sale -> "sale"
|
|
category -> category.slug
|
|
end
|
|
|
|
{:noreply, push_patch(socket, to: ~p"/collections/#{slug}?sort=#{sort}")}
|
|
end
|
|
|
|
defp collection_description("All Products"), do: "Browse our full range of products."
|
|
defp collection_description("Sale"), do: "Browse our current sale items."
|
|
defp collection_description(title), do: "Browse our #{String.downcase(title)} collection."
|
|
|
|
@impl true
|
|
def render(assigns) do
|
|
~H"""
|
|
<BerrypodWeb.PageRenderer.render_page {assigns} />
|
|
"""
|
|
end
|
|
end
|