fix: Specify vote content type so buttons work for both comments and posts

This commit is contained in:
Jay Sitter 2023-06-23 12:14:24 -04:00
parent c8a05fb290
commit 41c9b3baf9
4 changed files with 51 additions and 12 deletions

View file

@ -52,6 +52,7 @@ import {
CommentNodeI, CommentNodeI,
CommentViewType, CommentViewType,
PurgeType, PurgeType,
VoteContentType,
} from "../../interfaces"; } from "../../interfaces";
import { mdToHtml, mdToHtmlNoImages } from "../../markdown"; import { mdToHtml, mdToHtmlNoImages } from "../../markdown";
import { I18NextService, UserService } from "../../services"; import { I18NextService, UserService } from "../../services";
@ -437,6 +438,7 @@ export class CommentNode extends Component<CommentNodeProps, CommentNodeState> {
{UserService.Instance.myUserInfo && !this.props.viewOnly && ( {UserService.Instance.myUserInfo && !this.props.viewOnly && (
<> <>
<VoteButtonsCompact <VoteButtonsCompact
voteContentType={VoteContentType.Comment}
id={this.commentView.comment.id} id={this.commentView.comment.id}
onVote={this.props.onCommentVote} onVote={this.props.onCommentVote}
enableDownvotes={this.props.enableDownvotes} enableDownvotes={this.props.enableDownvotes}

View file

@ -8,11 +8,12 @@ import {
CreatePostLike, CreatePostLike,
PostAggregates, PostAggregates,
} from "lemmy-js-client"; } from "lemmy-js-client";
import { VoteType } from "../../interfaces"; import { VoteContentType, VoteType } from "../../interfaces";
import { I18NextService } from "../../services"; import { I18NextService } from "../../services";
import { Icon, Spinner } from "../common/icon"; import { Icon, Spinner } from "../common/icon";
interface VoteButtonsProps { interface VoteButtonsProps {
voteContentType: VoteContentType;
id: number; id: number;
onVote: (i: CreateCommentLike | CreatePostLike) => void; onVote: (i: CreateCommentLike | CreatePostLike) => void;
enableDownvotes?: boolean; enableDownvotes?: boolean;
@ -46,21 +47,45 @@ const tippy = (counts: CommentAggregates | PostAggregates): string => {
const handleUpvote = (i: VoteButtons) => { const handleUpvote = (i: VoteButtons) => {
i.setState({ upvoteLoading: true }); i.setState({ upvoteLoading: true });
i.props.onVote({
id: i.props.id, switch (i.props.voteContentType) {
score: newVote(VoteType.Upvote, i.props.my_vote), case VoteContentType.Comment:
auth: myAuthRequired(), 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(),
});
}
i.setState({ upvoteLoading: false }); i.setState({ upvoteLoading: false });
}; };
const handleDownvote = (i: VoteButtons) => { const handleDownvote = (i: VoteButtons) => {
i.setState({ downvoteLoading: true }); i.setState({ downvoteLoading: true });
i.props.onVote({ switch (i.props.voteContentType) {
id: i.props.id, case VoteContentType.Comment:
score: newVote(VoteType.Downvote, i.props.my_vote), i.props.onVote({
auth: myAuthRequired(), 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(),
});
}
i.setState({ downvoteLoading: false }); i.setState({ downvoteLoading: false });
}; };

View file

@ -43,7 +43,12 @@ import {
TransferCommunity, TransferCommunity,
} from "lemmy-js-client"; } from "lemmy-js-client";
import { relTags } from "../../config"; import { relTags } from "../../config";
import { BanType, PostFormParams, PurgeType } from "../../interfaces"; import {
BanType,
PostFormParams,
PurgeType,
VoteContentType,
} from "../../interfaces";
import { mdNoImages, mdToHtml, mdToHtmlInline } from "../../markdown"; import { mdNoImages, mdToHtml, mdToHtmlInline } from "../../markdown";
import { I18NextService, UserService } from "../../services"; import { I18NextService, UserService } from "../../services";
import { setupTippy } from "../../tippy"; import { setupTippy } from "../../tippy";
@ -598,6 +603,7 @@ export class PostListing extends Component<PostListingProps, PostListingState> {
)} )}
{mobile && !this.props.viewOnly && ( {mobile && !this.props.viewOnly && (
<VoteButtonsCompact <VoteButtonsCompact
voteContentType={VoteContentType.Post}
id={this.postView.post.id} id={this.postView.post.id}
onVote={this.props.onPostVote} onVote={this.props.onPostVote}
enableDownvotes={this.props.enableDownvotes} enableDownvotes={this.props.enableDownvotes}
@ -1380,6 +1386,7 @@ export class PostListing extends Component<PostListingProps, PostListingState> {
<article className="row post-container"> <article className="row post-container">
{!this.props.viewOnly && ( {!this.props.viewOnly && (
<VoteButtons <VoteButtons
voteContentType={VoteContentType.Post}
id={this.postView.post.id} id={this.postView.post.id}
onVote={this.props.onPostVote} onVote={this.props.onPostVote}
enableDownvotes={this.props.enableDownvotes} enableDownvotes={this.props.enableDownvotes}

View file

@ -77,6 +77,11 @@ export enum VoteType {
Downvote, Downvote,
} }
export enum VoteContentType {
Post,
Comment,
}
export interface CommentNodeI { export interface CommentNodeI {
comment_view: CommentView; comment_view: CommentView;
children: Array<CommentNodeI>; children: Array<CommentNodeI>;