-
Notifications
You must be signed in to change notification settings - Fork 90
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(@kampus/kampus): add post upvote mutations (#632)
- Loading branch information
Showing
10 changed files
with
598 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 18 additions & 4 deletions
22
apps/kampus/app/pano/__generated__/PanoFeedPaginationQuery.graphql.ts
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
22 changes: 18 additions & 4 deletions
22
apps/kampus/app/pano/__generated__/PostListContainerQuery.graphql.ts
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
80 changes: 71 additions & 9 deletions
80
apps/kampus/app/pano/features/post-list/PostUpvoteButton.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,86 @@ | ||
import { Triangle } from "lucide-react"; | ||
import { graphql, useFragment, useMutation } from "react-relay"; | ||
|
||
import { Button } from "@kampus/ui-next"; | ||
import { cn } from "@kampus/ui-next/utils"; | ||
|
||
import { type PostUpvoteButton_post$key } from "./__generated__/PostUpvoteButton_post.graphql"; | ||
|
||
const fragment = graphql` | ||
fragment PostUpvoteButton_post on PanoPost { | ||
id | ||
isUpvotedByViewer | ||
upvoteCount @required(action: LOG) | ||
} | ||
`; | ||
|
||
const createUpvoteMutation = graphql` | ||
mutation PostUpvoteButtonCreateMutation($postID: ID!) { | ||
createPanoUpvote(input: { id: $postID }) { | ||
node { | ||
node { | ||
...PostUpvoteButton_post | ||
} | ||
} | ||
} | ||
} | ||
`; | ||
|
||
const removeUpvoteMutation = graphql` | ||
mutation PostUpvoteButtonRemoveMutation($postID: ID!) { | ||
removePanoUpvote(input: { id: $postID }) { | ||
node { | ||
node { | ||
...PostUpvoteButton_post | ||
} | ||
} | ||
} | ||
} | ||
`; | ||
|
||
interface UpvoteProps { | ||
isUpvoted: boolean; | ||
upvoteCount: number; | ||
isVoting: boolean; | ||
disabled?: boolean; | ||
postRef: PostUpvoteButton_post$key; | ||
} | ||
|
||
export const UpvoteButton = (props: UpvoteProps) => { | ||
const upvoteStyle = props.isUpvoted ? "fill-primary" : "fill-none"; | ||
const opacity = props.isVoting ? "opacity-50" : "opacity-100"; | ||
const combinedStyle = cn(upvoteStyle, opacity); | ||
const post = useFragment(fragment, props.postRef); | ||
const [createUpvote, isCreating] = useMutation(createUpvoteMutation); | ||
const [removeUpvote, isRemoving] = useMutation(removeUpvoteMutation); | ||
|
||
if (!post) { | ||
return null; | ||
} | ||
|
||
const disabled = isCreating || isRemoving; | ||
|
||
const upvoteStyle = post?.isUpvotedByViewer ? "fill-primary" : "fill-none"; | ||
const combinedStyle = cn(upvoteStyle); | ||
|
||
const onClick = () => { | ||
if (!post) { | ||
return; | ||
} | ||
|
||
if (isCreating || isRemoving) { | ||
return; | ||
} | ||
|
||
if (post.isUpvotedByViewer) { | ||
removeUpvote({ variables: { postID: post.id } }); | ||
} else { | ||
createUpvote({ variables: { postID: post.id } }); | ||
} | ||
}; | ||
|
||
return ( | ||
<Button className="flex h-full flex-col" variant="outline"> | ||
<Button | ||
onClick={onClick} | ||
disabled={disabled} | ||
className="flex h-full flex-col" | ||
variant="outline" | ||
> | ||
<Triangle className={combinedStyle} size={12} /> | ||
{props.upvoteCount} | ||
{post?.upvoteCount ?? 0} | ||
</Button> | ||
); | ||
}; |
56 changes: 36 additions & 20 deletions
56
apps/kampus/app/pano/features/post-list/__generated__/PostItem_post.graphql.ts
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.