-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #38 from hytech-racing/file_upload
File upload
- Loading branch information
Showing
10 changed files
with
222 additions
and
33 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
import React, { useState } from 'react'; | ||
import { Modal, Button, Notification, FileInput } from '@mantine/core'; | ||
import '@/css/FileUpload.css'; | ||
|
||
interface FileUploadProps { | ||
uploadUrl: string; | ||
} | ||
|
||
const FileUpload: React.FC<FileUploadProps> = ({ uploadUrl }) => { | ||
const [showModal, setShowModal] = useState(false); | ||
const [selectedFiles, setSelectedFiles] = useState<File[]>([]); | ||
const [error, setError] = useState<string | null>(null); | ||
|
||
const handleFileChange = (files: File[]) => { | ||
if (files.length > 0) { | ||
setSelectedFiles((prevFiles) => [...prevFiles, ...files]); | ||
} | ||
}; | ||
|
||
const handleUpload = async () => { | ||
if (selectedFiles.length > 0) { | ||
const formData = new FormData(); | ||
selectedFiles.forEach((file) => { | ||
formData.append('files', file); | ||
}); | ||
|
||
try { | ||
const response = await fetch(uploadUrl, { | ||
method: 'POST', | ||
body: formData, | ||
}); | ||
|
||
if (!response.ok) { | ||
setError('Upload failed. Network response was not ok.'); | ||
return; | ||
} | ||
|
||
const data = await response.json(); | ||
console.log('Upload successful:', data); | ||
|
||
setSelectedFiles([]); | ||
handleClose(); | ||
} catch (error) { | ||
console.error('Upload failed:', error); | ||
setError('An error occurred while uploading. Please try again.'); | ||
} | ||
} else { | ||
setError('Please select files to upload.'); | ||
} | ||
}; | ||
|
||
const toggleModal = () => { | ||
setShowModal(!showModal); | ||
}; | ||
|
||
const handleClose = () => { | ||
setShowModal(false); | ||
setSelectedFiles([]); | ||
setError(null); | ||
}; | ||
|
||
return ( | ||
<div> | ||
<Button onClick={toggleModal}>Upload Files</Button> | ||
|
||
<Modal | ||
opened={showModal} | ||
onClose={handleClose} | ||
title="Select files to upload" | ||
centered | ||
style={{ textAlign: "center" }} | ||
> | ||
<div className="files"> | ||
<FileInput | ||
multiple | ||
accept=".mcap" | ||
onChange={handleFileChange} | ||
placeholder="Select files to upload" | ||
label="Choose files" | ||
style={{ display: 'block', margin: '0 auto' }} | ||
/> | ||
{selectedFiles.length > 0 && ( | ||
<div> | ||
<h3>Chosen Files:</h3> | ||
<ul> | ||
{selectedFiles.map((file, index) => ( | ||
<li key={index}>{file.name}</li> | ||
))} | ||
</ul> | ||
</div> | ||
)} | ||
</div> | ||
|
||
<Button onClick={handleUpload} style={{ marginTop: 10 }}>Upload</Button> | ||
|
||
{error && ( | ||
<Notification color="red" onClose={() => setError(null)} style={{ marginTop: 10 }}> | ||
{error} | ||
</Notification> | ||
)} | ||
</Modal> | ||
</div> | ||
); | ||
}; | ||
|
||
export default FileUpload; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
button { | ||
padding: 10px 10px; | ||
margin: 3px 3px; | ||
border: none; | ||
border-radius: 5px; | ||
background-color: #b39e63; | ||
color: white; | ||
cursor: pointer; | ||
transition: background-color 0.3s; | ||
} | ||
|
||
button:hover { | ||
background-color: #d4bc79; | ||
} | ||
|
||
ul { | ||
list-style-type: none; | ||
padding: 0; | ||
} | ||
|
||
h3 { | ||
margin-top: 20px; | ||
} | ||
|
||
.files { | ||
display: flex; | ||
flex-direction: column; | ||
text-align: center; | ||
justify-content: center; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters