diff --git a/apps/blueprints/src/components/Comments.tsx b/apps/blueprints/src/components/Comments.tsx index a049d62..b4b169a 100644 --- a/apps/blueprints/src/components/Comments.tsx +++ b/apps/blueprints/src/components/Comments.tsx @@ -37,6 +37,10 @@ const AddCommentDiv = styled.div` const CommentDiv = styled.div` background: #4e4c4c; margin: 0.5rem 0; + + .delete { + float: right; + } `; export const Comments: React.FC = ({ blueprint_page_id }) => { @@ -71,6 +75,21 @@ export const Comments: React.FC = ({ blueprint_page_id }) => { fetchTopLevelComments(); }; + const onDelete = async (comment_id: string) => { + // eslint-disable-next-line no-restricted-globals + if (!confirm("Are you sure you want to delete this comment?")) { + return; + } + await fetch("/api/blueprint/comment", { + method: "DELETE", + body: JSON.stringify({ comment_id }), + headers: { + "Content-Type": "application/json", + }, + }); + fetchTopLevelComments(); + }; + return (
{auth && ( @@ -98,6 +117,11 @@ export const Comments: React.FC = ({ blueprint_page_id }) => { comments.map((comment) => (
+ {(auth?.role === "moderator" || auth?.role === "admin") && ( +
+ +
+ )} {comment.user.username} at{" "} {format(new Date(comment.created_at), getLocaleDateFormat() + " HH:mm")}
diff --git a/apps/blueprints/src/pages/api/blueprint/comment.ts b/apps/blueprints/src/pages/api/blueprint/comment.ts index 06dada2..391c792 100644 --- a/apps/blueprints/src/pages/api/blueprint/comment.ts +++ b/apps/blueprints/src/pages/api/blueprint/comment.ts @@ -1,20 +1,33 @@ import { apiHandler } from "../../../utils/api-handler"; -import { createComment } from "@factorio-sites/database"; +import { createComment, deleteComment } from "@factorio-sites/database"; const handler = apiHandler(async (req, res, { session }) => { - if (req.method !== "POST") return res.status(400).json({ error: "method must be POST" }); + if (req.method === "POST") { + if (!session) { + return res.status(401).json({ status: "Not authenticated" }); + } - if (!session) { - return res.status(401).json({ status: "Not authenticated" }); + const { body, blueprint_page_id } = req.body; + console.log({ body, blueprint_page_id }); + + const result = await createComment(blueprint_page_id, session.user, body); + console.log(result); + + res.status(200).json({ status: "Comment submitted" }); + } else if (req.method === "DELETE") { + if (!session) { + return res.status(401).json({ status: "Not authenticated" }); + } + + if (session.user.role !== "moderator" && session.user.role !== "admin") { + return res.status(401).json({ status: "Not authenticated" }); + } + const { comment_id } = req.body; + await deleteComment(comment_id); + res.status(200).json({ status: "Comment deleted" }); + } else { + return res.status(400).json({ error: `Unsupported method ${req.method}` }); } - - const { body, blueprint_page_id } = req.body; - console.log({ body, blueprint_page_id }); - - const result = await createComment(blueprint_page_id, session.user, body); - console.log(result); - - res.status(200).json({ status: "Comment submitted" }); }); export default handler; diff --git a/libs/database/src/lib/data/comment.ts b/libs/database/src/lib/data/comment.ts index 1e43329..9b02469 100644 --- a/libs/database/src/lib/data/comment.ts +++ b/libs/database/src/lib/data/comment.ts @@ -11,6 +11,12 @@ export async function createComment(blueprint_page_id: string, user: user, body: }); return result; } +export async function deleteComment(comment_id: string) { + const result = await prisma.comment.delete({ + where: { id: comment_id }, + }); + return result; +} export async function getComments(blueprint_page_id: string) { const result = await prisma.comment.findMany({