From 1c51ea2e373dcdd458d8cdc1d3b2225c316f346d Mon Sep 17 00:00:00 2001
From: Wolfboy248 <105884620+Wolfboy248@users.noreply.github.com>
Date: Sun, 20 Oct 2024 16:51:43 +0200
Subject: refactor: touch ups

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

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

diff --git a/frontend/src/hooks/UseConfirm.tsx b/frontend/src/hooks/UseConfirm.tsx
new file mode 100644
index 0000000..6de7b10
--- /dev/null
+++ b/frontend/src/hooks/UseConfirm.tsx
@@ -0,0 +1,36 @@
+import React, { useState } from 'react';
+import ConfirmDialog from '../components/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

