40 lines
1.2 KiB
TypeScript
40 lines
1.2 KiB
TypeScript
'use client'
|
|
|
|
import { useState } from 'react'
|
|
|
|
import { Button } from '@/components/ui/button'
|
|
import { toast } from '@/components/ui/toast'
|
|
import { getApiError } from '@/lib/api/index'
|
|
|
|
interface ExportButtonsProps {
|
|
/** Runs the export for the chosen format; fetch the full filtered data inside. */
|
|
onExport: (format: 'excel' | 'pdf') => Promise<void>
|
|
disabled?: boolean
|
|
}
|
|
|
|
export function ExportButtons({ onExport, disabled }: ExportButtonsProps) {
|
|
const [busy, setBusy] = useState<'excel' | 'pdf' | null>(null)
|
|
|
|
async function run(format: 'excel' | 'pdf') {
|
|
setBusy(format)
|
|
try {
|
|
await onExport(format)
|
|
} catch (err) {
|
|
toast.add({ title: 'Export failed', description: getApiError(err), type: 'error' })
|
|
} finally {
|
|
setBusy(null)
|
|
}
|
|
}
|
|
|
|
return (
|
|
<>
|
|
<Button size="sm" variant="outline" disabled={disabled || busy !== null} onClick={() => run('excel')}>
|
|
{busy === 'excel' ? 'Exporting…' : 'Export Excel'}
|
|
</Button>
|
|
<Button size="sm" variant="outline" disabled={disabled || busy !== null} onClick={() => run('pdf')}>
|
|
{busy === 'pdf' ? 'Exporting…' : 'Export PDF'}
|
|
</Button>
|
|
</>
|
|
)
|
|
}
|