Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
tktcorporation committed Oct 10, 2022
1 parent 156e48a commit ab6b3f5
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 6 deletions.
1 change: 1 addition & 0 deletions api/src/graphql/comments.sdl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export const schema = gql`
type Query {
comments(postId: Int!): [Comment!]! @skipAuth
comment(id: Int!): Comment! @requireAuth(roles: ["moderator"])

This comment has been minimized.

Copy link
@tktcorporation

tktcorporation Oct 10, 2022

Author Owner

@requireAuth(roles: ["moderator"])
いらなさそう

}
input CreateCommentInput {
Expand Down
8 changes: 8 additions & 0 deletions api/src/services/comments/comments.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { Prisma } from '@prisma/client'
import type { QueryResolvers, CommentRelationResolvers } from 'types/graphql'

import { requireAuth } from 'src/lib/auth'
import { db } from 'src/lib/db'

export const comments: QueryResolvers['comments'] = ({
Expand All @@ -26,3 +27,10 @@ export const createComment = ({ input }: CreateCommentArgs) => {
data: input,
})
}

export const deleteComment: QueryResolvers['comment'] = ({ id }) => {
requireAuth({ roles: 'moderator' })
return db.comment.delete({
where: { id },
})
}
48 changes: 42 additions & 6 deletions web/src/components/Comment/Comment.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,18 @@
import type { Comment as IComment } from 'types/graphql'

import { useAuth } from '@redwoodjs/auth'
import { useMutation } from '@redwoodjs/web'

import { QUERY as CommentsQuery } from 'src/components/CommentsCell'

const DELETE = gql`
mutation DeleteCommentMutation($id: Int!) {
deleteComment(id: $id) {
id
}
}
`

const formattedDate = (datetime: ConstructorParameters<typeof Date>[0]) => {
const parsedDate = new Date(datetime)
const month = parsedDate.toLocaleString('default', { month: 'long' })
Expand All @@ -6,23 +21,44 @@ const formattedDate = (datetime: ConstructorParameters<typeof Date>[0]) => {

// Just a temporary type. We'll replace this later
interface Props {
comment: {
name: string
createdAt: string
body: string
}
comment: Pick<IComment, 'postId' | 'id' | 'name' | 'createdAt' | 'body'>
}

const Comment = ({ comment }: Props) => {
const { hasRole } = useAuth()
const [deleteComment] = useMutation(DELETE, {
refetchQueries: [
{
query: CommentsQuery,
variables: { postId: comment.postId },
},
],
})
const moderate = () => {
if (confirm('Are you sure?')) {
deleteComment({
variables: { id: comment.id },
})
}
}
return (
<div className="bg-gray-200 p-8 rounded-lg">
<div className="bg-gray-200 p-8 rounded-lg relative">
<header className="flex justify-between">
<h2 className="font-semibold text-gray-700">{comment.name}</h2>
<time className="text-xs text-gray-500" dateTime={comment.createdAt}>
{formattedDate(comment.createdAt)}
</time>
</header>
<p className="text-sm mt-2">{comment.body}</p>
{hasRole('moderator') && (
<button
type="button"
onClick={moderate}
className="absolute bottom-2 right-2 bg-red-500 text-xs rounded text-white px-2 py-1"
>
Delete
</button>
)}
</div>
)
}
Expand Down
1 change: 1 addition & 0 deletions web/src/components/CommentsCell/CommentsCell.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export const QUERY = gql`
id
name
body
postId
createdAt
}
}
Expand Down

0 comments on commit ab6b3f5

Please sign in to comment.