mirror of
https://github.com/barthuijgen/factorio-sites.git
synced 2025-03-17 21:17:57 +02:00
feat: added deleting of comments
This commit is contained in:
parent
b88ded4615
commit
23747a4557
@ -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<CommentsProps> = ({ blueprint_page_id }) => {
|
||||
@ -71,6 +75,21 @@ export const Comments: React.FC<CommentsProps> = ({ 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 (
|
||||
<div>
|
||||
{auth && (
|
||||
@ -98,6 +117,11 @@ export const Comments: React.FC<CommentsProps> = ({ blueprint_page_id }) => {
|
||||
comments.map((comment) => (
|
||||
<CommentDiv key={comment.id}>
|
||||
<div>
|
||||
{(auth?.role === "moderator" || auth?.role === "admin") && (
|
||||
<div className="delete">
|
||||
<Button onClick={() => onDelete(comment.id)}>[moderator] Delete</Button>
|
||||
</div>
|
||||
)}
|
||||
{comment.user.username} at{" "}
|
||||
{format(new Date(comment.created_at), getLocaleDateFormat() + " HH:mm")}
|
||||
</div>
|
||||
|
@ -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;
|
||||
|
@ -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({
|
||||
|
Loading…
x
Reference in New Issue
Block a user