-
This commit is contained in:
@@ -0,0 +1,63 @@
|
||||
import { useState } from 'react'
|
||||
import { Copy, Trash2 } from 'lucide-react'
|
||||
import { can, useAuth } from '../../context/AuthContext'
|
||||
import { useApi } from '../../hooks/useApi'
|
||||
import api, { errorMessage } from '../../services/api'
|
||||
import { EmptyBlock, ErrorBlock, LoadingBlock, useFeedback } from '../components/feedback'
|
||||
import { Btn, Card, fmtDate, PageHeader, Pagination } from '../components/kit'
|
||||
|
||||
const PAGE_SIZE = 50
|
||||
|
||||
export default function SubscribersPage() {
|
||||
const { admin } = useAuth()
|
||||
const { toast, confirm } = useFeedback()
|
||||
const [page, setPage] = useState(1)
|
||||
const { data, loading, error, reload } = useApi('/admin/subscribers', { params: { page, page_size: PAGE_SIZE } })
|
||||
|
||||
const copyAll = async () => {
|
||||
try {
|
||||
await navigator.clipboard.writeText(data.items.map((s) => s.email).join(', '))
|
||||
toast('Emails on this page copied')
|
||||
} catch {
|
||||
toast('Could not access the clipboard', 'error')
|
||||
}
|
||||
}
|
||||
const remove = async (s) => {
|
||||
if (!(await confirm({ title: 'Remove subscriber?', message: s.email, confirmLabel: 'Remove', danger: true }))) return
|
||||
try {
|
||||
await api.delete(`/admin/subscribers/${s.id}`)
|
||||
toast('Subscriber removed')
|
||||
reload()
|
||||
} catch (err) {
|
||||
toast(errorMessage(err), 'error')
|
||||
}
|
||||
}
|
||||
|
||||
return (
|
||||
<>
|
||||
<PageHeader title="Subscribers" subtitle="Email sign-ups from the website footer." actions={data?.items.length > 0 && <Btn variant="ghost" onClick={copyAll}><Copy className="size-4" /> Copy emails</Btn>} />
|
||||
{loading && !data ? (
|
||||
<LoadingBlock />
|
||||
) : error ? (
|
||||
<ErrorBlock message={error} onRetry={reload} />
|
||||
) : data.items.length === 0 ? (
|
||||
<EmptyBlock title="No subscribers yet" />
|
||||
) : (
|
||||
<Card className="p-0 sm:p-0">
|
||||
<ul className="divide-y divide-line">
|
||||
{data.items.map((s) => (
|
||||
<li key={s.id} className="flex items-center justify-between gap-3 px-5 py-3">
|
||||
<span>
|
||||
<span className="font-medium text-navy">{s.email}</span>
|
||||
<span className="ml-2 text-[12px] text-slate">{fmtDate(s.created_at)}</span>
|
||||
</span>
|
||||
{can(admin, 'delete') && <Btn variant="danger" className="size-8 px-0" onClick={() => remove(s)} aria-label={`Remove ${s.email}`}><Trash2 className="size-3.5" /></Btn>}
|
||||
</li>
|
||||
))}
|
||||
</ul>
|
||||
</Card>
|
||||
)}
|
||||
{data && <Pagination page={page} pageSize={PAGE_SIZE} total={data.total} onPage={setPage} />}
|
||||
</>
|
||||
)
|
||||
}
|
||||
Reference in New Issue
Block a user