From 6195eae6865f6242512a055c44065c37599fcdaf Mon Sep 17 00:00:00 2001 From: Sahej Panag Date: Mon, 22 Apr 2024 13:46:55 -0400 Subject: [PATCH] upvote and downvote --- frontend/src/screens/Results.tsx | 47 ++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/frontend/src/screens/Results.tsx b/frontend/src/screens/Results.tsx index 2bc6a72..9670cdd 100644 --- a/frontend/src/screens/Results.tsx +++ b/frontend/src/screens/Results.tsx @@ -61,6 +61,8 @@ const Results: React.FC = () => { Downvotes Original Author Download + Upvote + Downvote @@ -74,6 +76,8 @@ const Results: React.FC = () => { {file.downvotes.length} {file.original_author.username} + + ))} @@ -119,4 +123,47 @@ function DownloadButton({ file }: { file: File }) { } ); +} + +function UpvoteButton({ file, onUpdate }: { file: File, onUpdate: () => void }) { + const [upvoted, setUpvoted] = useState(false); + + const handleUpvote = async () => { + try { + await fetch(`http://localhost:8000/api/files/${file.id}/upvote/`, { + method: 'POST', + headers: getAuthHeaders(), + }); + setUpvoted(true); + window.location.reload(); + onUpdate(); + } catch (error) { + console.error('Error:', error); + } + }; + return ( + + ); +} + +function DownvoteButton({ file, onUpdate }: { file: File, onUpdate: () => void }) { + const [downvoted, setDownvoted] = useState(false); + + const handleDownvote = async () => { + try { + await fetch(`http://localhost:8000/api/files/${file.id}/downvote/`, { + method: 'POST', + headers: getAuthHeaders(), + }); + setDownvoted(true); + window.location.reload(); + onUpdate(); + } catch (error) { + console.error('Error:', error); + } + }; + + return ( + + ); } \ No newline at end of file