add download functionality

This commit is contained in:
afazio1
2024-04-22 02:27:34 -04:00
parent 7467092e0f
commit 9fe99e0d96
3 changed files with 38 additions and 5 deletions

View File

@@ -7,7 +7,7 @@ export const usePoll = () => {
.catch((error) => {
console.error('Error:', error);
});
}, 30 * 60000) // poll every 30 minutes
}, 30 * 60000) // poll every 30 minutes
return () => {
// clean up
clearInterval(interval);
@@ -16,7 +16,7 @@ export const usePoll = () => {
const pollServer = async () => {
// request private ip from local api
return fetch('http://127.0.0.1:8080/ip')
return fetch('http://localhost:8080/ip')
.then(response => response.text())
.then((data) => {
// send private ip to central server

View File

@@ -72,7 +72,7 @@ const Results: React.FC = () => {
<td>{file.upvotes.length}</td>
<td>{file.downvotes.length}</td>
<td>{file.original_author.username}</td>
<td><a href="">Download</a></td>
<td><DownloadButton file={file} /></td>
</tr>
))}
</tbody>
@@ -86,3 +86,36 @@ const Results: React.FC = () => {
};
export default Results;
function DownloadButton({ file }: { file: File }) {
const [success, setSuccess] = useState(false);
const handleDownload = () => {
// Implement download functionality
fetch("http://localhost:8080/request", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({ id: file.id, filename: file.filename, ip: file.original_author.ip_address }),
})
.then((response) => response.text())
.then((data) => {
console.log("Success download response data:", data);
setSuccess(true);
})
.catch((error) => {
console.error("Error:", error);
setSuccess(false);
});
};
return (
<>
{
success === true ?
<p>Downloaded!</p> :
<button onClick={handleDownload}>Download</button>
}
</>
);
}