From 5b3fc6de4da5e2545701b99abd5c020a731b2125 Mon Sep 17 00:00:00 2001
From: Kentai Radiquum
Date: Sat, 10 Aug 2024 18:28:54 +0500
Subject: [PATCH] feat: add user owned comment editing and deleting
---
TODO.md | 4 -
app/components/Comments/Comments.Comment.tsx | 46 ++++++++-
app/components/Comments/Comments.Edit.tsx | 101 +++++++++++++++++++
3 files changed, 144 insertions(+), 7 deletions(-)
create mode 100644 app/components/Comments/Comments.Edit.tsx
diff --git a/TODO.md b/TODO.md
index 1954512..e5571e3 100644
--- a/TODO.md
+++ b/TODO.md
@@ -29,10 +29,6 @@
### Страница аниме тайтла
-- [ ] Просмотр комментариев и комментирование
- - [ ] Удаление
- - [ ] Редактирование
-
- [ ] Видео тайтла [трейлеры, опенинги] (?)
### Профиль
diff --git a/app/components/Comments/Comments.Comment.tsx b/app/components/Comments/Comments.Comment.tsx
index e13bf9a..02b3707 100644
--- a/app/components/Comments/Comments.Comment.tsx
+++ b/app/components/Comments/Comments.Comment.tsx
@@ -1,9 +1,11 @@
import { unixToDate, sinceUnixDate } from "#/api/utils";
-import { useEffect, useState, useCallback } from "react";
+import { useEffect, useState } from "react";
import { ENDPOINTS } from "#/api/config";
-import { Button } from "flowbite-react";
+import { Button, Dropdown } from "flowbite-react";
import Link from "next/link";
import { CommentsAddModal } from "./Comments.Add";
+import { CommentsEditModal } from "./Comments.Edit";
+import { useUserStore } from "#/store/auth";
export const CommentsComment = (props: {
release_id: number;
@@ -30,10 +32,12 @@ export const CommentsComment = (props: {
const [likes, setLikes] = useState(props.comment.likes_count);
const [vote, setVote] = useState(props.comment.vote);
const [isAddCommentsOpen, setIsAddCommentsOpen] = useState(false);
+ const [isEditCommentsOpen, setIsEditCommentsOpen] = useState(false);
const [isHidden, setIsHidden] = useState(
!props.isReplying &&
(props.comment.isSpoiler || props.comment.likes_count < -5)
);
+ const user: any = useUserStore((state) => state.user);
const [shouldRender, setShouldRender] = useState(true);
const [commentSend, setCommentSend] = useState(false);
@@ -43,6 +47,18 @@ export const CommentsComment = (props: {
parentCommentId = props.parentComment.id;
}
+ async function _deleteComment() {
+ if (props.token) {
+ let url = `${ENDPOINTS.release.info}/comment/delete/${props.comment.id}?token=${props.token}`;
+ await fetch(url);
+
+ if (props.setShouldRender && props.setCommentSend) {
+ props.setShouldRender(true);
+ props.setCommentSend(true);
+ }
+ }
+ }
+
useEffect(() => {
async function _fetchReplies() {
setReplies([]);
@@ -135,6 +151,20 @@ export const CommentsComment = (props: {
+ {user && props.profile.id == user.id && (
+ (
+
+ )}
+ >
+ setIsEditCommentsOpen(true)}>
+ Редактировать
+
+ _deleteComment()}>Удалить
+
+ )}
@@ -266,11 +296,21 @@ export const CommentsComment = (props: {
release_id={props.release_id}
token={props.token}
isReply={true}
- parentComment={props.comment}
+ parentComment={props.parentComment || props.comment}
parentProfile={props.profile}
setShouldRender={props.setShouldRender || setShouldRender}
setCommentSend={props.setCommentSend || setCommentSend}
/>
+ {props.token && (
+
+ )}
>
);
};
diff --git a/app/components/Comments/Comments.Edit.tsx b/app/components/Comments/Comments.Edit.tsx
new file mode 100644
index 0000000..67faca0
--- /dev/null
+++ b/app/components/Comments/Comments.Edit.tsx
@@ -0,0 +1,101 @@
+import { Button, Modal, ToggleSwitch, Label, Textarea } from "flowbite-react";
+import { useState } from "react";
+import { ENDPOINTS } from "#/api/config";
+
+export const CommentsEditModal = (props: {
+ isOpen: boolean;
+ setIsOpen: any;
+ parentComment?: any;
+ token: string;
+ setShouldRender?: any;
+ setCommentSend?: any;
+}) => {
+ const [message, setMessage] = useState(props.parentComment.message);
+ const [isSpoiler, setIsSpoiler] = useState(props.parentComment.isSpoiler);
+ const [isSending, setIsSending] = useState(false);
+
+ function _sendComment(e) {
+ e.preventDefault();
+ const re = /\n/gi;
+ const data = {
+ message: message.replace(re, "\r\n").trim(),
+ spoiler: isSpoiler,
+ };
+
+ async function _send() {
+ const res = await fetch(
+ `${ENDPOINTS.release.info}/comment/edit/${props.parentComment.id}?token=${props.token}`,
+ {
+ method: "POST",
+ body: JSON.stringify(data),
+ }
+ );
+
+ if (props.setShouldRender && props.setCommentSend) {
+ props.setShouldRender(true);
+ props.setCommentSend(true);
+ }
+
+ props.setIsOpen(false);
+ setIsSending(false);
+ }
+
+ if (props.token && message.trim() != "") {
+ setIsSending(true);
+ _send();
+ }
+ }
+
+ return (
+ props.setIsOpen(false)}
+ >
+
+
+ Редактировать комментарий
+
+
+
+
+
+
+ );
+};