freshyo/apps/superadmin-ui/src/routes/admins.tsx
2026-10-06 11:53:12 +05:30

143 lines
5.1 KiB
TypeScript

import { createFileRoute, Link, Outlet, useLocation, useNavigate } from '@tanstack/react-router'
import { Pencil, Plus, Trash2, UserCog } from 'lucide-react'
import { AppContainer, MyButton, p as MyText } from 'web-components'
import { trpc } from '../lib/trpc-client'
import { ProtectedRoute } from '../components/ProtectedRoute'
import { AppShell } from '../components/AppShell'
export const Route = createFileRoute('/admins')({
component: () => (
<ProtectedRoute>
<AppShell>
<Admins />
</AppShell>
</ProtectedRoute>
),
})
// This route is the layout for /admins plus its children
// (/admins/new and /admins/$id/edit). Render the list only on the exact
// /admins path; otherwise hand off to the child route.
function Admins() {
const location = useLocation()
if (location.pathname !== '/admins') {
return <Outlet />
}
return <AdminsList />
}
function AdminsList() {
const navigate = useNavigate()
const utils = trpc.useUtils()
const { data, isLoading, error, refetch } = trpc.superadmin.staffUser.getStaff.useQuery()
const deleteStaffUser = trpc.superadmin.staffUser.deleteStaffUser.useMutation({
onSuccess: () => {
void utils.superadmin.staffUser.getStaff.invalidate()
},
onError: (err) => {
window.alert(`Error\n\n${err.message || 'Failed to delete admin'}`)
},
})
const staff = data?.staff ?? []
const handleDelete = (id: number, name: string) => {
if (!window.confirm(`Delete admin "${name}"? This cannot be undone.`)) return
deleteStaffUser.mutate({ id })
}
return (
<AppContainer>
<div className="bg-gray-50 p-4">
<div className="mb-6 flex items-center justify-between">
<div>
<MyText className="mb-1 block text-2xl font-bold text-gray-900">Admins</MyText>
<MyText className="block text-gray-600">
Staff users and the region each one manages.
</MyText>
</div>
<MyButton
onClick={() => navigate({ to: '/admins/new' })}
className="flex items-center gap-2 rounded-lg bg-blue-600 px-4 py-2.5 text-white hover:bg-blue-700"
>
<Plus className="h-4 w-4" />
Add Admin
</MyButton>
</div>
{isLoading && (
<MyText className="block py-8 text-center text-gray-500">Loading admins...</MyText>
)}
{error && (
<div className="flex flex-col items-center py-8">
<MyText className="mb-4 block text-red-600">Error loading admins</MyText>
<MyButton
onClick={() => refetch()}
className="rounded-lg bg-blue-500 px-4 py-2 text-white"
>
Retry
</MyButton>
</div>
)}
{!isLoading && !error && staff.length === 0 && (
<div className="flex flex-col items-center rounded-xl border border-dashed border-gray-300 bg-white py-12">
<UserCog className="mb-3 h-8 w-8 text-gray-400" />
<MyText className="block text-gray-600">No admins yet.</MyText>
<MyText className="block text-sm text-gray-400">
Click "Add Admin" to create the first one.
</MyText>
</div>
)}
<div className="grid gap-4 sm:grid-cols-2">
{staff.map((member) => (
<div
key={member.id}
className="flex flex-col rounded-xl border border-gray-200 bg-white p-5 shadow-sm"
>
<div className="mb-1 flex items-start gap-3">
<div className="flex h-10 w-10 shrink-0 items-center justify-center rounded-lg bg-blue-50">
<UserCog className="h-5 w-5 text-blue-600" />
</div>
<div className="min-w-0">
<MyText className="block truncate font-semibold text-gray-900">
{member.name}
</MyText>
<MyText className="block text-sm text-gray-500">
{member.roleName || 'No role'} · {member.regionName || 'No region'}
</MyText>
</div>
</div>
<div className="mt-auto flex gap-2 pt-4">
<Link
to="/admins/$id/edit"
params={{ id: String(member.id) }}
className="flex flex-1 items-center justify-center gap-2 rounded-lg border border-gray-200 px-3 py-2 text-sm font-medium text-gray-700 hover:bg-gray-50"
>
<Pencil className="h-4 w-4" />
Edit
</Link>
<button
type="button"
onClick={() => handleDelete(member.id, member.name)}
disabled={deleteStaffUser.isPending}
className="flex flex-1 items-center justify-center gap-2 rounded-lg border border-red-200 px-3 py-2 text-sm font-medium text-red-600 hover:bg-red-50 disabled:opacity-50"
>
<Trash2 className="h-4 w-4" />
Delete
</button>
</div>
</div>
))}
</div>
</div>
</AppContainer>
)
}