diff options
Diffstat (limited to 'frontend/src/components')
| -rw-r--r-- | frontend/src/components/UploadRunDialog.tsx | 32 | ||||
| -rw-r--r-- | frontend/src/components/UseConfirm.tsx | 36 | ||||
| -rw-r--r-- | frontend/src/components/UseMessage.tsx | 27 |
3 files changed, 22 insertions, 73 deletions
diff --git a/frontend/src/components/UploadRunDialog.tsx b/frontend/src/components/UploadRunDialog.tsx index b26bbe6..f3258e8 100644 --- a/frontend/src/components/UploadRunDialog.tsx +++ b/frontend/src/components/UploadRunDialog.tsx | |||
| @@ -7,6 +7,8 @@ import { Game } from '../types/Game'; | |||
| 7 | import { Map } from '../types/Map'; | 7 | import { Map } from '../types/Map'; |
| 8 | import { API } from '../api/Api'; | 8 | import { API } from '../api/Api'; |
| 9 | import { useNavigate } from 'react-router-dom'; | 9 | import { useNavigate } from 'react-router-dom'; |
| 10 | import useMessage from '../hooks/UseMessage'; | ||
| 11 | import useConfirm from '../hooks/UseConfirm'; | ||
| 10 | 12 | ||
| 11 | interface UploadRunDialogProps { | 13 | interface UploadRunDialogProps { |
| 12 | token?: string; | 14 | token?: string; |
| @@ -16,6 +18,8 @@ interface UploadRunDialogProps { | |||
| 16 | } | 18 | } |
| 17 | 19 | ||
| 18 | const UploadRunDialog: React.FC<UploadRunDialogProps> = ({ token, open, onClose, games }) => { | 20 | const UploadRunDialog: React.FC<UploadRunDialogProps> = ({ token, open, onClose, games }) => { |
| 21 | const { message, MessageDialogComponent } = useMessage(); | ||
| 22 | const { confirm, ConfirmDialogComponent } = useConfirm("Upload demo?", "Are you sure you want to upload this demo?"); | ||
| 19 | 23 | ||
| 20 | const navigate = useNavigate(); | 24 | const navigate = useNavigate(); |
| 21 | 25 | ||
| @@ -89,18 +93,18 @@ const UploadRunDialog: React.FC<UploadRunDialogProps> = ({ token, open, onClose, | |||
| 89 | if (token) { | 93 | if (token) { |
| 90 | if (games[selectedGameID].is_coop) { | 94 | if (games[selectedGameID].is_coop) { |
| 91 | if (uploadRunContent.host_demo === null) { | 95 | if (uploadRunContent.host_demo === null) { |
| 92 | alert("You must select a host demo to upload.") | 96 | message("Error", "You must select a host demo to upload.") |
| 93 | return | 97 | return |
| 94 | } else if (uploadRunContent.partner_demo === null) { | 98 | } else if (uploadRunContent.partner_demo === null) { |
| 95 | alert("You must select a partner demo to upload.") | 99 | message("Error", "You must select a partner demo to upload.") |
| 96 | return | 100 | return |
| 97 | } else if (uploadRunContent.partner_id === undefined) { | 101 | } else if (uploadRunContent.partner_id === undefined) { |
| 98 | alert("You must specify your partner.") | 102 | message("Error", "You must specify your partner.") |
| 99 | return | 103 | return |
| 100 | } | 104 | } |
| 101 | } else { | 105 | } else { |
| 102 | if (uploadRunContent.host_demo === null) { | 106 | if (uploadRunContent.host_demo === null) { |
| 103 | alert("You must select a demo to upload.") | 107 | message("Error", "You must select a demo to upload.") |
| 104 | return | 108 | return |
| 105 | } | 109 | } |
| 106 | } | 110 | } |
| @@ -112,17 +116,22 @@ const UploadRunDialog: React.FC<UploadRunDialogProps> = ({ token, open, onClose, | |||
| 112 | }) | 116 | }) |
| 113 | 117 | ||
| 114 | if (!scoreboard) { | 118 | if (!scoreboard) { |
| 115 | alert("Error while processing demo: Unable to get scoreboard result. Is this not a CM demo?") | 119 | message("Error", "Error while processing demo: Unable to get scoreboard result. Is this not a CM demo?") |
| 116 | return | 120 | return |
| 117 | } | 121 | } |
| 118 | const { portalScore, timeScore } = scoreboard.userMessage?.as<ScoreboardTempUpdate>() ?? {}; | 122 | const { portalScore, timeScore } = scoreboard.userMessage?.as<ScoreboardTempUpdate>() ?? {}; |
| 119 | console.log(`Portal count: ${portalScore}. Ticks: ${timeScore}.`); | 123 | console.log(`Portal count: ${portalScore}. Ticks: ${timeScore}.`); |
| 120 | if (window.confirm("Are you sure you want to submit this run to LPHUB?")) { | 124 | |
| 121 | const message = await API.post_record(token, uploadRunContent); | 125 | const userConfirmed = await confirm(); |
| 122 | alert(message); | 126 | |
| 123 | navigate(0); | 127 | if (!userConfirmed) { |
| 124 | onClose(); | 128 | return; |
| 125 | } | 129 | } |
| 130 | |||
| 131 | const response = await API.post_record(token, uploadRunContent); | ||
| 132 | message("Message", response); | ||
| 133 | // navigate(0); | ||
| 134 | onClose(); | ||
| 126 | } | 135 | } |
| 127 | }; | 136 | }; |
| 128 | 137 | ||
| @@ -136,6 +145,9 @@ const UploadRunDialog: React.FC<UploadRunDialogProps> = ({ token, open, onClose, | |||
| 136 | return ( | 145 | return ( |
| 137 | <> | 146 | <> |
| 138 | <div id="upload-run-block" /> | 147 | <div id="upload-run-block" /> |
| 148 | {MessageDialogComponent} | ||
| 149 | {ConfirmDialogComponent} | ||
| 150 | |||
| 139 | <div id='upload-run-menu'> | 151 | <div id='upload-run-menu'> |
| 140 | <div id='upload-run-menu-add'> | 152 | <div id='upload-run-menu-add'> |
| 141 | <div id='upload-run-route-category'> | 153 | <div id='upload-run-route-category'> |
diff --git a/frontend/src/components/UseConfirm.tsx b/frontend/src/components/UseConfirm.tsx deleted file mode 100644 index 2fe0e12..0000000 --- a/frontend/src/components/UseConfirm.tsx +++ /dev/null | |||
| @@ -1,36 +0,0 @@ | |||
| 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; | ||
diff --git a/frontend/src/components/UseMessage.tsx b/frontend/src/components/UseMessage.tsx deleted file mode 100644 index fbd3c82..0000000 --- a/frontend/src/components/UseMessage.tsx +++ /dev/null | |||
| @@ -1,27 +0,0 @@ | |||
| 1 | import React, { useState } from 'react'; | ||
| 2 | import MessageDialog from "./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; | ||