berrypod/lib/berrypod_web/live/admin/dashboard.ex
jamey 559798206f extract setup wizard to dedicated /admin/setup page
Move the setup stepper out of the dashboard into its own LiveView.
Dashboard now redirects to setup when site isn't live, and shows
stats-only view once live. Also cleans up button component variant
handling, fixes alert CSS, and removes stale demo.html.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-18 23:55:42 +00:00

167 lines
5.4 KiB
Elixir

defmodule BerrypodWeb.Admin.Dashboard do
use BerrypodWeb, :live_view
alias Berrypod.{Cart, Orders, Products, Settings}
@impl true
def mount(_params, _session, socket) do
if Settings.site_live?() do
status_counts = Orders.count_orders_by_status()
paid_count = Map.get(status_counts, "paid", 0)
recent_orders = Orders.list_orders(status: "paid") |> Enum.take(5)
{:ok,
socket
|> assign(:page_title, "Dashboard")
|> assign(:paid_count, paid_count)
|> assign(:revenue, Orders.total_revenue())
|> assign(:product_count, Products.count_products())
|> assign(:recent_orders, recent_orders)}
else
{:ok, push_navigate(socket, to: ~p"/admin/setup")}
end
end
@impl true
def render(assigns) do
~H"""
<.header>
Dashboard
</.header>
<%!-- Stats --%>
<div class="grid grid-cols-1 sm:grid-cols-3 gap-4 mt-6">
<.stat_card
label="Orders"
value={@paid_count}
icon="hero-shopping-bag"
href={~p"/admin/orders"}
/>
<.stat_card
label="Revenue"
value={format_revenue(@revenue)}
icon="hero-banknotes"
href={~p"/admin/orders"}
/>
<.stat_card
label="Products"
value={@product_count}
icon="hero-cube"
href={~p"/admin/products"}
/>
</div>
<%!-- Recent orders --%>
<section class="mt-8">
<div class="flex items-center justify-between mb-4">
<h2 class="text-lg font-semibold">Recent orders</h2>
<.link
navigate={~p"/admin/orders"}
class="text-sm text-base-content/60 hover:text-base-content"
>
View all &rarr;
</.link>
</div>
<%= if @recent_orders == [] do %>
<div class="rounded-lg border border-base-200 p-8 text-center text-base-content/60">
<.icon name="hero-inbox" class="size-10 mx-auto mb-3 text-base-content/30" />
<p class="font-medium">No orders yet</p>
<p class="text-sm mt-1">Orders will appear here once customers check out.</p>
</div>
<% else %>
<div class="overflow-x-auto">
<table class="w-full text-sm">
<thead>
<tr class="border-b border-base-200 text-left text-base-content/60">
<th class="pb-2 font-medium">Order</th>
<th class="pb-2 font-medium">Date</th>
<th class="pb-2 font-medium">Customer</th>
<th class="pb-2 font-medium text-right">Total</th>
<th class="pb-2 font-medium">Fulfilment</th>
</tr>
</thead>
<tbody>
<tr
:for={order <- @recent_orders}
class="border-b border-base-200 hover:bg-base-200/50 cursor-pointer"
phx-click={JS.navigate(~p"/admin/orders/#{order}")}
>
<td class="py-2.5 font-medium">{order.order_number}</td>
<td class="py-2.5 text-base-content/60">{format_date(order.inserted_at)}</td>
<td class="py-2.5 text-base-content/60">{order.customer_email || "—"}</td>
<td class="py-2.5 text-right">{Cart.format_price(order.total)}</td>
<td class="py-2.5"><.fulfilment_pill status={order.fulfilment_status} /></td>
</tr>
</tbody>
</table>
</div>
<% end %>
</section>
"""
end
# ==========================================================================
# Components
# ==========================================================================
attr :label, :string, required: true
attr :value, :any, required: true
attr :icon, :string, required: true
attr :href, :string, required: true
defp stat_card(assigns) do
~H"""
<.link
navigate={@href}
class="rounded-lg border border-base-200 p-4 hover:border-base-300 transition-colors"
>
<div class="flex items-center gap-3">
<div class="rounded-lg bg-base-200 p-2">
<.icon name={@icon} class="size-5 text-base-content/60" />
</div>
<div>
<p class="text-2xl font-bold">{@value}</p>
<p class="text-sm text-base-content/60">{@label}</p>
</div>
</div>
</.link>
"""
end
defp fulfilment_pill(assigns) do
{color, label} =
case assigns.status do
"unfulfilled" -> {"bg-base-200 text-base-content/60", "unfulfilled"}
"submitted" -> {"bg-blue-50 text-blue-700", "submitted"}
"processing" -> {"bg-amber-50 text-amber-700", "processing"}
"shipped" -> {"bg-purple-50 text-purple-700", "shipped"}
"delivered" -> {"bg-green-50 text-green-700", "delivered"}
"failed" -> {"bg-red-50 text-red-700", "failed"}
_ -> {"bg-base-200 text-base-content/60", assigns.status || ""}
end
assigns = assign(assigns, color: color, label: label)
~H"""
<span class={["inline-flex rounded-full px-2 py-0.5 text-xs font-medium", @color]}>
{@label}
</span>
"""
end
# ==========================================================================
# Helpers
# ==========================================================================
defp format_revenue(amount_pence) when is_integer(amount_pence) do
Cart.format_price(amount_pence)
end
defp format_revenue(_), do: "£0.00"
defp format_date(datetime) do
Calendar.strftime(datetime, "%d %b %Y")
end
end