diff options
Diffstat (limited to 'frontend/src/components/UseConfirm.tsx')
| -rw-r--r-- | frontend/src/components/UseConfirm.tsx | 36 |
1 files changed, 36 insertions, 0 deletions
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 @@ | |||
| 1 | import React, { useState } from 'react'; | ||
| 2 | import ConfirmDialog from './ConfirmDialog'; | ||
| 3 | |||
| 4 | const useConfirm = ( title: string, subtitle: string ) => { | ||
| 5 | const [isOpen, setIsOpen] = useState(false); | ||
| 6 | const [resolvePromise, setResolvePromise] = useState<((value: boolean) => void) | null>(null); | ||
| 7 | |||
| 8 | const confirm = () => { | ||
| 9 | setIsOpen(true); | ||
| 10 | return new Promise((resolve) => { | ||
| 11 | setResolvePromise(() => resolve); | ||
| 12 | }); | ||
| 13 | }; | ||
| 14 | |||
| 15 | const handleConfirm = () => { | ||
| 16 | setIsOpen(false); | ||
| 17 | if (resolvePromise) { | ||
| 18 | resolvePromise(true); | ||
| 19 | } | ||
| 20 | } | ||
| 21 | |||
| 22 | const handleCancel = () => { | ||
| 23 | setIsOpen(false); | ||
| 24 | if (resolvePromise) { | ||
| 25 | resolvePromise(false); | ||
| 26 | } | ||
| 27 | } | ||
| 28 | |||
| 29 | const ConfirmDialogComponent = isOpen && ( | ||
| 30 | <ConfirmDialog title={title} subtitle={subtitle} onConfirm={handleConfirm} onCancel={handleCancel}></ConfirmDialog> | ||
| 31 | ); | ||
| 32 | |||
| 33 | return { confirm, ConfirmDialogComponent }; | ||
| 34 | } | ||
| 35 | |||
| 36 | export default useConfirm; | ||