Files
spliit/src/components/async-button.tsx
Sebastien Castiel 570aa713b1 Delete expense
2023-12-06 15:08:52 -05:00

43 lines
858 B
TypeScript

'use client'
import { Button, ButtonProps } from '@/components/ui/button'
import { Loader2 } from 'lucide-react'
import { ReactNode, useState } from 'react'
type Props = ButtonProps & {
action?: () => Promise<void>
loadingContent?: ReactNode
}
export function AsyncButton({
action,
children,
loadingContent,
...props
}: Props) {
const [loading, setLoading] = useState(false)
return (
<Button
onClick={async () => {
try {
setLoading(true)
await action?.()
} catch (err) {
console.error(err)
} finally {
setLoading(false)
}
}}
{...props}
>
{loading ? (
<>
<Loader2 className="w-4 h-4 mr-2 animate-spin" />{' '}
{loadingContent ?? children}
</>
) : (
children
)}
</Button>
)
}