From fc1d3d26f63e5675746ba945286a63d9e1244014 Mon Sep 17 00:00:00 2001 From: Kentai Radiquum Date: Tue, 6 Aug 2024 13:42:15 +0500 Subject: [PATCH] feat: add comment voting --- TODO.md | 6 +- app/components/Comments/Comments.Comment.tsx | 105 +++++++++++++++++-- app/components/Comments/Comments.Main.tsx | 9 +- 3 files changed, 106 insertions(+), 14 deletions(-) diff --git a/TODO.md b/TODO.md index 82b7b52..bbbc468 100644 --- a/TODO.md +++ b/TODO.md @@ -60,8 +60,8 @@ - [ ] Переход на страницу пользователя оставившего комментарий - [ ] Отправление комментариев - [ ] Отправление ответов - - [ ] Оценка комментариев - - [ ] Метка комментариев как споилер + - [X] Оценка комментариев + - [ ] Метка комментариев как спойлер - [X] Сохранение эпизода в историю просмотров - [X] Добавление \ Удаление аниме в\из списков закладок и избранных - [X] Связанные релизы @@ -83,4 +83,4 @@ - [ ] документация API Anixart - [ ] Уведомление о том что мы не связаны с аниксарт при первом открытии -- [ ] Использование PWA +- [X] Использование PWA diff --git a/app/components/Comments/Comments.Comment.tsx b/app/components/Comments/Comments.Comment.tsx index 50710bc..aca0850 100644 --- a/app/components/Comments/Comments.Comment.tsx +++ b/app/components/Comments/Comments.Comment.tsx @@ -1,6 +1,7 @@ import { unixToDate } from "#/api/utils"; import { useEffect, useState } from "react"; import { ENDPOINTS } from "#/api/config"; +import { Button } from "flowbite-react"; export const CommentsComment = (props: { profile: { login: string; avatar: string; id: number }; @@ -8,17 +9,24 @@ export const CommentsComment = (props: { id: number; timestamp: number; message: string; - likes: number; reply_count: number; + likes_count: number; + vote: number; }; isSubComment?: boolean; + token: string | null; }) => { const [replies, setReplies] = useState([]); + const [likes, setLikes] = useState(props.comment.likes_count); + const [vote, setVote] = useState(props.comment.vote); + useEffect(() => { async function _fetchReplies() { - await fetch( - `${ENDPOINTS.release.info}/comment/replies/${props.comment.id}/0?sort=2` - ) + let url = `${ENDPOINTS.release.info}/comment/replies/${props.comment.id}/0?sort=2`; + if (props.token) { + url += `&token=${props.token}`; + } + await fetch(url) .then((res) => res.json()) .then((data) => { setReplies(data.content); @@ -29,6 +37,44 @@ export const CommentsComment = (props: { } }, []); + async function _sendVote(action: number) { + if (props.token) { + let url = `${ENDPOINTS.release.info}/comment/vote/${props.comment.id}/${action}?token=${props.token}`; + fetch(url); + } + } + + // TODO: make it readable + function _updateVote(action: string) { + if (action === "like" && vote == 0) { + setVote(2); + setLikes(likes + 1); + _sendVote(2); + } else if (action === "dislike" && vote == 0) { + setVote(1); + setLikes(likes - 1); + _sendVote(1); + } else if (action === "like" && vote == 1) { + setVote(2); + setLikes(likes + 2); + _sendVote(2); + } else if (action === "dislike" && vote == 2) { + setVote(1); + setLikes(likes - 2); + _sendVote(1); + } else { + setVote(0); + _sendVote(0); + if (action === "dislike" && vote == 1) { + setLikes(likes + 1); + } else if (action === "like" && vote == 2) { + setLikes(likes - 1); + } else { + setLikes(props.comment.likes_count); + } + } + } + return (
@@ -54,7 +100,7 @@ export const CommentsComment = (props: {

{props.comment.message}

-
+
+
+ {props.token && ( + + )} +

0 + ? "text-green-500 dark:text-green-400" + : likes < 0 + ? "text-red-500 dark:text-red-400" + : "text-gray-500 dark:text-gray-400" + }`} + > + {likes} +

+ {props.token && ( + + )} +
{replies.length > 0 && replies.map((comment: any) => ( @@ -86,10 +177,12 @@ export const CommentsComment = (props: { id: comment.id, timestamp: comment.timestamp, message: comment.message, - likes: comment.likes_count, reply_count: comment.reply_count, + likes_count: comment.likes_count, + vote: comment.vote, }} isSubComment={true} + token={props.token} /> ))}
diff --git a/app/components/Comments/Comments.Main.tsx b/app/components/Comments/Comments.Main.tsx index 3009db0..d0837e0 100644 --- a/app/components/Comments/Comments.Main.tsx +++ b/app/components/Comments/Comments.Main.tsx @@ -37,10 +37,7 @@ export const CommentsMain = (props: { required > - @@ -53,9 +50,11 @@ export const CommentsMain = (props: { id: comment.id, timestamp: comment.timestamp, message: comment.message, - likes: comment.likes_count, reply_count: comment.reply_count, + likes_count: comment.likes_count, + vote: comment.vote, }} + token={props.token} /> ))}