2026-02-18 21:23:15 +00:00
|
|
|
defmodule BerrypodWeb.ThemeHook do
|
2026-02-08 11:59:33 +00:00
|
|
|
@moduledoc """
|
|
|
|
|
LiveView on_mount hook for theme settings, CSS, and media assigns.
|
|
|
|
|
|
|
|
|
|
Mounted in the public_shop live_session alongside CartHook.
|
|
|
|
|
Eliminates the identical theme-loading boilerplate from every shop LiveView.
|
2026-02-11 22:58:58 +00:00
|
|
|
|
|
|
|
|
## Actions
|
|
|
|
|
|
|
|
|
|
- `:mount_theme` — loads theme settings, CSS, and media assigns
|
|
|
|
|
- `:require_site_live` — redirects unauthenticated visitors to /coming-soon
|
|
|
|
|
when the shop is not live
|
2026-02-08 11:59:33 +00:00
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
import Phoenix.Component, only: [assign: 3]
|
|
|
|
|
|
2026-02-18 21:23:15 +00:00
|
|
|
alias Berrypod.{Products, Settings, Media}
|
|
|
|
|
alias Berrypod.Theme.{CSSCache, CSSGenerator}
|
2026-02-08 11:59:33 +00:00
|
|
|
|
2026-02-28 11:18:37 +00:00
|
|
|
@default_header_nav [
|
|
|
|
|
%{"label" => "Home", "href" => "/", "slug" => "home"},
|
|
|
|
|
%{
|
|
|
|
|
"label" => "Shop",
|
|
|
|
|
"href" => "/collections/all",
|
|
|
|
|
"slug" => "collection",
|
|
|
|
|
"active_slugs" => ["collection", "pdp"]
|
|
|
|
|
},
|
|
|
|
|
%{"label" => "About", "href" => "/about", "slug" => "about"},
|
|
|
|
|
%{"label" => "Contact", "href" => "/contact", "slug" => "contact"}
|
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
@default_footer_nav [
|
|
|
|
|
%{"label" => "Delivery & returns", "href" => "/delivery", "slug" => "delivery"},
|
|
|
|
|
%{"label" => "Privacy policy", "href" => "/privacy", "slug" => "privacy"},
|
|
|
|
|
%{"label" => "Terms of service", "href" => "/terms", "slug" => "terms"},
|
|
|
|
|
%{"label" => "Contact", "href" => "/contact", "slug" => "contact"}
|
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
def default_header_nav, do: @default_header_nav
|
|
|
|
|
def default_footer_nav, do: @default_footer_nav
|
|
|
|
|
|
2026-02-08 11:59:33 +00:00
|
|
|
def on_mount(:mount_theme, _params, _session, socket) do
|
|
|
|
|
theme_settings = Settings.get_theme_settings()
|
|
|
|
|
|
|
|
|
|
generated_css =
|
|
|
|
|
case CSSCache.get() do
|
|
|
|
|
{:ok, css} ->
|
|
|
|
|
css
|
|
|
|
|
|
|
|
|
|
:miss ->
|
2026-02-20 18:39:41 +00:00
|
|
|
css = CSSGenerator.generate(theme_settings, &BerrypodWeb.Endpoint.static_path/1)
|
2026-02-08 11:59:33 +00:00
|
|
|
CSSCache.put(css)
|
|
|
|
|
css
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
socket =
|
|
|
|
|
socket
|
|
|
|
|
|> assign(:theme_settings, theme_settings)
|
2026-03-03 14:52:31 +00:00
|
|
|
|> assign(:site_name, Settings.site_name())
|
|
|
|
|
|> assign(:site_description, Settings.site_description())
|
2026-02-08 11:59:33 +00:00
|
|
|
|> assign(:generated_css, generated_css)
|
|
|
|
|
|> assign(:logo_image, Media.get_logo())
|
|
|
|
|
|> assign(:header_image, Media.get_header())
|
2026-02-13 08:27:26 +00:00
|
|
|
|> assign(:categories, Products.list_categories())
|
2026-02-08 11:59:33 +00:00
|
|
|
|> assign(:mode, :shop)
|
2026-02-13 16:02:25 +00:00
|
|
|
|> assign(
|
|
|
|
|
:is_admin,
|
|
|
|
|
!!(socket.assigns[:current_scope] && socket.assigns.current_scope.user)
|
|
|
|
|
)
|
2026-02-28 11:18:37 +00:00
|
|
|
|> assign(:header_nav_items, load_nav("header_nav", @default_header_nav))
|
|
|
|
|
|> assign(:footer_nav_items, load_nav("footer_nav", @default_footer_nav))
|
2026-02-08 11:59:33 +00:00
|
|
|
|
|
|
|
|
{:cont, socket}
|
|
|
|
|
end
|
2026-02-11 22:58:58 +00:00
|
|
|
|
2026-02-12 14:46:07 +00:00
|
|
|
def on_mount(:require_site_live, _params, _session, socket) do
|
2026-02-11 22:58:58 +00:00
|
|
|
cond do
|
|
|
|
|
Settings.site_live?() ->
|
|
|
|
|
{:cont, socket}
|
|
|
|
|
|
2026-02-12 14:46:07 +00:00
|
|
|
# mount_current_scope runs first, so current_scope is already validated
|
|
|
|
|
socket.assigns[:current_scope] && socket.assigns.current_scope.user ->
|
2026-02-11 22:58:58 +00:00
|
|
|
{:cont, socket}
|
|
|
|
|
|
|
|
|
|
true ->
|
|
|
|
|
{:halt, Phoenix.LiveView.redirect(socket, to: "/coming-soon")}
|
|
|
|
|
end
|
|
|
|
|
end
|
2026-02-28 11:18:37 +00:00
|
|
|
|
|
|
|
|
defp load_nav(key, default) do
|
|
|
|
|
case Settings.get_setting(key) do
|
|
|
|
|
items when is_list(items) -> items
|
|
|
|
|
_ -> default
|
|
|
|
|
end
|
|
|
|
|
end
|
2026-02-08 11:59:33 +00:00
|
|
|
end
|