-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
131 additions
and
95 deletions.
There are no files selected for viewing
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
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,89 @@ | ||
import { useState } from "react"; | ||
import { FaArrowDown, FaArrowUp } from "react-icons/fa"; | ||
|
||
import { useAlertQueue } from "@/hooks/useAlertQueue"; | ||
import { useAuthentication } from "@/hooks/useAuth"; | ||
|
||
interface Props { | ||
listingId: string; | ||
userVote: boolean | null; | ||
} | ||
|
||
const ListingVote = ({ listingId, userVote: initialUserVote }: Props) => { | ||
const auth = useAuthentication(); | ||
const [voting, setVoting] = useState(false); | ||
const [userVote, setUserVote] = useState(initialUserVote); | ||
const { addErrorAlert, addAlert } = useAlertQueue(); | ||
|
||
const handleVote = async (vote: boolean) => { | ||
if (!auth.isAuthenticated) { | ||
addErrorAlert("You must be logged in to vote"); | ||
return; | ||
} | ||
|
||
setVoting(true); | ||
try { | ||
if (userVote === vote) { | ||
const response = await auth.client.DELETE("/listings/{id}/vote", { | ||
params: { | ||
path: { | ||
id: listingId, | ||
}, | ||
}, | ||
}); | ||
if (response.error) { | ||
addErrorAlert(response.error); | ||
} else { | ||
addAlert("Vote removed", "success"); | ||
setUserVote(null); | ||
} | ||
} else { | ||
const response = await auth.client.POST("/listings/{id}/vote", { | ||
params: { | ||
path: { | ||
id: listingId, | ||
}, | ||
query: { | ||
upvote: vote, | ||
}, | ||
}, | ||
}); | ||
if (response.error) { | ||
addErrorAlert(response.error); | ||
} else { | ||
addAlert("Vote added", "success"); | ||
setUserVote(vote); | ||
} | ||
} | ||
} catch (error) { | ||
addErrorAlert(error); | ||
} finally { | ||
setVoting(false); | ||
} | ||
}; | ||
|
||
return ( | ||
<div className="flex items-center gap-2 text-gray-600"> | ||
<button | ||
className={`p-1 hover:bg-gray-100 rounded ${ | ||
userVote === true && !voting ? "text-green-500" : "" | ||
}`} | ||
onClick={() => handleVote(true)} | ||
disabled={voting || !auth.isAuthenticated} | ||
> | ||
<FaArrowUp /> | ||
</button> | ||
<button | ||
className={`p-1 hover:bg-gray-100 rounded ${ | ||
userVote === false && !voting ? "text-red-500" : "" | ||
}`} | ||
onClick={() => handleVote(false)} | ||
disabled={voting || !auth.isAuthenticated} | ||
> | ||
<FaArrowDown /> | ||
</button> | ||
</div> | ||
); | ||
}; | ||
|
||
export default ListingVote; |
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