diff options
| author | Wolfboy248 <105884620+Wolfboy248@users.noreply.github.com> | 2024-10-20 16:51:43 +0200 |
|---|---|---|
| committer | Wolfboy248 <105884620+Wolfboy248@users.noreply.github.com> | 2024-10-20 16:51:43 +0200 |
| commit | 1c51ea2e373dcdd458d8cdc1d3b2225c316f346d (patch) | |
| tree | e8a667b3260ae6e79582e89070967b90552c6e73 /frontend/src/hooks | |
| parent | rankings: handle ties like iverb (diff) | |
| download | lphub-1c51ea2e373dcdd458d8cdc1d3b2225c316f346d.tar.gz lphub-1c51ea2e373dcdd458d8cdc1d3b2225c316f346d.tar.bz2 lphub-1c51ea2e373dcdd458d8cdc1d3b2225c316f346d.zip | |
refactor: touch ups
Diffstat (limited to 'frontend/src/hooks')
| -rw-r--r-- | frontend/src/hooks/UseConfirm.tsx | 36 | ||||
| -rw-r--r-- | frontend/src/hooks/UseMessage.tsx | 27 |
2 files changed, 63 insertions, 0 deletions
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 @@ | |||
| 1 | import React, { useState } from 'react'; | ||
| 2 | import ConfirmDialog from '../components/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; | ||
diff --git a/frontend/src/hooks/UseMessage.tsx b/frontend/src/hooks/UseMessage.tsx new file mode 100644 index 0000000..249a3bf --- /dev/null +++ b/frontend/src/hooks/UseMessage.tsx | |||
| @@ -0,0 +1,27 @@ | |||
| 1 | import React, { useState } from 'react'; | ||
| 2 | import MessageDialog from "../components/MessageDialog"; | ||
| 3 | |||
| 4 | const useMessage = () => { | ||
| 5 | const [isOpen, setIsOpen] = useState(false); | ||
| 6 | |||
| 7 | const [title, setTitle] = useState<string>(""); | ||
| 8 | const [subtitle, setSubtitle] = useState<string>(""); | ||
| 9 | |||
| 10 | const message = (title: string, subtitle: string) => { | ||
| 11 | setIsOpen(true); | ||
| 12 | setTitle(title); | ||
| 13 | setSubtitle(subtitle); | ||
| 14 | }; | ||
| 15 | |||
| 16 | const handleClose = () => { | ||
| 17 | setIsOpen(false); | ||
| 18 | }; | ||
| 19 | |||
| 20 | const MessageDialogComponent = isOpen && ( | ||
| 21 | <MessageDialog title={title} subtitle={subtitle} onClose={handleClose}></MessageDialog> | ||
| 22 | ); | ||
| 23 | |||
| 24 | return { message, MessageDialogComponent }; | ||
| 25 | } | ||
| 26 | |||
| 27 | export default useMessage; | ||