Skip to content

Commit

Permalink
vote-status
Browse files Browse the repository at this point in the history
  • Loading branch information
benawad committed Aug 15, 2020
1 parent c8a23f9 commit 36da2e6
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 8 deletions.
5 changes: 4 additions & 1 deletion server/src/entities/Post.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ObjectType, Field } from "type-graphql";
import { ObjectType, Field, Int } from "type-graphql";
import {
Entity,
Column,
Expand Down Expand Up @@ -31,6 +31,9 @@ export class Post extends BaseEntity {
@Column({ type: "int", default: 0 })
points!: number;

@Field(() => Int, { nullable: true })
voteStatus: number | null; // 1 or -1 or null

@Field()
@Column()
creatorId: number;
Expand Down
14 changes: 10 additions & 4 deletions server/src/resolvers/post.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,13 +105,14 @@ export class PostResolver {
@Query(() => PaginatedPosts)
async posts(
@Arg("limit", () => Int) limit: number,
@Arg("cursor", () => String, { nullable: true }) cursor: string | null
@Arg("cursor", () => String, { nullable: true }) cursor: string | null,
@Ctx() { req }: MyContext
): Promise<PaginatedPosts> {
// 20 -> 21
const realLimit = Math.min(50, limit);
const reaLimitPlusOne = realLimit + 1;

const replacements: any[] = [reaLimitPlusOne];
const replacements: any[] = [reaLimitPlusOne, req.session.userId];

if (cursor) {
replacements.push(new Date(parseInt(cursor)));
Expand All @@ -126,10 +127,15 @@ export class PostResolver {
'email', u.email,
'createdAt', u."createdAt",
'updatedAt', u."updatedAt"
) creator
) creator,
${
req.session.userId
? '(select value from updoot where "userId" = $2 and "postId" = p.id) "voteStatus"'
: 'null as "voteStatus"'
}
from post p
inner join public.user u on u.id = p."creatorId"
${cursor ? `where p."createdAt" < $2` : ""}
${cursor ? `where p."createdAt" < $3` : ""}
order by p."createdAt" DESC
limit $1
`,
Expand Down
8 changes: 8 additions & 0 deletions web/src/components/UpdootSection.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,27 +15,35 @@ export const UpdootSection: React.FC<UpdootSectionProps> = ({ post }) => {
<Flex direction="column" justifyContent="center" alignItems="center" mr={4}>
<IconButton
onClick={async () => {
if (post.voteStatus === 1) {
return;
}
setLoadingState("updoot-loading");
await vote({
postId: post.id,
value: 1,
});
setLoadingState("not-loading");
}}
variantColor={post.voteStatus === 1 ? "green" : undefined}
isLoading={loadingState === "updoot-loading"}
aria-label="updoot post"
icon="chevron-up"
/>
{post.points}
<IconButton
onClick={async () => {
if (post.voteStatus === -1) {
return;
}
setLoadingState("downdoot-loading");
await vote({
postId: post.id,
value: -1,
});
setLoadingState("not-loading");
}}
variantColor={post.voteStatus === -1 ? "red" : undefined}
isLoading={loadingState === "downdoot-loading"}
aria-label="downdoot post"
icon="chevron-down"
Expand Down
4 changes: 3 additions & 1 deletion web/src/generated/graphql.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ export type Post = {
title: Scalars['String'];
text: Scalars['String'];
points: Scalars['Float'];
voteStatus?: Maybe<Scalars['Int']>;
creatorId: Scalars['Float'];
creator: User;
createdAt: Scalars['String'];
Expand Down Expand Up @@ -141,7 +142,7 @@ export type UsernamePasswordInput = {

export type PostSnippetFragment = (
{ __typename?: 'Post' }
& Pick<Post, 'id' | 'createdAt' | 'updatedAt' | 'title' | 'points' | 'textSnippet'>
& Pick<Post, 'id' | 'createdAt' | 'updatedAt' | 'title' | 'points' | 'textSnippet' | 'voteStatus'>
& { creator: (
{ __typename?: 'User' }
& Pick<User, 'id' | 'username'>
Expand Down Expand Up @@ -289,6 +290,7 @@ export const PostSnippetFragmentDoc = gql`
title
points
textSnippet
voteStatus
creator {
id
username
Expand Down
1 change: 1 addition & 0 deletions web/src/graphql/fragments/PostSnippet.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ fragment PostSnippet on Post {
title
points
textSnippet
voteStatus
creator {
id
username
Expand Down
10 changes: 8 additions & 2 deletions web/src/utils/createUrqlClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,20 +142,26 @@ export const createUrqlClient = (ssrExchange: any) => ({
fragment _ on Post {
id
points
voteStatus
}
`,
{ id: postId } as any
);

if (data) {
const newPoints = (data.points as number) + value;
if (data.voteStatus === value) {
return;
}
const newPoints =
(data.points as number) + (!data.voteStatus ? 1 : 2) * value;
cache.writeFragment(
gql`
fragment __ on Post {
points
voteStatus
}
`,
{ id: postId, points: newPoints } as any
{ id: postId, points: newPoints, voteStatus: value } as any
);
}
},
Expand Down

0 comments on commit 36da2e6

Please sign in to comment.