2026-03-09 14:47:50 +00:00
|
|
|
defmodule BerrypodWeb.Shop.Pages.Collection do
|
|
|
|
|
@moduledoc """
|
|
|
|
|
Collection page handler for the unified Shop.Page LiveView.
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
import Phoenix.Component, only: [assign: 3]
|
|
|
|
|
import Phoenix.LiveView, only: [push_patch: 2, push_navigate: 2, put_flash: 3]
|
2026-01-19 23:26:41 +00:00
|
|
|
|
2026-03-01 09:42:34 +00:00
|
|
|
alias Berrypod.{Pages, Pagination, 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-03-09 14:47:50 +00:00
|
|
|
def init(socket, _params, _uri) 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
|
|
|
|
2026-03-09 14:47:50 +00:00
|
|
|
{:noreply, socket}
|
2026-01-19 23:26:41 +00:00
|
|
|
end
|
|
|
|
|
|
2026-01-19 23:38:22 +00:00
|
|
|
def handle_params(%{"slug" => slug} = params, _uri, socket) do
|
|
|
|
|
sort = params["sort"] || "featured"
|
2026-03-01 09:42:34 +00:00
|
|
|
page_num = Pagination.parse_page(params)
|
2026-01-19 23:26:41 +00:00
|
|
|
|
2026-03-01 09:42:34 +00:00
|
|
|
case load_collection(slug, sort, page_num) do
|
|
|
|
|
{:ok, title, category, pagination} ->
|
2026-03-09 14:47:50 +00:00
|
|
|
socket =
|
|
|
|
|
socket
|
|
|
|
|
|> assign(:page_title, title)
|
|
|
|
|
|> assign(:page_description, collection_description(title))
|
|
|
|
|
|> assign(:og_url, BerrypodWeb.Endpoint.url() <> "/collections/#{slug}")
|
|
|
|
|
|> assign(:collection_title, title)
|
|
|
|
|
|> assign(:collection_slug, slug)
|
|
|
|
|
|> assign(:current_category, category)
|
|
|
|
|
|> assign(:current_sort, sort)
|
|
|
|
|
|> assign(:pagination, pagination)
|
|
|
|
|
|> assign(:products, pagination.items)
|
|
|
|
|
|
|
|
|
|
{:noreply, socket}
|
2026-01-19 23:38:22 +00:00
|
|
|
|
|
|
|
|
:not_found ->
|
2026-03-09 14:47:50 +00:00
|
|
|
socket =
|
|
|
|
|
socket
|
|
|
|
|
|> put_flash(:error, "Collection not found")
|
|
|
|
|
|> push_navigate(to: "/collections/all")
|
|
|
|
|
|
|
|
|
|
{:noreply, socket}
|
2026-01-19 23:38:22 +00:00
|
|
|
end
|
|
|
|
|
end
|
2026-01-19 23:26:41 +00:00
|
|
|
|
2026-03-09 14:47:50 +00:00
|
|
|
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: "/collections/#{slug}?sort=#{sort}")}
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def handle_event(_event, _params, _socket), do: :cont
|
|
|
|
|
|
2026-03-01 09:42:34 +00:00
|
|
|
defp load_collection("all", sort, page) do
|
|
|
|
|
pagination = Products.list_visible_products_paginated(sort: sort, page: page)
|
|
|
|
|
{:ok, "All Products", nil, pagination}
|
2026-01-19 23:38:22 +00:00
|
|
|
end
|
2026-01-19 23:26:41 +00:00
|
|
|
|
2026-03-01 09:42:34 +00:00
|
|
|
defp load_collection("sale", sort, page) do
|
|
|
|
|
pagination = Products.list_visible_products_paginated(on_sale: true, sort: sort, page: page)
|
|
|
|
|
{:ok, "Sale", :sale, pagination}
|
2026-02-11 08:38:54 +00:00
|
|
|
end
|
|
|
|
|
|
2026-03-01 09:42:34 +00:00
|
|
|
defp load_collection(slug, sort, page) do
|
2026-02-13 08:27:26 +00:00
|
|
|
case Enum.find(Products.list_categories(), &(&1.slug == slug)) do
|
|
|
|
|
nil ->
|
|
|
|
|
:not_found
|
|
|
|
|
|
|
|
|
|
category ->
|
2026-03-01 09:42:34 +00:00
|
|
|
pagination =
|
|
|
|
|
Products.list_visible_products_paginated(
|
|
|
|
|
category: category.name,
|
|
|
|
|
sort: sort,
|
|
|
|
|
page: page
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
{:ok, category.name, category, pagination}
|
2026-01-19 23:26:41 +00:00
|
|
|
end
|
|
|
|
|
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
|
|
|
end
|