Skip to content

Commit

Permalink
delete-post
Browse files Browse the repository at this point in the history
  • Loading branch information
benawad committed Aug 16, 2020
1 parent 1c75b1a commit cdfa09c
Show file tree
Hide file tree
Showing 7 changed files with 97 additions and 34 deletions.
4 changes: 3 additions & 1 deletion server/src/entities/Updoot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ export class Updoot extends BaseEntity {
@PrimaryColumn()
postId: number;

@ManyToOne(() => Post, (post) => post.updoots)
@ManyToOne(() => Post, (post) => post.updoots, {
onDelete: "CASCADE",
})
post: Post;
}
20 changes: 18 additions & 2 deletions server/src/resolvers/post.ts
Original file line number Diff line number Diff line change
Expand Up @@ -203,8 +203,24 @@ export class PostResolver {
}

@Mutation(() => Boolean)
async deletePost(@Arg("id") id: number): Promise<boolean> {
await Post.delete(id);
@UseMiddleware(isAuth)
async deletePost(
@Arg("id", () => Int) id: number,
@Ctx() { req }: MyContext
): Promise<boolean> {
// not cascade way
// const post = await Post.findOne(id);
// if (!post) {
// return false;
// }
// if (post.creatorId !== req.session.userId) {
// throw new Error("not authorized");
// }

// await Updoot.delete({ postId: id });
// await Post.delete({ id });

await Post.delete({ id, creatorId: req.session.userId });
return true;
}
}
23 changes: 15 additions & 8 deletions web/src/components/NavBar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,12 @@ export const NavBar: React.FC<NavBarProps> = ({}) => {
// user is logged in
} else {
body = (
<Flex>
<Flex align="center">
<NextLink href="/create-post">
<Button as={Link} mr={4}>
create post
</Button>
</NextLink>
<Box mr={2}>{data.me.username}</Box>
<Button
onClick={() => {
Expand All @@ -47,13 +52,15 @@ export const NavBar: React.FC<NavBarProps> = ({}) => {
}

return (
<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 zIndex={1} position="sticky" top={0} bg="tan" p={4}>
<Flex flex={1} m="auto" align="center" maxW={800}>
<NextLink href="/">
<Link>
<Heading>LiReddit</Heading>
</Link>
</NextLink>
<Box ml={"auto"}>{body}</Box>
</Flex>
</Flex>
);
};
21 changes: 20 additions & 1 deletion web/src/generated/graphql.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ export type MutationUpdatePostArgs = {


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


Expand Down Expand Up @@ -197,6 +197,16 @@ export type CreatePostMutation = (
) }
);

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


export type DeletePostMutation = (
{ __typename?: 'Mutation' }
& Pick<Mutation, 'deletePost'>
);

export type ForgotPasswordMutationVariables = Exact<{
email: Scalars['String'];
}>;
Expand Down Expand Up @@ -365,6 +375,15 @@ export const CreatePostDocument = gql`
export function useCreatePostMutation() {
return Urql.useMutation<CreatePostMutation, CreatePostMutationVariables>(CreatePostDocument);
};
export const DeletePostDocument = gql`
mutation DeletePost($id: Int!) {
deletePost(id: $id)
}
`;

export function useDeletePostMutation() {
return Urql.useMutation<DeletePostMutation, DeletePostMutationVariables>(DeletePostDocument);
};
export const ForgotPasswordDocument = gql`
mutation ForgotPassword($email: String!) {
forgotPassword(email: $email)
Expand Down
3 changes: 3 additions & 0 deletions web/src/graphql/mutations/deletePost.graphql
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
mutation DeletePost($id: Int!) {
deletePost(id: $id)
}
53 changes: 31 additions & 22 deletions web/src/pages/index.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { withUrqlClient } from "next-urql";
import { createUrqlClient } from "../utils/createUrqlClient";
import { usePostsQuery } from "../generated/graphql";
import { usePostsQuery, useDeletePostMutation } from "../generated/graphql";
import { Layout } from "../components/Layout";
import {
Link,
Expand All @@ -26,38 +26,47 @@ const Index = () => {
const [{ data, fetching }] = usePostsQuery({
variables,
});
const [, deletePost] = useDeletePostMutation();

if (!fetching && !data) {
return <div>you got query failed for some reason</div>;
}

return (
<Layout>
<Flex align="center">
<Heading>LiReddit</Heading>
<NextLink href="/create-post">
<Link ml="auto">create post</Link>
</NextLink>
</Flex>
<br />
{!data && fetching ? (
<div>loading...</div>
) : (
<Stack spacing={8}>
{data!.posts.posts.map((p) => (
<Flex key={p.id} p={5} shadow="md" borderWidth="1px">
<UpdootSection post={p} />
<Box>
<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>
</Flex>
))}
{data!.posts.posts.map((p) =>
!p ? null : (
<Flex key={p.id} p={5} shadow="md" borderWidth="1px">
<UpdootSection post={p} />
<Box flex={1}>
<NextLink href="/post/[id]" as={`/post/${p.id}`}>
<Link>
<Heading fontSize="xl">{p.title}</Heading>
</Link>
</NextLink>
<Text>posted by {p.creator.username}</Text>
<Flex align="center">
<Text flex={1} mt={4}>
{p.textSnippet}
</Text>
<IconButton
ml="auto"
variantColor="red"
icon="delete"
aria-label="Delete Post"
onClick={() => {
deletePost({ id: p.id });
}}
/>
</Flex>
</Box>
</Flex>
)
)}
</Stack>
)}
{data && data.posts.hasMore ? (
Expand Down
7 changes: 7 additions & 0 deletions web/src/utils/createUrqlClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
MeQuery,
RegisterMutation,
VoteMutationVariables,
DeletePostMutationVariables,
} from "../generated/graphql";
import { betterUpdateQuery } from "./betterUpdateQuery";
import Router from "next/router";
Expand Down Expand Up @@ -147,6 +148,12 @@ export const createUrqlClient = (ssrExchange: any, ctx: any) => {
},
updates: {
Mutation: {
deletePost: (_result, args, cache, info) => {
cache.invalidate({
__typename: "Post",
id: (args as DeletePostMutationVariables).id,
});
},
vote: (_result, args, cache, info) => {
const { postId, value } = args as VoteMutationVariables;
const data = cache.readFragment(
Expand Down

0 comments on commit cdfa09c

Please sign in to comment.