Skip to content

Commit

Permalink
Added check if user has perms to remove the post
Browse files Browse the repository at this point in the history
  • Loading branch information
zaxovaiko committed Oct 29, 2023
1 parent 1c4b9a9 commit 35c4c33
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 12 deletions.
6 changes: 3 additions & 3 deletions schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,12 +137,12 @@ const schema = buildSchema(`
}
type Query {
currentUser: User
user(input: UserInput!): User
currentUser: User!
user(input: UserInput!): User!
users(input: PaginationInput): UserConnection!
posts(input: PaginationInput): PostConnection!
comments(input: PaginationInput): CommentConnection!
post(input: PostInput!): Post
post(input: PostInput!): Post!
}
type Mutation {
Expand Down
5 changes: 5 additions & 0 deletions web/graphql/queries/getCurrentUser.graphql
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
query getCurrentUser {
currentUser {
id
}
}
20 changes: 11 additions & 9 deletions web/pages/Profile/PostCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { IconTrash } from '@tabler/icons';
import { Post, PostThumbnailFragment } from 'generated/client';
import { useRouter } from 'next/router';
import React, { memo } from 'react';
import { useCurrentUser } from 'web/shared/hooks/useCurrentUser';

type Props = {
post: PostThumbnailFragment;
Expand All @@ -20,6 +21,8 @@ type Props = {

export const PostCard = memo(({ post, onDelete }: Props) => {
const { push } = useRouter();
const currentUser = useCurrentUser();
const isAuthor = currentUser?.id === post.user.id;

return (
<Card shadow="sm" padding="lg" radius="md" withBorder>
Expand All @@ -34,15 +37,14 @@ export const PostCard = memo(({ post, onDelete }: Props) => {
<Title order={2} fw="bold" weight={500}>
{post.title}
</Title>
<IconTrash
onClick={() => {
onDelete(post.id).catch(console.error);
}}
style={{
cursor: 'pointer',
marginLeft: 'auto',
}}
/>
{isAuthor && (
<IconTrash
onClick={() => {
onDelete(post.id).catch(console.error);
}}
style={{ cursor: 'pointer', marginLeft: 'auto' }}
/>
)}
</Flex>
{post.isDraft && <Badge variant="gradient">Draft</Badge>}
</Group>
Expand Down
10 changes: 10 additions & 0 deletions web/shared/hooks/useCurrentUser.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { useGetCurrentUserQuery } from 'generated/client';

export const useCurrentUser = () => {
const { data } = useGetCurrentUserQuery({
fetchPolicy: 'cache-first',
nextFetchPolicy: 'cache-first',
});

return data?.currentUser;
};

0 comments on commit 35c4c33

Please sign in to comment.