2026-02-18 21:23:15 +00:00
|
|
|
defmodule BerrypodWeb.Shop.Collection do
|
|
|
|
|
use BerrypodWeb, :live_view
|
2026-01-19 23:26:41 +00:00
|
|
|
|
2026-02-26 19:13:00 +00:00
|
|
|
alias Berrypod.{Pages, Products}
|
2026-01-19 23:26:41 +00:00
|
|
|
|
2026-01-19 23:38:22 +00:00
|
|
|
@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"}
|
|
|
|
|
]
|
|
|
|
|
|
2026-01-19 23:26:41 +00:00
|
|
|
@impl true
|
|
|
|
|
def mount(_params, _session, socket) do
|
2026-02-26 19:13:00 +00:00
|
|
|
page = Pages.get_page("collection")
|
|
|
|
|
|
2026-01-19 23:26:41 +00:00
|
|
|
socket =
|
|
|
|
|
socket
|
2026-02-26 19:13:00 +00:00
|
|
|
|> assign(:page, page)
|
2026-01-19 23:38:22 +00:00
|
|
|
|> assign(:sort_options, @sort_options)
|
|
|
|
|
|> assign(:current_sort, "featured")
|
2026-01-19 23:26:41 +00:00
|
|
|
|
|
|
|
|
{:ok, socket}
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
@impl true
|
2026-01-19 23:38:22 +00:00
|
|
|
def handle_params(%{"slug" => slug} = params, _uri, socket) do
|
|
|
|
|
sort = params["sort"] || "featured"
|
2026-01-19 23:26:41 +00:00
|
|
|
|
2026-02-13 08:27:26 +00:00
|
|
|
case load_collection(slug, sort) do
|
2026-01-19 23:38:22 +00:00
|
|
|
{:ok, title, category, products} ->
|
|
|
|
|
{:noreply,
|
|
|
|
|
socket
|
|
|
|
|
|> assign(:page_title, title)
|
2026-02-23 21:31:35 +00:00
|
|
|
|> assign(:page_description, collection_description(title))
|
add canonical URLs, robots.txt, and sitemap.xml
Canonical: all shop pages now assign og_url (reusing the existing og:url
assign), which the layout renders as <link rel="canonical">. Collection
pages strip the sort param so ?sort=price_asc doesn't create a duplicate
canonical.
robots.txt: dynamic controller disallows /admin/, /api/, /users/,
/webhooks/, /checkout/. Removed robots.txt from static_paths so it
goes through the router instead of Plug.Static.
sitemap.xml: auto-generated from all visible products + categories +
static pages, served as application/xml. 8 tests.
Also updates PROGRESS.md: marks tasks 55, 58, 59, 61, 62 as done.
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-02-23 21:47:35 +00:00
|
|
|
|> assign(:og_url, BerrypodWeb.Endpoint.url() <> "/collections/#{slug}")
|
2026-01-19 23:38:22 +00:00
|
|
|
|> assign(:collection_title, title)
|
|
|
|
|
|> assign(:current_category, category)
|
|
|
|
|
|> assign(:current_sort, sort)
|
2026-02-13 08:27:26 +00:00
|
|
|
|> assign(:products, products)}
|
2026-01-19 23:38:22 +00:00
|
|
|
|
|
|
|
|
:not_found ->
|
2026-01-19 23:26:41 +00:00
|
|
|
{:noreply,
|
|
|
|
|
socket
|
|
|
|
|
|> put_flash(:error, "Collection not found")
|
|
|
|
|
|> push_navigate(to: ~p"/collections/all")}
|
2026-01-19 23:38:22 +00:00
|
|
|
end
|
|
|
|
|
end
|
2026-01-19 23:26:41 +00:00
|
|
|
|
2026-02-13 08:27:26 +00:00
|
|
|
defp load_collection("all", sort) do
|
|
|
|
|
{:ok, "All Products", nil, Products.list_visible_products(sort: sort)}
|
2026-01-19 23:38:22 +00:00
|
|
|
end
|
2026-01-19 23:26:41 +00:00
|
|
|
|
2026-02-13 08:27:26 +00:00
|
|
|
defp load_collection("sale", sort) do
|
|
|
|
|
{:ok, "Sale", :sale, Products.list_visible_products(on_sale: true, sort: sort)}
|
2026-02-11 08:38:54 +00:00
|
|
|
end
|
|
|
|
|
|
2026-02-13 08:27:26 +00:00
|
|
|
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}
|
2026-01-19 23:26:41 +00:00
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2026-01-19 23:38:22 +00:00
|
|
|
@impl true
|
|
|
|
|
def handle_event("sort_changed", %{"sort" => sort}, socket) do
|
2026-01-31 14:24:58 +00:00
|
|
|
slug =
|
2026-02-11 08:38:54 +00:00
|
|
|
case socket.assigns.current_category do
|
|
|
|
|
nil -> "all"
|
|
|
|
|
:sale -> "sale"
|
|
|
|
|
category -> category.slug
|
|
|
|
|
end
|
2026-01-31 14:24:58 +00:00
|
|
|
|
2026-01-19 23:38:22 +00:00
|
|
|
{:noreply, push_patch(socket, to: ~p"/collections/#{slug}?sort=#{sort}")}
|
|
|
|
|
end
|
|
|
|
|
|
2026-02-23 21:31:35 +00:00
|
|
|
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."
|
|
|
|
|
|
2026-01-19 23:26:41 +00:00
|
|
|
@impl true
|
|
|
|
|
def render(assigns) do
|
|
|
|
|
~H"""
|
2026-02-26 19:13:00 +00:00
|
|
|
<BerrypodWeb.PageRenderer.render_page {assigns} />
|
2026-01-19 23:26:41 +00:00
|
|
|
"""
|
|
|
|
|
end
|
|
|
|
|
end
|