simpleshop_theme/lib/simpleshop_theme_web/theme_hook.ex
jamey e64bf40a71 add setup foundations: site gate, registration lockdown, coming soon page
- Settings.site_live?/0 and set_site_live/1 for shop visibility control
- Accounts.has_admin?/0 to detect single-tenant admin existence
- Registration lockdown: /users/register redirects when admin exists
- Setup.setup_status/0 aggregates provider, product, and stripe checks
- Coming soon page at /coming-soon with themed styling
- ThemeHook :require_site_live gate on all public shop routes
  - Site live → everyone through
  - Authenticated → admin preview through
  - No admin → fresh install demo through
  - Otherwise → redirect to coming soon
- Go live / take offline toggle on /admin/settings
- 648 tests, 0 failures

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-11 22:58:58 +00:00

62 lines
1.6 KiB
Elixir

defmodule SimpleshopThemeWeb.ThemeHook do
@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.
## 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
"""
import Phoenix.Component, only: [assign: 3]
alias SimpleshopTheme.{Settings, Media}
alias SimpleshopTheme.Theme.{CSSCache, CSSGenerator}
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 ->
css = CSSGenerator.generate(theme_settings)
CSSCache.put(css)
css
end
socket =
socket
|> assign(:theme_settings, theme_settings)
|> assign(:generated_css, generated_css)
|> assign(:logo_image, Media.get_logo())
|> assign(:header_image, Media.get_header())
|> assign(:mode, :shop)
{:cont, socket}
end
def on_mount(:require_site_live, _params, session, socket) do
cond do
Settings.site_live?() ->
{:cont, socket}
session["user_token"] ->
{:cont, socket}
not SimpleshopTheme.Accounts.has_admin?() ->
# Fresh install — no admin yet, show the demo shop
{:cont, socket}
true ->
{:halt, Phoenix.LiveView.redirect(socket, to: "/coming-soon")}
end
end
end