Add admin screen for managing businesses

- Add AdminLive.Businesses at /admin/businesses for business management
- Support deactivate/reactivate (soft delete) and permanent deletion
- Filter tabs for All, Active, and Inactive businesses
- Delete confirmation modal to prevent accidental deletion
- Add admin functions to Businesses context (list_all, get_any, deactivate, activate, delete)

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
Kevin Sivic 2025-12-01 00:58:58 -05:00
parent 70bca86c23
commit f62ddffb80
2 changed files with 356 additions and 0 deletions

View file

@ -0,0 +1,251 @@
defmodule LocalspotWeb.AdminLive.Businesses do
use LocalspotWeb, :live_view
alias Localspot.Businesses
@impl true
def mount(_params, _session, socket) do
{:ok,
socket
|> assign(:page_title, "Manage Businesses")
|> assign(:businesses, Businesses.list_all_businesses())
|> assign(:filter, "all")
|> assign(:confirm_delete, nil)}
end
@impl true
def handle_event("filter", %{"filter" => filter}, socket) do
{:noreply,
socket
|> assign(:filter, filter)
|> assign(:businesses, filter_businesses(filter))}
end
@impl true
def handle_event("deactivate", %{"id" => id}, socket) do
business = Businesses.get_any_business(id)
case Businesses.deactivate_business(business) do
{:ok, _business} ->
{:noreply,
socket
|> put_flash(:info, "#{business.name} has been deactivated")
|> assign(:businesses, filter_businesses(socket.assigns.filter))}
{:error, _changeset} ->
{:noreply, put_flash(socket, :error, "Failed to deactivate business")}
end
end
@impl true
def handle_event("activate", %{"id" => id}, socket) do
business = Businesses.get_any_business(id)
case Businesses.activate_business(business) do
{:ok, _business} ->
{:noreply,
socket
|> put_flash(:info, "#{business.name} has been reactivated")
|> assign(:businesses, filter_businesses(socket.assigns.filter))}
{:error, _changeset} ->
{:noreply, put_flash(socket, :error, "Failed to activate business")}
end
end
@impl true
def handle_event("confirm_delete", %{"id" => id}, socket) do
{:noreply, assign(socket, :confirm_delete, id)}
end
@impl true
def handle_event("cancel_delete", _params, socket) do
{:noreply, assign(socket, :confirm_delete, nil)}
end
@impl true
def handle_event("delete", %{"id" => id}, socket) do
business = Businesses.get_any_business(id)
case Businesses.delete_business(business) do
{:ok, _business} ->
{:noreply,
socket
|> put_flash(:info, "#{business.name} has been permanently deleted")
|> assign(:confirm_delete, nil)
|> assign(:businesses, filter_businesses(socket.assigns.filter))}
{:error, _changeset} ->
{:noreply,
socket
|> put_flash(:error, "Failed to delete business")
|> assign(:confirm_delete, nil)}
end
end
defp filter_businesses("all"), do: Businesses.list_all_businesses()
defp filter_businesses("active") do
Businesses.list_all_businesses()
|> Enum.filter(& &1.active)
end
defp filter_businesses("inactive") do
Businesses.list_all_businesses()
|> Enum.filter(&(!&1.active))
end
@impl true
def render(assigns) do
~H"""
<Layouts.app flash={@flash}>
<.header>
Manage Businesses
<:subtitle>View, deactivate, or remove businesses from the directory</:subtitle>
<:actions>
<.link navigate={~p"/admin/import"} class="btn btn-outline btn-sm">
<.icon name="hero-arrow-up-tray" class="w-4 h-4" /> Import
</.link>
</:actions>
</.header>
<div class="mt-6">
<div class="flex items-center justify-between mb-4">
<div class="tabs tabs-boxed">
<button
class={"tab #{if @filter == "all", do: "tab-active"}"}
phx-click="filter"
phx-value-filter="all"
>
All ({length(Businesses.list_all_businesses())})
</button>
<button
class={"tab #{if @filter == "active", do: "tab-active"}"}
phx-click="filter"
phx-value-filter="active"
>
Active ({Enum.count(Businesses.list_all_businesses(), & &1.active)})
</button>
<button
class={"tab #{if @filter == "inactive", do: "tab-active"}"}
phx-click="filter"
phx-value-filter="inactive"
>
Inactive ({Enum.count(Businesses.list_all_businesses(), &(!&1.active))})
</button>
</div>
<div class="text-sm text-base-content/60">
{length(@businesses)} business(es)
</div>
</div>
<div :if={@businesses == []} class="alert">
<.icon name="hero-information-circle" class="w-5 h-5" />
<span>No businesses found.</span>
</div>
<div class="overflow-x-auto">
<table :if={@businesses != []} class="table table-zebra">
<thead>
<tr>
<th>Name</th>
<th>Category</th>
<th>Location</th>
<th>Status</th>
<th class="text-right">Actions</th>
</tr>
</thead>
<tbody>
<tr :for={business <- @businesses} class={if !business.active, do: "opacity-60"}>
<td>
<div class="font-medium">{business.name}</div>
<div class="text-sm text-base-content/60">{business.slug}</div>
</td>
<td>
<span :if={business.category} class="badge badge-ghost">
{business.category.name}
</span>
<span :if={!business.category} class="text-base-content/40"></span>
</td>
<td>
<div class="text-sm">{business.city}, {business.state}</div>
</td>
<td>
<span :if={business.active} class="badge badge-success badge-sm">Active</span>
<span :if={!business.active} class="badge badge-warning badge-sm">Inactive</span>
</td>
<td class="text-right">
<div class="flex justify-end gap-1">
<.link
navigate={~p"/businesses/#{business.slug}"}
class="btn btn-ghost btn-xs"
title="View"
>
<.icon name="hero-eye" class="w-4 h-4" />
</.link>
<button
:if={business.active}
class="btn btn-ghost btn-xs"
phx-click="deactivate"
phx-value-id={business.id}
title="Deactivate"
>
<.icon name="hero-pause" class="w-4 h-4" />
</button>
<button
:if={!business.active}
class="btn btn-ghost btn-xs"
phx-click="activate"
phx-value-id={business.id}
title="Reactivate"
>
<.icon name="hero-play" class="w-4 h-4" />
</button>
<button
class="btn btn-ghost btn-xs text-error"
phx-click="confirm_delete"
phx-value-id={business.id}
title="Delete permanently"
>
<.icon name="hero-trash" class="w-4 h-4" />
</button>
</div>
</td>
</tr>
</tbody>
</table>
</div>
</div>
<!-- Delete Confirmation Modal -->
<div
:if={@confirm_delete}
class="modal modal-open"
phx-window-keydown="cancel_delete"
phx-key="escape"
>
<div class="modal-box">
<h3 class="font-bold text-lg">Confirm Permanent Deletion</h3>
<p class="py-4">
Are you sure you want to permanently delete this business? This action cannot be undone.
</p>
<p class="text-sm text-base-content/60">
Consider deactivating instead if you might want to restore it later.
</p>
<div class="modal-action">
<button class="btn" phx-click="cancel_delete">Cancel</button>
<button class="btn btn-error" phx-click="delete" phx-value-id={@confirm_delete}>
Delete Permanently
</button>
</div>
</div>
<div class="modal-backdrop" phx-click="cancel_delete"></div>
</div>
</Layouts.app>
"""
end
end

