feat: add export csv button
This commit is contained in:
parent
d561e7da82
commit
9bb8acdce9
@ -157,6 +157,50 @@ export default function AdminUserManagementPage() {
|
||||
setPage(1)
|
||||
}
|
||||
|
||||
// NEW: CSV export utilities (exports all filtered results, not only current page)
|
||||
const toCsvValue = (v: unknown) => {
|
||||
if (v === null || v === undefined) return '""'
|
||||
const s = String(v).replace(/"/g, '""')
|
||||
return `"${s}"`
|
||||
}
|
||||
|
||||
const exportCsv = () => {
|
||||
const headers = [
|
||||
'ID','Email','Type','Role','Status','Admin Verified',
|
||||
'First Name','Last Name','Company Name','Created At','Last Login At'
|
||||
]
|
||||
const rows = filtered.map(u => {
|
||||
// Map backend to friendly values
|
||||
const userStatus: 'active'|'pending'|'disabled' =
|
||||
u.is_admin_verified === 1 ? 'active' :
|
||||
u.status === 'pending' ? 'pending' :
|
||||
u.status === 'suspended' ? 'disabled' : 'pending'
|
||||
return [
|
||||
u.id,
|
||||
u.email,
|
||||
u.user_type,
|
||||
u.role,
|
||||
userStatus,
|
||||
u.is_admin_verified === 1 ? 'yes' : 'no',
|
||||
u.first_name || '',
|
||||
u.last_name || '',
|
||||
u.company_name || '',
|
||||
new Date(u.created_at).toISOString(),
|
||||
u.last_login_at ? new Date(u.last_login_at).toISOString() : ''
|
||||
].map(toCsvValue).join(',')
|
||||
})
|
||||
const csv = [headers.join(','), ...rows].join('\r\n')
|
||||
const blob = new Blob([csv], { type: 'text/csv;charset=utf-8;' })
|
||||
const url = URL.createObjectURL(blob)
|
||||
const a = document.createElement('a')
|
||||
a.href = url
|
||||
a.download = `users_${new Date().toISOString().slice(0,10)}.csv`
|
||||
document.body.appendChild(a)
|
||||
a.click()
|
||||
a.remove()
|
||||
URL.revokeObjectURL(url)
|
||||
}
|
||||
|
||||
const badge = (text: string, color: 'blue'|'amber'|'green'|'gray'|'rose'|'indigo'|'purple') => {
|
||||
const base = 'inline-flex items-center gap-1 rounded-full px-2 py-0.5 text-[10px] font-medium tracking-wide'
|
||||
const map: Record<string,string> = {
|
||||
@ -297,7 +341,16 @@ export default function AdminUserManagementPage() {
|
||||
</select>
|
||||
</div>
|
||||
</div>
|
||||
<div className="flex justify-end">
|
||||
<div className="flex justify-end gap-2">
|
||||
{/* NEW: Export CSV (exports all filtered results) */}
|
||||
<button
|
||||
type="button"
|
||||
onClick={exportCsv}
|
||||
className="inline-flex items-center gap-2 rounded-md border border-gray-300 bg-white hover:bg-gray-50 text-gray-700 text-sm font-semibold px-5 py-2.5 shadow-sm transition"
|
||||
title="Export all filtered users to CSV"
|
||||
>
|
||||
Export all users as CSV
|
||||
</button>
|
||||
<button
|
||||
type="submit"
|
||||
className="inline-flex items-center gap-2 rounded-md bg-blue-600 hover:bg-blue-500 text-white text-sm font-semibold px-5 py-2.5 shadow transition"
|
||||
|
||||
Loading…
Reference in New Issue
Block a user