Skip to content

Commit

Permalink
upvote and downvote from comment context menu
Browse files Browse the repository at this point in the history
  • Loading branch information
sgriff96 committed Aug 6, 2023
1 parent 0519531 commit c6cb1c2
Showing 1 changed file with 34 additions and 1 deletion.
35 changes: 34 additions & 1 deletion src/hooks/comments/useComment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ import { useTranslation } from "react-i18next";
import { produce } from "immer";
import { useRoute } from "@react-navigation/core";
import setCollapsed from "@src/stores/posts/actions/setCollapsed";
import setPostCommentVote from "@root/src/stores/posts/actions/setPostCommentVote";
import { determineVotes } from "@root/src/helpers/VoteHelper";
import { ILemmyVote } from "@root/src/types/lemmy/ILemmyVote";
import { useAppDispatch, useAppSelector } from "../../../store";
import { selectSite, setUnread } from "../../slices/site/siteSlice";
import ILemmyComment from "../../types/lemmy/ILemmyComment";
Expand Down Expand Up @@ -58,6 +61,8 @@ const useComment = ({ comment }: { comment: ILemmyComment }): UseComment => {

const basicOptions = useMemo<ContextMenuOption[]>(
() => [
{ key: "upvote", title: t("Upvote"), icon: ICON_MAP.UPVOTE },
{ key: "downvote", title: t("Downvote"), icon: ICON_MAP.DOWNVOTE },
{ key: "copy_text", title: t("Copy Text"), icon: ICON_MAP.COPY },
{ key: "copy_link", title: t("Copy Link"), icon: ICON_MAP.LINK },
{ key: "save", title: t("Save"), icon: ICON_MAP.SAVE },
Expand Down Expand Up @@ -93,6 +98,34 @@ const useComment = ({ comment }: { comment: ILemmyComment }): UseComment => {
async (selection: string) => {
onGenericHapticFeedback();

if (selection === "upvote") {
const newValues = determineVotes(
comment.comment.my_vote === 1 ? 0 : 1,
comment.comment.my_vote as ILemmyVote,
comment.comment.counts.upvotes,
comment.comment.counts.downvotes
);
setPostCommentVote(
postKey,
comment.comment.comment.id,
newValues
).then();
}

if (selection === "downvote") {
const newValues = determineVotes(
comment.comment.my_vote === -1 ? 0 : -1,
comment.comment.my_vote as ILemmyVote,
comment.comment.counts.upvotes,
comment.comment.counts.downvotes
);
setPostCommentVote(
postKey,
comment.comment.comment.id,
newValues
).then();
}

if (selection === "copy_text") {
Clipboard.setString(comment.comment.comment.content);
dispatch(
Expand Down Expand Up @@ -210,7 +243,7 @@ const useComment = ({ comment }: { comment: ILemmyComment }): UseComment => {
onSave().then();
}
},
[comment.comment.comment.id]
[comment.comment.comment.id, comment.comment.my_vote]
);

const onReply = useCallback(() => {
Expand Down

0 comments on commit c6cb1c2

Please sign in to comment.