diff --git a/schema.ts b/schema.ts index 02d1ac2..930b3d2 100644 --- a/schema.ts +++ b/schema.ts @@ -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 { diff --git a/web/graphql/queries/getCurrentUser.graphql b/web/graphql/queries/getCurrentUser.graphql new file mode 100644 index 0000000..767d9c5 --- /dev/null +++ b/web/graphql/queries/getCurrentUser.graphql @@ -0,0 +1,5 @@ +query getCurrentUser { + currentUser { + id + } +} diff --git a/web/pages/Profile/PostCard.tsx b/web/pages/Profile/PostCard.tsx index abd1f1a..4217287 100644 --- a/web/pages/Profile/PostCard.tsx +++ b/web/pages/Profile/PostCard.tsx @@ -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; @@ -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 ( @@ -34,15 +37,14 @@ export const PostCard = memo(({ post, onDelete }: Props) => { {post.title} - { - onDelete(post.id).catch(console.error); - }} - style={{ - cursor: 'pointer', - marginLeft: 'auto', - }} - /> + {isAuthor && ( + { + onDelete(post.id).catch(console.error); + }} + style={{ cursor: 'pointer', marginLeft: 'auto' }} + /> + )} {post.isDraft && Draft} diff --git a/web/shared/hooks/useCurrentUser.ts b/web/shared/hooks/useCurrentUser.ts new file mode 100644 index 0000000..c494396 --- /dev/null +++ b/web/shared/hooks/useCurrentUser.ts @@ -0,0 +1,10 @@ +import { useGetCurrentUserQuery } from 'generated/client'; + +export const useCurrentUser = () => { + const { data } = useGetCurrentUserQuery({ + fetchPolicy: 'cache-first', + nextFetchPolicy: 'cache-first', + }); + + return data?.currentUser; +};