Skip to content
Open
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
39 changes: 39 additions & 0 deletions src/components/UpvoteButton.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import React from 'react';
import { Button, message } from 'antd';
import { LikeOutlined } from '@ant-design/icons';
import { useMutation } from '@apollo/react-hooks';
import gql from 'graphql-tag';

const UPVOTE_PATH = gql`
mutation UpvotePath($pathId: Int!, $userId: String!) {
update_learning_path_by_pk(pk_columns: {id: $pathId}, _append: {voted_by: [ $userId ]}) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Target the correct Hasura mutation and vote storage model

This mutation uses update_learning_path_by_pk with _append on voted_by, but the rest of this codebase consistently targets Learning_Paths plus a separate votes relation (for example, src/components/EditPath/editPath.js mutates update_Learning_Paths and src/pages/search.js reads votes/votes_aggregate), so this operation does not match the existing schema shape and upvote requests will fail at runtime when invoked.

Useful? React with 👍 / 👎.

id
voted_by
}
}
`;

const UpvoteButton = ({ pathId, userId, votedBy = [] }) => {
const [upvote] = useMutation(UPVOTE_PATH);

const handleUpvote = async () => {
if (votedBy.includes(userId)) {
message.info('You have already upvoted this path!');
Comment on lines +20 to +21

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Block repeated upvotes until local vote state is refreshed

The duplicate guard checks only the incoming votedBy prop, but this component never updates local state after a successful mutation; if the user clicks again before parent data refreshes, the check still passes and another upvote mutation is sent, which can record duplicate votes for the same user and inflate counts.

Useful? React with 👍 / 👎.

return;
}
try {
await upvote({ variables: { pathId, userId } });
message.success('Upvoted successfully!');
} catch (err) {
message.error('Failed to upvote');
}
};

return (
<Button icon={<LikeOutlined />} onClick={handleUpvote}>
{votedBy.length} Upvotes
</Button>
);
};

export default UpvoteButton;