import { myAuthRequired, newVote, showScores } from "@utils/app"; import { numToSI } from "@utils/helpers"; import classNames from "classnames"; import { Component, linkEvent } from "inferno"; import { CommentAggregates, CreateCommentLike, CreatePostLike, PostAggregates, } from "lemmy-js-client"; import { VoteContentType, VoteType } from "../../interfaces"; import { I18NextService } from "../../services"; import { Icon, Spinner } from "../common/icon"; interface VoteButtonsProps { voteContentType: VoteContentType; id: number; onVote: (i: CreateCommentLike | CreatePostLike) => void; enableDownvotes?: boolean; counts: CommentAggregates | PostAggregates; my_vote?: number; } interface VoteButtonsState { upvoteLoading: boolean; downvoteLoading: boolean; } const tippy = (counts: CommentAggregates | PostAggregates): string => { const points = I18NextService.i18n.t("number_of_points", { count: Number(counts.score), formattedCount: Number(counts.score), }); const upvotes = I18NextService.i18n.t("number_of_upvotes", { count: Number(counts.upvotes), formattedCount: Number(counts.upvotes), }); const downvotes = I18NextService.i18n.t("number_of_downvotes", { count: Number(counts.downvotes), formattedCount: Number(counts.downvotes), }); return `${points} • ${upvotes} • ${downvotes}`; }; const handleUpvote = (i: VoteButtons) => { i.setState({ upvoteLoading: true }); switch (i.props.voteContentType) { case VoteContentType.Comment: i.props.onVote({ comment_id: i.props.id, score: newVote(VoteType.Upvote, i.props.my_vote), auth: myAuthRequired(), }); break; case VoteContentType.Post: default: i.props.onVote({ post_id: i.props.id, score: newVote(VoteType.Upvote, i.props.my_vote), auth: myAuthRequired(), }); } }; const handleDownvote = (i: VoteButtons) => { i.setState({ downvoteLoading: true }); switch (i.props.voteContentType) { case VoteContentType.Comment: i.props.onVote({ comment_id: i.props.id, score: newVote(VoteType.Downvote, i.props.my_vote), auth: myAuthRequired(), }); break; case VoteContentType.Post: default: i.props.onVote({ post_id: i.props.id, score: newVote(VoteType.Downvote, i.props.my_vote), auth: myAuthRequired(), }); } }; export class VoteButtonsCompact extends Component< VoteButtonsProps, VoteButtonsState > { state: VoteButtonsState = { upvoteLoading: false, downvoteLoading: false, }; constructor(props: any, context: any) { super(props, context); } componentWillReceiveProps(nextProps: VoteButtonsProps) { if (this.props !== nextProps) { this.setState({ upvoteLoading: false, downvoteLoading: false, }); } } render() { return ( <> {this.props.enableDownvotes && ( )} ); } } export class VoteButtons extends Component { state: VoteButtonsState = { upvoteLoading: false, downvoteLoading: false, }; constructor(props: any, context: any) { super(props, context); } componentWillReceiveProps(nextProps: VoteButtonsProps) { if (this.props !== nextProps) { this.setState({ upvoteLoading: false, downvoteLoading: false, }); } } render() { return (
{showScores() ? (
{numToSI(this.props.counts.score)}
) : (
)} {this.props.enableDownvotes && ( )}
); } }