feat: add Cart page and themed error pages
- Add ShopLive.Cart at /cart using shared PageTemplates - Update ErrorHTML to render fully themed 404/500 pages - Add dev-only error preview routes at /dev/errors/404 and /dev/errors/500 - Update error page tests for themed output Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
@@ -6,19 +6,115 @@ defmodule SimpleshopThemeWeb.ErrorHTML do
|
||||
"""
|
||||
use SimpleshopThemeWeb, :html
|
||||
|
||||
# If you want to customize your error pages,
|
||||
# uncomment the embed_templates/1 call below
|
||||
# and add pages to the error directory:
|
||||
#
|
||||
# * lib/simpleshop_theme_web/controllers/error_html/404.html.heex
|
||||
# * lib/simpleshop_theme_web/controllers/error_html/500.html.heex
|
||||
#
|
||||
# embed_templates "error_html/*"
|
||||
alias SimpleshopTheme.Settings
|
||||
alias SimpleshopTheme.Settings.ThemeSettings
|
||||
alias SimpleshopTheme.Media
|
||||
alias SimpleshopTheme.Theme.{CSSCache, CSSGenerator, PreviewData}
|
||||
|
||||
def render("404.html", assigns) do
|
||||
render_error_page(assigns, "404", "Page Not Found",
|
||||
"Sorry, we couldn't find the page you're looking for. Perhaps you've mistyped the URL or the page has been moved.")
|
||||
end
|
||||
|
||||
def render("500.html", assigns) do
|
||||
render_error_page(assigns, "500", "Server Error",
|
||||
"Something went wrong on our end. Please try again later or contact support if the problem persists.")
|
||||
end
|
||||
|
||||
# The default is to render a plain text page based on
|
||||
# the template name. For example, "404.html" becomes
|
||||
# "Not Found".
|
||||
def render(template, _assigns) do
|
||||
Phoenix.Controller.status_message_from_template(template)
|
||||
end
|
||||
|
||||
defp render_error_page(assigns, error_code, error_title, error_description) do
|
||||
# Load theme settings with fallback for error conditions
|
||||
{theme_settings, generated_css} = load_theme_data()
|
||||
logo_image = safe_load(&Media.get_logo/0)
|
||||
header_image = safe_load(&Media.get_header/0)
|
||||
|
||||
preview_data = %{
|
||||
products: PreviewData.products(),
|
||||
categories: PreviewData.categories()
|
||||
}
|
||||
|
||||
assigns =
|
||||
assigns
|
||||
|> Map.put(:theme_settings, theme_settings)
|
||||
|> Map.put(:generated_css, generated_css)
|
||||
|> Map.put(:logo_image, logo_image)
|
||||
|> Map.put(:header_image, header_image)
|
||||
|> Map.put(:preview_data, preview_data)
|
||||
|> Map.put(:error_code, error_code)
|
||||
|> Map.put(:error_title, error_title)
|
||||
|> Map.put(:error_description, error_description)
|
||||
|> Map.put(:mode, :shop)
|
||||
|> Map.put(:cart_items, [])
|
||||
|> Map.put(:cart_count, 0)
|
||||
|> Map.put(:cart_subtotal, "£0.00")
|
||||
|
||||
~H"""
|
||||
<!DOCTYPE html>
|
||||
<html lang="en" class="h-full">
|
||||
<head>
|
||||
<meta charset="utf-8" />
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1" />
|
||||
<title><%= @error_code %> - <%= @error_title %></title>
|
||||
<link phx-track-static rel="stylesheet" href={~p"/assets/app.css"} />
|
||||
<style id="theme-css">
|
||||
<%= Phoenix.HTML.raw(@generated_css) %>
|
||||
</style>
|
||||
</head>
|
||||
<body class="h-full">
|
||||
<div class="shop-root themed h-full"
|
||||
data-mood={@theme_settings.mood}
|
||||
data-typography={@theme_settings.typography}
|
||||
data-shape={@theme_settings.shape}
|
||||
data-density={@theme_settings.density}
|
||||
data-grid={@theme_settings.grid_columns}
|
||||
data-header={@theme_settings.header_layout}
|
||||
data-sticky={to_string(@theme_settings.sticky_header)}
|
||||
data-layout={@theme_settings.layout_width}
|
||||
data-shadow={@theme_settings.card_shadow}>
|
||||
<SimpleshopThemeWeb.PageTemplates.error
|
||||
theme_settings={@theme_settings}
|
||||
logo_image={@logo_image}
|
||||
header_image={@header_image}
|
||||
preview_data={@preview_data}
|
||||
error_code={@error_code}
|
||||
error_title={@error_title}
|
||||
error_description={@error_description}
|
||||
mode={@mode}
|
||||
cart_items={@cart_items}
|
||||
cart_count={@cart_count}
|
||||
cart_subtotal={@cart_subtotal}
|
||||
/>
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
||||
"""
|
||||
end
|
||||
|
||||
defp load_theme_data do
|
||||
try 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
|
||||
{theme_settings, generated_css}
|
||||
rescue
|
||||
_ -> {%ThemeSettings{}, ""}
|
||||
end
|
||||
end
|
||||
|
||||
defp safe_load(fun) do
|
||||
try do
|
||||
fun.()
|
||||
rescue
|
||||
_ -> nil
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -0,0 +1,20 @@
|
||||
defmodule SimpleshopThemeWeb.ErrorPreviewController do
|
||||
@moduledoc """
|
||||
Development-only controller for previewing error pages.
|
||||
"""
|
||||
use SimpleshopThemeWeb, :controller
|
||||
|
||||
def not_found(conn, _params) do
|
||||
conn
|
||||
|> put_status(:not_found)
|
||||
|> put_view(SimpleshopThemeWeb.ErrorHTML)
|
||||
|> render("404.html")
|
||||
end
|
||||
|
||||
def server_error(conn, _params) do
|
||||
conn
|
||||
|> put_status(:internal_server_error)
|
||||
|> put_view(SimpleshopThemeWeb.ErrorHTML)
|
||||
|> render("500.html")
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user