import { useState, useEffect, useRef } from "react"; import { observer } from "mobx-react-lite"; import { useRouter } from "next/router"; // lib import { useMobxStore } from "lib/mobx/store-provider"; export const IssueVotes: React.FC = observer(() => { const [isSubmitting, setIsSubmitting] = useState(false); const router = useRouter(); const { workspace_slug, project_slug } = router.query as { workspace_slug: string; project_slug: string }; const { user: userStore, issue: issueStore } = useMobxStore(); const user = userStore?.currentUser; const issueId = issueStore.activePeekOverviewIssueId; const votes = issueId ? issueStore.issue_detail[issueId]?.votes : []; const upVoteCount = votes?.filter((vote) => vote.vote === 1).length || 0; const downVoteCount = votes?.filter((vote) => vote.vote === -1).length || 0; const isUpVotedByUser = votes?.some((vote) => vote.actor === user?.id && vote.vote === 1); const isDownVotedByUser = votes?.some((vote) => vote.actor === user?.id && vote.vote === -1); const handleVote = async (e: any, voteValue: 1 | -1) => { if (!workspace_slug || !project_slug || !issueId) return; setIsSubmitting(true); const actionPerformed = votes?.find((vote) => vote.actor === user?.id && vote.vote === voteValue); if (actionPerformed) await issueStore.deleteIssueVoteAsync(workspace_slug, project_slug, issueId); else await issueStore.createIssueVoteAsync(workspace_slug, project_slug, issueId, { vote: voteValue, }); setIsSubmitting(false); }; useEffect(() => { if (user) return; userStore.getUserAsync(); }, [user, userStore]); return (
{/* upvote button 👇 */} {/* downvote button 👇 */}
); });