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-18 21:23:15 +00:00
|
|
|
alias Berrypod.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
|
|
|
|
|
socket =
|
|
|
|
|
socket
|
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)
|
|
|
|
|
|> 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
|
|
|
|
|
|
|
|
|
|
defp collection_path(slug, "featured"), do: ~p"/collections/#{slug}"
|
|
|
|
|
defp collection_path(slug, sort), do: ~p"/collections/#{slug}?sort=#{sort}"
|
|
|
|
|
|
2026-01-19 23:26:41 +00:00
|
|
|
@impl true
|
|
|
|
|
def render(assigns) do
|
|
|
|
|
~H"""
|
2026-02-13 16:02:25 +00:00
|
|
|
<.shop_layout {layout_assigns(assigns)} active_page="collection">
|
2026-01-19 23:26:41 +00:00
|
|
|
<main id="main-content">
|
refactor: split shop_components.ex into 5 focused sub-modules
4,487-line monolith → 23-line facade + 5 modules:
- Base (inputs, buttons, cards)
- Layout (header, footer, mobile nav, shop_layout)
- Cart (drawer, items, order summary)
- Product (cards, gallery, variant selector, hero)
- Content (rich text, images, contact, reviews)
`use SimpleshopThemeWeb.ShopComponents` imports all sub-modules.
No single file over ~1,600 lines now.
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-08 14:30:25 +00:00
|
|
|
<.collection_header
|
2026-01-19 23:26:41 +00:00
|
|
|
title={@collection_title}
|
|
|
|
|
product_count={length(@products)}
|
|
|
|
|
/>
|
|
|
|
|
|
2026-02-17 19:07:15 +00:00
|
|
|
<div class="page-container collection-body">
|
2026-01-19 23:38:22 +00:00
|
|
|
<.collection_filter_bar
|
|
|
|
|
categories={@categories}
|
2026-02-11 08:38:54 +00:00
|
|
|
current_slug={
|
|
|
|
|
case @current_category do
|
|
|
|
|
:sale -> "sale"
|
|
|
|
|
nil -> nil
|
|
|
|
|
cat -> cat.slug
|
|
|
|
|
end
|
|
|
|
|
}
|
2026-01-19 23:38:22 +00:00
|
|
|
sort_options={@sort_options}
|
|
|
|
|
current_sort={@current_sort}
|
|
|
|
|
/>
|
2026-01-19 23:26:41 +00:00
|
|
|
|
refactor: split shop_components.ex into 5 focused sub-modules
4,487-line monolith → 23-line facade + 5 modules:
- Base (inputs, buttons, cards)
- Layout (header, footer, mobile nav, shop_layout)
- Cart (drawer, items, order summary)
- Product (cards, gallery, variant selector, hero)
- Content (rich text, images, contact, reviews)
`use SimpleshopThemeWeb.ShopComponents` imports all sub-modules.
No single file over ~1,600 lines now.
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-08 14:30:25 +00:00
|
|
|
<.product_grid theme_settings={@theme_settings}>
|
2026-01-19 23:26:41 +00:00
|
|
|
<%= for product <- @products do %>
|
refactor: split shop_components.ex into 5 focused sub-modules
4,487-line monolith → 23-line facade + 5 modules:
- Base (inputs, buttons, cards)
- Layout (header, footer, mobile nav, shop_layout)
- Cart (drawer, items, order summary)
- Product (cards, gallery, variant selector, hero)
- Content (rich text, images, contact, reviews)
`use SimpleshopThemeWeb.ShopComponents` imports all sub-modules.
No single file over ~1,600 lines now.
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-08 14:30:25 +00:00
|
|
|
<.product_card
|
2026-01-19 23:26:41 +00:00
|
|
|
product={product}
|
|
|
|
|
theme_settings={@theme_settings}
|
|
|
|
|
mode={@mode}
|
|
|
|
|
variant={:default}
|
2026-02-11 08:38:54 +00:00
|
|
|
show_category={@current_category in [nil, :sale]}
|
2026-01-19 23:26:41 +00:00
|
|
|
/>
|
|
|
|
|
<% end %>
|
refactor: split shop_components.ex into 5 focused sub-modules
4,487-line monolith → 23-line facade + 5 modules:
- Base (inputs, buttons, cards)
- Layout (header, footer, mobile nav, shop_layout)
- Cart (drawer, items, order summary)
- Product (cards, gallery, variant selector, hero)
- Content (rich text, images, contact, reviews)
`use SimpleshopThemeWeb.ShopComponents` imports all sub-modules.
No single file over ~1,600 lines now.
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-08 14:30:25 +00:00
|
|
|
</.product_grid>
|
2026-01-19 23:26:41 +00:00
|
|
|
|
|
|
|
|
<%= if @products == [] do %>
|
2026-02-17 19:07:15 +00:00
|
|
|
<div class="collection-empty">
|
|
|
|
|
<p>No products found in this collection.</p>
|
|
|
|
|
<.link navigate={~p"/collections/all"} class="collection-empty-link">
|
2026-01-19 23:26:41 +00:00
|
|
|
View all products
|
|
|
|
|
</.link>
|
|
|
|
|
</div>
|
|
|
|
|
<% end %>
|
|
|
|
|
</div>
|
|
|
|
|
</main>
|
refactor: split shop_components.ex into 5 focused sub-modules
4,487-line monolith → 23-line facade + 5 modules:
- Base (inputs, buttons, cards)
- Layout (header, footer, mobile nav, shop_layout)
- Cart (drawer, items, order summary)
- Product (cards, gallery, variant selector, hero)
- Content (rich text, images, contact, reviews)
`use SimpleshopThemeWeb.ShopComponents` imports all sub-modules.
No single file over ~1,600 lines now.
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-08 14:30:25 +00:00
|
|
|
</.shop_layout>
|
2026-01-19 23:26:41 +00:00
|
|
|
"""
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
defp collection_filter_bar(assigns) do
|
|
|
|
|
~H"""
|
2026-02-17 19:07:15 +00:00
|
|
|
<div class="filter-bar">
|
2026-02-16 08:20:55 +00:00
|
|
|
<nav
|
|
|
|
|
aria-label="Collection filters"
|
2026-02-16 17:47:51 +00:00
|
|
|
id="collection-filters"
|
|
|
|
|
phx-hook="CollectionFilters"
|
|
|
|
|
class="collection-filters"
|
2026-02-16 08:20:55 +00:00
|
|
|
>
|
2026-02-16 17:47:51 +00:00
|
|
|
<ul class="collection-filter-pills">
|
|
|
|
|
<li>
|
2026-01-19 23:26:41 +00:00
|
|
|
<.link
|
2026-01-19 23:38:22 +00:00
|
|
|
navigate={collection_path("all", @current_sort)}
|
2026-02-16 17:47:51 +00:00
|
|
|
aria-current={@current_slug == nil && "page"}
|
2026-02-17 19:07:15 +00:00
|
|
|
class={["collection-filter-pill", @current_slug == nil && "active"]}
|
2026-01-19 23:26:41 +00:00
|
|
|
>
|
2026-01-19 23:38:22 +00:00
|
|
|
All
|
2026-01-19 23:26:41 +00:00
|
|
|
</.link>
|
|
|
|
|
</li>
|
2026-02-16 17:47:51 +00:00
|
|
|
<li>
|
2026-02-11 08:38:54 +00:00
|
|
|
<.link
|
|
|
|
|
navigate={collection_path("sale", @current_sort)}
|
2026-02-16 17:47:51 +00:00
|
|
|
aria-current={@current_slug == "sale" && "page"}
|
2026-02-17 19:07:15 +00:00
|
|
|
class={["collection-filter-pill", @current_slug == "sale" && "active"]}
|
2026-02-11 08:38:54 +00:00
|
|
|
>
|
|
|
|
|
Sale
|
|
|
|
|
</.link>
|
|
|
|
|
</li>
|
2026-01-19 23:38:22 +00:00
|
|
|
<%= for category <- @categories do %>
|
2026-02-16 17:47:51 +00:00
|
|
|
<li>
|
2026-01-19 23:38:22 +00:00
|
|
|
<.link
|
|
|
|
|
navigate={collection_path(category.slug, @current_sort)}
|
2026-02-16 17:47:51 +00:00
|
|
|
aria-current={@current_slug == category.slug && "page"}
|
2026-02-17 19:07:15 +00:00
|
|
|
class={["collection-filter-pill", @current_slug == category.slug && "active"]}
|
2026-01-19 23:38:22 +00:00
|
|
|
>
|
|
|
|
|
{category.name}
|
|
|
|
|
</.link>
|
|
|
|
|
</li>
|
|
|
|
|
<% end %>
|
|
|
|
|
</ul>
|
|
|
|
|
</nav>
|
|
|
|
|
|
|
|
|
|
<form phx-change="sort_changed">
|
2026-01-25 19:09:49 +00:00
|
|
|
<.shop_select
|
2026-01-19 23:38:22 +00:00
|
|
|
name="sort"
|
2026-01-25 19:09:49 +00:00
|
|
|
options={@sort_options}
|
|
|
|
|
selected={@current_sort}
|
2026-01-19 23:38:22 +00:00
|
|
|
aria-label="Sort products"
|
2026-01-25 19:09:49 +00:00
|
|
|
/>
|
2026-01-19 23:38:22 +00:00
|
|
|
</form>
|
|
|
|
|
</div>
|
2026-01-19 23:26:41 +00:00
|
|
|
"""
|
|
|
|
|
end
|
|
|
|
|
end
|