+
+
+ {error &&
{error}
}
);
}
diff --git a/src/components/skills/SkillVoteButton.tsx b/src/components/skills/SkillVoteButton.tsx
index a92ceaa1..95816038 100644
--- a/src/components/skills/SkillVoteButton.tsx
+++ b/src/components/skills/SkillVoteButton.tsx
@@ -23,9 +23,11 @@ export function SkillVoteButton({
const [score, setScore] = useState(initialScore);
const [userVote, setUserVote] = useState
(initialUserVote);
const [loading, setLoading] = useState(false);
+ const [error, setError] = useState(null);
async function handleVote(voteType: 1 | -1) {
setLoading(true);
+ setError(null);
try {
const res = await fetch(`/api/skills/${slug}/vote`, {
method: "POST",
@@ -33,22 +35,29 @@ export function SkillVoteButton({
body: JSON.stringify({ vote_type: voteType }),
});
- if (res.ok) {
- const data = await res.json();
- setUpvotes(data.upvotes);
- setDownvotes(data.downvotes);
- setScore(data.score);
- setUserVote(data.user_vote);
+ if (!res.ok) {
+ const data = await res.json().catch(() => ({}));
+ setError(data.error || "Failed to update vote");
+ return;
}
+
+ const data = await res.json();
+ setUpvotes(data.upvotes);
+ setDownvotes(data.downvotes);
+ setScore(data.score);
+ setUserVote(data.user_vote);
} catch {
- // silently fail
+ setError("Failed to update vote");
+ } finally {
+ setLoading(false);
}
- setLoading(false);
}
return (
-
+
+
handleVote(1)}
disabled={loading}
className={`p-1.5 rounded-md transition-colors ${
@@ -66,6 +75,7 @@ export function SkillVoteButton({
{score}
handleVote(-1)}
disabled={loading}
className={`p-1.5 rounded-md transition-colors ${
@@ -77,6 +87,8 @@ export function SkillVoteButton({
>
+
+ {error &&
{error}
}
);
}