8000 Implement upvote toggle by laymonage · Pull Request #18 · giscus/giscus · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Implement upvote toggle #18

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 9, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 27 additions & 1 deletion components/Comment.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import { ArrowUpIcon, KebabHorizontalIcon } from '@primer/octicons-react';
import { ReactElement, ReactNode, useCallback, useState } from 'react';
import { ReactElement, ReactNode, useCallback, useContext, useState } from 'react';
import { handleCommentClick, processCommentBody } from '../lib/adapter';
import { IComment, IReply } from '../lib/types/adapter';
import { Reactions, updateCommentReaction } from '../lib/reactions';
import { formatDate, formatDateDistance } from '../lib/utils';
import { toggleUpvote } from '../services/github/toggleUpvote';
import CommentBox from './CommentBox';
import ReactButtons from './ReactButtons';
import Reply from './Reply';
import { AuthContext } from '../lib/context';

interface ICommentProps {
children?: ReactNode;
Expand All @@ -25,6 +27,7 @@ export default function Comment({
}: ICommentProps) {
const [page, setPage] = useState(0);
const replies = comment.replies.slice(0, page === 0 ? 3 : undefined);
const { token } = useContext(AuthContext);

const updateReactions = useCallback(
(reaction: Reactions, promise: Promise<unknown>) =>
Expand All @@ -34,6 +37,27 @@ export default function Comment({

const incrementPage = () => page < 1 && setPage(page + 1);

const upvote = useCallback(() => {
const upvoteCount = comment.viewerHasUpvoted
? comment.upvoteCount - 1
: comment.upvoteCount + 1;

const promise = toggleUpvote(
{ upvoteInput: { subjectId: comment.id } },
token,
comment.viewerHasUpvoted,
);

onCommentUpdate(
{
...comment,
upvoteCount,
viewerHasUpvoted: !comment.viewerHasUpvoted,
},
promise,
);
}, [comment, onCommentUpdate, token]);

const hidden = comment.deletedAt || comment.isMinimized;

return (
Expand All @@ -44,6 +68,8 @@ export default function Comment({
<button
type="button"
className={`${comment.viewerHasUpvoted ? 'color-text-link' : 'color-text-secondary'}`}
>
disabled={!token || !comment.viewerCanUpvote}
>
<ArrowUpIcon className="transform hover:translate-y-[-10%] transition-transform ease-in-out duration-150" />
</button>
Expand Down
1 change: 1 addition & 0 deletions lib/types/adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ export interface IReply extends IBaseComment {
export interface IComment extends IBaseComment {
upvoteCount: number;
viewerHasUpvoted: boolean;
viewerCanUpvote: boolean;
replyCount: number;
replies: IReply[];
}
Expand Down
1 change: 1 addition & 0 deletions lib/types/github.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ export interface GReply extends GBaseComment {
export interface GComment extends GBaseComment {
upvoteCount: number;
viewerHasUpvoted: boolean;
viewerCanUpvote: boolean;
replies: {
totalCount: number;
nodes: GReply[];
Expand Down
1 change: 1 addition & 0 deletions pages/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ export default function Home({ content }: { content: string }) {
url: 'https://github.com/laymonage/giscussions',
viewerDidAuthor: false,
viewerHasUpvoted: false,
viewerCanUpvote: false,
};

return (
Expand Down
1 change: 1 addition & 0 deletions services/github/addDiscussionComment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const ADD_DISCUSSION_COMMENT_QUERY = `
id
upvoteCount
viewerHasUpvoted
viewerCanUpvote
author {
avatarUrl
login
Expand Down
1 change: 1 addition & 0 deletions services/github/getDiscussion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ const GET_DISCUSSION_QUERY = `
id
upvoteCount
viewerHasUpvoted
viewerCanUpvote
author {
avatarUrl
login
Expand Down
40 changes: 40 additions & 0 deletions services/github/toggleUpvote.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
const TOGGLE_UPVOTE_QUERY = (mode: 'Add' | 'Remove') => `
mutation($upvoteInput: ${mode}UpvoteInput!) {
toggleUpvote: ${mode.toLowerCase()}Upvote(input: $upvoteInput) {
subject {
upvoteCount
}
}
}`;

export interface ToggleUpvoteBody {
upvoteInput: { subjectId: string };
}

export interface ToggleUpvoteResponse {
data: {
toggleUpvote: {
subject: {
upvoteCount: number;
};
};
};
}

export async function toggleUpvote(
params: ToggleUpvoteBody,
token: string,
viewerHasUpvoted: boolean,
): Promise<ToggleUpvoteResponse> {
return fetch('/api/graphql', {
method: 'POST',
headers: {
Authorization: `Bearer ${token}`,
'Content-Type': 'application/json',
},
body: JSON.stringify({
query: TOGGLE_UPVOTE_QUERY(viewerHasUpvoted ? 'Remove' : 'Add'),
variables: params,
}),
}).then((r) => r.json());
}
0