aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorWolfboy248 <georgejvindkarlsen@gmail.com>2024-10-28 11:35:04 +0100
committerWolfboy248 <georgejvindkarlsen@gmail.com>2024-10-28 11:35:04 +0100
commitb229a524fae8908928f16832e8e4d4c604cefa3e (patch)
tree0fec3ae174394d1cb6baf4abfe285770054821c4
parentrefactor: hook updated (diff)
downloadlphub-b229a524fae8908928f16832e8e4d4c604cefa3e.tar.gz
lphub-b229a524fae8908928f16832e8e4d4c604cefa3e.tar.bz2
lphub-b229a524fae8908928f16832e8e4d4c604cefa3e.zip
refactor: uploadrundialog
-rw-r--r--frontend/src/api/Api.tsx2
-rw-r--r--frontend/src/components/Login.tsx3
-rw-r--r--frontend/src/components/UploadRunDialog.tsx57
-rw-r--r--frontend/src/css/UploadRunDialog.css30
-rw-r--r--frontend/src/hooks/UseMessage.tsx4
5 files changed, 86 insertions, 10 deletions
diff --git a/frontend/src/api/Api.tsx b/frontend/src/api/Api.tsx
index ddc7ac6..053e920 100644
--- a/frontend/src/api/Api.tsx
+++ b/frontend/src/api/Api.tsx
@@ -49,7 +49,7 @@ export const API = {
49 delete_map_summary: (token: string, map_id: string, route_id: number) => delete_map_summary(token, map_id, route_id), 49 delete_map_summary: (token: string, map_id: string, route_id: number) => delete_map_summary(token, map_id, route_id),
50}; 50};
51 51
52const BASE_API_URL: string = "https://lp.portal2.sr/api/v1/" 52const BASE_API_URL: string = "/api/v1/"
53 53
54export function url(path: string): string { 54export function url(path: string): string {
55 return BASE_API_URL + path; 55 return BASE_API_URL + path;
diff --git a/frontend/src/components/Login.tsx b/frontend/src/components/Login.tsx
index 77236ce..a8c5503 100644
--- a/frontend/src/components/Login.tsx
+++ b/frontend/src/components/Login.tsx
@@ -17,8 +17,7 @@ const Login: React.FC<LoginProps> = ({ setToken, profile, setProfile }) => {
17 const navigate = useNavigate(); 17 const navigate = useNavigate();
18 18
19 const _login = () => { 19 const _login = () => {
20 setToken("eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE3MzExNjU1NTgsIm1vZCI6dHJ1ZSwic3ViIjoiNzY1NjExOTgxMzE2Mjk5ODkifQ.KoENjj6Z41-QQu1VKvgAiACsjLK7IoVWlJgrGdr6s24"); 20 window.location.href = "/api/v1/login";
21 // window.location.href = "/api/v1/login";
22 }; 21 };
23 22
24 const _logout = () => { 23 const _logout = () => {
diff --git a/frontend/src/components/UploadRunDialog.tsx b/frontend/src/components/UploadRunDialog.tsx
index 0476d6f..8081643 100644
--- a/frontend/src/components/UploadRunDialog.tsx
+++ b/frontend/src/components/UploadRunDialog.tsx
@@ -49,6 +49,35 @@ const UploadRunDialog: React.FC<UploadRunDialogProps> = ({ token, open, onClose,
49 49
50 const [loading, setLoading] = React.useState<boolean>(false); 50 const [loading, setLoading] = React.useState<boolean>(false);
51 51
52 const [dragHightlight, setDragHighlight] = React.useState<boolean>(false);
53 const fileInputRef = React.useRef<HTMLInputElement>(null);
54
55 const _handle_file_click = () => {
56 fileInputRef.current?.click();
57 }
58
59 const _handle_drag_over = (e: React.DragEvent<HTMLDivElement>) => {
60 e.preventDefault();
61 e.stopPropagation();
62 setDragHighlight(true);
63 }
64
65 const _handle_drag_leave = (e: React.DragEvent<HTMLDivElement>) => {
66 e.preventDefault();
67 e.stopPropagation();
68 setDragHighlight(false);
69 }
70
71 const _handle_drop = (e: React.DragEvent<HTMLDivElement>, host: boolean) => {
72 e.preventDefault();
73 e.stopPropagation();
74 setDragHighlight(true);
75
76 console.log(e.dataTransfer.files);
77
78 _handle_file_change(e.dataTransfer.files, host);
79 }
80
52 const _handle_dropdowns = (dropdown: number) => { 81 const _handle_dropdowns = (dropdown: number) => {
53 setDropdown1Vis(false); 82 setDropdown1Vis(false);
54 setDropdown2Vis(false); 83 setDropdown2Vis(false);
@@ -75,17 +104,18 @@ const UploadRunDialog: React.FC<UploadRunDialogProps> = ({ token, open, onClose,
75 setLoading(false); 104 setLoading(false);
76 }; 105 };
77 106
78 const _handle_file_change = async (e: React.ChangeEvent<HTMLInputElement>, host: boolean) => { 107 const _handle_file_change = async (files: FileList | null, host: boolean) => {
79 if (e.target.files) { 108 console.log(files);
109 if (files) {
80 if (host) { 110 if (host) {
81 setUploadRunContent({ 111 setUploadRunContent({
82 ...uploadRunContent, 112 ...uploadRunContent,
83 host_demo: e.target.files[0], 113 host_demo: files[0],
84 }); 114 });
85 } else { 115 } else {
86 setUploadRunContent({ 116 setUploadRunContent({
87 ...uploadRunContent, 117 ...uploadRunContent,
88 partner_demo: e.target.files[0], 118 partner_demo: files[0],
89 }); 119 });
90 } 120 }
91 } 121 }
@@ -130,7 +160,7 @@ const UploadRunDialog: React.FC<UploadRunDialogProps> = ({ token, open, onClose,
130 } 160 }
131 161
132 const response = await API.post_record(token, uploadRunContent); 162 const response = await API.post_record(token, uploadRunContent);
133 message("Message", response); 163 await message("Message", response);
134 // navigate(0); 164 // navigate(0);
135 onClose(); 165 onClose();
136 } 166 }
@@ -184,13 +214,26 @@ const UploadRunDialog: React.FC<UploadRunDialogProps> = ({ token, open, onClose,
184 </div> 214 </div>
185 </div> 215 </div>
186 <span>Host Demo</span> 216 <span>Host Demo</span>
187 <input type="file" name="host_demo" id="host_demo" accept=".dem" onChange={(e) => _handle_file_change(e, true)} /> 217 <div onClick={_handle_file_click} onDragOver={(e) => {_handle_drag_over(e)}} onDrop={(e) => {_handle_drop(e, true)}} onDragLeave={(e) => {_handle_drag_leave(e)}} className={`upload-run-drag-area ${dragHightlight ? "upload-run-drag-area-highlight" : ""} ${uploadRunContent.host_demo ? "upload-run-drag-area-hidden" : ""}`}>
218 <input ref={fileInputRef} type="file" name="host_demo" id="host_demo" accept=".dem" onChange={(e) => _handle_file_change(e.target.files, true)} />
219 {!uploadRunContent.host_demo ?
220 <div>
221 <span>Drag and drop</span>
222 <div>
223 <span>Or click here</span>
224 <button>Upload</button>
225 </div>
226 </div>
227 : null}
228
229 <span>{uploadRunContent.host_demo?.name}</span>
230 </div>
188 { 231 {
189 games[selectedGameID].is_coop && 232 games[selectedGameID].is_coop &&
190 ( 233 (
191 <> 234 <>
192 <span>Partner Demo</span> 235 <span>Partner Demo</span>
193 <input type="file" name="partner_demo" id="partner_demo" accept=".dem" onChange={(e) => _handle_file_change(e, false)} /> 236 <input type="file" name="partner_demo" id="partner_demo" accept=".dem" onChange={(e) => _handle_file_change(e.target.files, false)} />
194 </> 237 </>
195 ) 238 )
196 } 239 }
diff --git a/frontend/src/css/UploadRunDialog.css b/frontend/src/css/UploadRunDialog.css
index 6ea9884..b4667da 100644
--- a/frontend/src/css/UploadRunDialog.css
+++ b/frontend/src/css/UploadRunDialog.css
@@ -116,3 +116,33 @@ button:hover {
116 width: 100%; 116 width: 100%;
117} 117}
118 118
119#host_demo {
120 background-color: rgba(0, 0, 0, 0);
121 display: none;
122}
123
124.upload-run-drag-area {
125 border: 2px dashed grey;
126 border-radius: 10px;
127 height: 200px;
128 cursor: pointer;
129 width: 300px;
130 transition: all 0.2s ease;
131 text-align: center;
132 display: flex;
133 flex-direction: column;
134 align-items: center;
135 justify-content: center;
136}
137
138.upload-run-drag-area-highlight {
139 border: 2px dashed white;
140}
141
142.upload-run-drag-area-hidden {
143 height: fit-content;
144 text-align: left;
145 border: none;
146 align-items: flex-start;
147}
148
diff --git a/frontend/src/hooks/UseMessage.tsx b/frontend/src/hooks/UseMessage.tsx
index 249a3bf..ebc4276 100644
--- a/frontend/src/hooks/UseMessage.tsx
+++ b/frontend/src/hooks/UseMessage.tsx
@@ -3,6 +3,7 @@ import MessageDialog from "../components/MessageDialog";
3 3
4const useMessage = () => { 4const useMessage = () => {
5 const [isOpen, setIsOpen] = useState(false); 5 const [isOpen, setIsOpen] = useState(false);
6 const [resolvePromise, setResolvePromise] = useState<((value: boolean) => void) | null>(null);
6 7
7 const [title, setTitle] = useState<string>(""); 8 const [title, setTitle] = useState<string>("");
8 const [subtitle, setSubtitle] = useState<string>(""); 9 const [subtitle, setSubtitle] = useState<string>("");
@@ -11,6 +12,9 @@ const useMessage = () => {
11 setIsOpen(true); 12 setIsOpen(true);
12 setTitle(title); 13 setTitle(title);
13 setSubtitle(subtitle); 14 setSubtitle(subtitle);
15 return new Promise((resolve) => {
16 setResolvePromise(() => resolve);
17 });
14 }; 18 };
15 19
16 const handleClose = () => { 20 const handleClose = () => {