From 8559b615734760ff060ac2c714c8fca80d5ed251 Mon Sep 17 00:00:00 2001 From: Pinapelz Date: Mon, 30 Jun 2025 00:59:25 -0700 Subject: add score import page --- frontend/src/components/modals/JsonUploadModal.tsx | 161 +++++++++++++++++++++ 1 file changed, 161 insertions(+) create mode 100644 frontend/src/components/modals/JsonUploadModal.tsx (limited to 'frontend/src/components') diff --git a/frontend/src/components/modals/JsonUploadModal.tsx b/frontend/src/components/modals/JsonUploadModal.tsx new file mode 100644 index 0000000..1cb541b --- /dev/null +++ b/frontend/src/components/modals/JsonUploadModal.tsx @@ -0,0 +1,161 @@ +import { useState, useRef } from 'react'; + +interface JsonUploadModalProps { + isOpen: boolean; + onClose: () => void; + onUpload: (data: any) => void; + game: string; +} + +const JsonUploadModal = ({ isOpen, onClose, onUpload, game }: JsonUploadModalProps) => { + const [file, setFile] = useState(null); + const [error, setError] = useState(''); + const [isLoading, setIsLoading] = useState(false); + const fileInputRef = useRef(null); + + if (!isOpen) return null; + + const handleFileChange = (e: React.ChangeEvent) => { + const selectedFile = e.target.files?.[0]; + setError(''); + + if (selectedFile) { + if (!selectedFile.name.endsWith('.json')) { + setError('Please select a JSON file'); + return; + } + setFile(selectedFile); + } + }; + + const handleUpload = async () => { + if (!file) { + setError('Please select a file to upload'); + return; + } + + setIsLoading(true); + setError(''); + + try { + const text = await file.text(); + const data = JSON.parse(text); + onUpload(data); + setFile(null); + if (fileInputRef.current) { + fileInputRef.current.value = ''; + } + onClose(); + } catch (err) { + if (err instanceof SyntaxError) { + setError('Invalid JSON file format'); + } else { + setError('Failed to process file. Please try again.'); + } + } finally { + setIsLoading(false); + } + }; + + const handleClose = () => { + setFile(null); + setError(''); + if (fileInputRef.current) { + fileInputRef.current.value = ''; + } + onClose(); + }; + + return ( +
+ {/* Backdrop */} +
+ + {/* Modal */} +
+
+ {/* Header */} +
+

Import {game} Data

+

Upload your game data in JSON format

+
+ + {/* File Upload Area */} +
+ +
+ + {/* Error Message */} + {error && ( +
+

{error}

+
+ )} + + {/* Actions */} +
+ + +
+
+
+
+ ); +}; + +export default JsonUploadModal; -- cgit v1.2.3