View file

@ -0,0 +1,105 @@
defmodule Localspot.Businesses.AdminTest do
use Localspot.DataCase
alias Localspot.Businesses
describe "admin functions" do
setup do
{:ok, category} =
Businesses.create_category(%{
name: "Test Category",
slug: "test-category",
description: "For testing"
})
{:ok, business} =
Businesses.create_business(%{
name: "Test Business",
slug: "test-business",
street_address: "123 Test St",
city: "Columbus",
state: "OH",
zip_code: "43215",
category_id: category.id,
active: true
})
%{category: category, business: business}
end
test "list_all_businesses/0 returns all businesses including inactive", %{
category: category,
business: business
} do
# Create an inactive business
{:ok, inactive} =
Businesses.create_business(%{
name: "Inactive Business",
slug: "inactive-business",
street_address: "456 Test St",
city: "Columbus",
state: "OH",
zip_code: "43215",
category_id: category.id,
active: false
})
businesses = Businesses.list_all_businesses()
assert length(businesses) == 2
assert Enum.any?(businesses, &(&1.id == business.id))
assert Enum.any?(businesses, &(&1.id == inactive.id))
end
test "get_any_business/1 returns inactive businesses", %{category: category} do
{:ok, inactive} =
Businesses.create_business(%{
name: "Inactive Business",
slug: "inactive-business",
street_address: "456 Test St",
city: "Columbus",
state: "OH",
zip_code: "43215",
category_id: category.id,
active: false
})
# Regular get_business should not find it
assert is_nil(Businesses.get_business(inactive.id))
# But get_any_business should
found = Businesses.get_any_business(inactive.id)
assert found.id == inactive.id
end
test "deactivate_business/1 sets active to false", %{business: business} do
assert business.active == true
{:ok, deactivated} = Businesses.deactivate_business(business)
assert deactivated.active == false
# Should no longer appear in regular listing
businesses = Businesses.list_businesses()
refute Enum.any?(businesses, &(&1.id == business.id))
end
test "activate_business/1 sets active to true", %{business: business} do
{:ok, deactivated} = Businesses.deactivate_business(business)
assert deactivated.active == false
{:ok, reactivated} = Businesses.activate_business(deactivated)
assert reactivated.active == true
# Should appear in regular listing again
businesses = Businesses.list_businesses()
assert Enum.any?(businesses, &(&1.id == business.id))
end
test "delete_business/1 permanently removes the business", %{business: business} do
{:ok, _deleted} = Businesses.delete_business(business)
# Should not exist anywhere
assert is_nil(Businesses.get_business(business.id))
assert is_nil(Businesses.get_any_business(business.id))
end
end
end