Skip to content

Commit

Permalink
single-post
Browse files Browse the repository at this point in the history
  • Loading branch information
benawad committed Aug 16, 2020
1 parent 34ef721 commit 1c75b1a
Show file tree
Hide file tree
Showing 6 changed files with 116 additions and 6 deletions.
4 changes: 2 additions & 2 deletions server/src/resolvers/post.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,8 +171,8 @@ export class PostResolver {
}

@Query(() => Post, { nullable: true })
post(@Arg("id") id: number): Promise<Post | undefined> {
return Post.findOne(id);
post(@Arg("id", () => Int) id: number): Promise<Post | undefined> {
return Post.findOne(id, { relations: ["creator"] });
}

@Mutation(() => Post)
Expand Down
9 changes: 7 additions & 2 deletions web/src/components/NavBar.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React from "react";
import { Box, Link, Flex, Button } from "@chakra-ui/core";
import { Box, Link, Flex, Button, Heading } from "@chakra-ui/core";
import NextLink from "next/link";
import { useMeQuery, useLogoutMutation } from "../generated/graphql";
import { isServer } from "../utils/isServer";
Expand Down Expand Up @@ -47,7 +47,12 @@ export const NavBar: React.FC<NavBarProps> = ({}) => {
}

return (
<Flex zIndex={1} position="sticky" top={0} bg="tan" p={4}>
<Flex zIndex={1} position="sticky" top={0} bg="tan" p={4} align="center">
<NextLink href="/">
<Link>
<Heading>LiReddit</Heading>
</Link>
</NextLink>
<Box ml={"auto"}>{body}</Box>
</Flex>
);
Expand Down
40 changes: 39 additions & 1 deletion web/src/generated/graphql.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export type QueryPostsArgs = {


export type QueryPostArgs = {
id: Scalars['Float'];
id: Scalars['Int'];
};

export type PaginatedPosts = {
Expand Down Expand Up @@ -264,6 +264,23 @@ export type MeQuery = (
)> }
);

export type PostQueryVariables = Exact<{
id: Scalars['Int'];
}>;


export type PostQuery = (
{ __typename?: 'Query' }
& { post?: Maybe<(
{ __typename?: 'Post' }
& Pick<Post, 'id' | 'createdAt' | 'updatedAt' | 'title' | 'points' | 'text' | 'voteStatus'>
& { creator: (
{ __typename?: 'User' }
& Pick<User, 'id' | 'username'>
) }
)> }
);

export type PostsQueryVariables = Exact<{
limit: Scalars['Int'];
cursor?: Maybe<Scalars['String']>;
Expand Down Expand Up @@ -408,6 +425,27 @@ export const MeDocument = gql`
export function useMeQuery(options: Omit<Urql.UseQueryArgs<MeQueryVariables>, 'query'> = {}) {
return Urql.useQuery<MeQuery>({ query: MeDocument, ...options });
};
export const PostDocument = gql`
query Post($id: Int!) {
post(id: $id) {
id
createdAt
updatedAt
title
points
text
voteStatus
creator {
id
username
}
}
}
`;

export function usePostQuery(options: Omit<Urql.UseQueryArgs<PostQueryVariables>, 'query'> = {}) {
return Urql.useQuery<PostQuery>({ query: PostDocument, ...options });
};
export const PostsDocument = gql`
query Posts($limit: Int!, $cursor: String) {
posts(limit: $limit, cursor: $cursor) {
Expand Down
15 changes: 15 additions & 0 deletions web/src/graphql/queries/post.graphql
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
query Post($id: Int!) {
post(id: $id) {
id
createdAt
updatedAt
title
points
text
voteStatus
creator {
id
username
}
}
}
6 changes: 5 additions & 1 deletion web/src/pages/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,11 @@ const Index = () => {
<Flex key={p.id} p={5} shadow="md" borderWidth="1px">
<UpdootSection post={p} />
<Box>
<Heading fontSize="xl">{p.title}</Heading>
<NextLink href="/post/[id]" as={`/post/${p.id}`}>
<Link>
<Heading fontSize="xl">{p.title}</Heading>
</Link>
</NextLink>
<Text>posted by {p.creator.username}</Text>
<Text mt={4}>{p.textSnippet}</Text>
</Box>
Expand Down
48 changes: 48 additions & 0 deletions web/src/pages/post/[id].tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import React from "react";
import { withUrqlClient } from "next-urql";
import { createUrqlClient } from "../../utils/createUrqlClient";
import { useRouter } from "next/router";
import { usePostQuery } from "../../generated/graphql";
import { Layout } from "../../components/Layout";
import { Heading, Box } from "@chakra-ui/core";

const Post = ({}) => {
const router = useRouter();
const intId =
typeof router.query.id === "string" ? parseInt(router.query.id) : -1;
const [{ data, error, fetching }] = usePostQuery({
pause: intId === -1,
variables: {
id: intId,
},
});

if (fetching) {
return (
<Layout>
<div>loading...</div>
</Layout>
);
}

if (error) {
return <div>{error.message}</div>;
}

if (!data?.post) {
return (
<Layout>
<Box>could not find post</Box>
</Layout>
);
}

return (
<Layout>
<Heading mb={4}>{data.post.title}</Heading>
{data.post.text}
</Layout>
);
};

export default withUrqlClient(createUrqlClient, { ssr: true })(Post);

0 comments on commit 1c75b1a

Please sign in to comment.