From 8c8edfc9c8bb1a0fa3d1e02aab576044857f1c95 Mon Sep 17 00:00:00 2001
From: Wolfboy248 <105884620+Wolfboy248@users.noreply.github.com>
Date: Sun, 20 Oct 2024 00:27:38 +0200
Subject: refactor: custom modals

---
 frontend/src/components/UseConfirm.tsx | 36 ++++++++++++++++++++++++++++++++++
 1 file changed, 36 insertions(+)
 create mode 100644 frontend/src/components/UseConfirm.tsx

(limited to 'frontend/src/components/UseConfirm.tsx')

diff --git a/frontend/src/components/UseConfirm.tsx b/frontend/src/components/UseConfirm.tsx
new file mode 100644
index 0000000..2fe0e12
--- /dev/null
+++ b/frontend/src/components/UseConfirm.tsx
@@ -0,0 +1,36 @@
+import React, { useState } from 'react';
+import ConfirmDialog from './ConfirmDialog';
+
+const useConfirm = ( title: string, subtitle: string ) => {
+    const [isOpen, setIsOpen] = useState(false);
+    const [resolvePromise, setResolvePromise] = useState<((value: boolean) => void) | null>(null);
+
+    const confirm = () => {
+        setIsOpen(true);
+        return new Promise((resolve) => {
+            setResolvePromise(() => resolve);
+        });
+    };
+
+    const handleConfirm = () => {
+        setIsOpen(false);
+        if (resolvePromise) {
+            resolvePromise(true);
+        }
+    }
+
+    const handleCancel = () => {
+        setIsOpen(false);
+        if (resolvePromise) {
+            resolvePromise(false);
+        }
+    }
+
+    const ConfirmDialogComponent = isOpen && (
+        <ConfirmDialog title={title} subtitle={subtitle} onConfirm={handleConfirm} onCancel={handleCancel}></ConfirmDialog>
+    );
+
+    return { confirm, ConfirmDialogComponent };
+}
+
+export default useConfirm;
-- 
cgit v1.2.3

