From 570aa713b1b2becf7c06c89610c75d2cb119b532 Mon Sep 17 00:00:00 2001
From: Sebastien Castiel <sebastien@castiel.me>
Date: Wed, 6 Dec 2023 15:08:52 -0500
Subject: Delete expense

---
 src/components/async-button.tsx | 42 +++++++++++++++++++++++++++++++++++++++++
 1 file changed, 42 insertions(+)
 create mode 100644 src/components/async-button.tsx

(limited to 'src/components/async-button.tsx')

diff --git a/src/components/async-button.tsx b/src/components/async-button.tsx
new file mode 100644
index 0000000..7d2bf93
--- /dev/null
+++ b/src/components/async-button.tsx
@@ -0,0 +1,42 @@
+'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>
+  )
+}
-- 
cgit v1.3

