diff --git a/TODO.md b/TODO.md index 920c6c0..53d6c19 100644 --- a/TODO.md +++ b/TODO.md @@ -31,7 +31,7 @@ ### Страница аниме тайтла - [ ] Просмотр комментариев и комментирование - - [ ] Просмотр всех комментариев (как Modal) + - [X] Просмотр всех комментариев (как Modal) - [ ] Отправление комментариев - [ ] Отправление ответов - [ ] Метка комментариев как спойлер @@ -51,6 +51,7 @@ - в статусе профиля нет переноса на новые линии - лишний отступ на последнем ответе на комментарий +- оценка комментариев не работает ? ## Другое diff --git a/app/components/Comments/Comments.Comment.tsx b/app/components/Comments/Comments.Comment.tsx index e8fc314..ea7cb8c 100644 --- a/app/components/Comments/Comments.Comment.tsx +++ b/app/components/Comments/Comments.Comment.tsx @@ -118,9 +118,9 @@ export const CommentsComment = (props: { > diff --git a/app/components/Comments/Comments.Main.tsx b/app/components/Comments/Comments.Main.tsx index d0837e0..0147a41 100644 --- a/app/components/Comments/Comments.Main.tsx +++ b/app/components/Comments/Comments.Main.tsx @@ -1,48 +1,176 @@ -import { Card, Button } from "flowbite-react"; +import { Card, Button, Modal, Spinner } from "flowbite-react"; import { CommentsComment } from "./Comments.Comment"; +import { useState, useEffect, useCallback } from "react"; +import { ENDPOINTS } from "#/api/config"; +import useSWRInfinite from "swr/infinite"; + export const CommentsMain = (props: { release_id: number; token: string | null; comments: any; }) => { + const [isAllCommentsOpen, setIsAllCommentsOpen] = useState(false); return ( - -
-
-
-

- Комментарии -

-

- Популярные и актуальные -

+ <> + +
+
+
+

+ Комментарии +

+

+ Популярные и актуальные +

+
+ +
+
+
+ + +
+ +
+
+ {props.comments.map((comment: any) => ( + + ))}
-
-
-
- - -
- -
-
- {props.comments.map((comment: any) => ( + + + + ); +}; + +const fetcher = async (url: string) => { + const res = await fetch(url); + + if (!res.ok) { + const error = new Error( + `An error occurred while fetching the data. status: ${res.status}` + ); + error.message = await res.json(); + throw error; + } + + return res.json(); +}; + +const CommentsAllModal = (props: { + isOpen: boolean; + setIsOpen: any; + release_id: number; + token: string | null; +}) => { + const [isLoadingEnd, setIsLoadingEnd] = useState(false); + const [currentRef, setCurrentRef] = useState(null); + const modalRef = useCallback((ref) => { + setCurrentRef(ref); + }, []); + + const getKey = (pageIndex: number, previousPageData: any) => { + if (previousPageData && !previousPageData.content.length) return null; + let url = `${ENDPOINTS.release.info}/comment/all/${props.release_id}/${pageIndex}?sort=1`; + if (props.token) { + url += `&token=${props.token}`; + } + return url; + }; + + const { data, error, isLoading, size, setSize } = useSWRInfinite( + getKey, + fetcher, + { initialSize: 2 } + ); + + const [content, setContent] = useState(null); + useEffect(() => { + if (data) { + let allReleases = []; + for (let i = 0; i < data.length; i++) { + allReleases.push(...data[i].content); + } + setContent(allReleases); + setIsLoadingEnd(true); + } + }, [data]); + + const [scrollPosition, setScrollPosition] = useState(0); + function handleScroll() { + const height = currentRef.scrollHeight - currentRef.clientHeight; + const windowScroll = currentRef.scrollTop; + const scrolled = (windowScroll / height) * 100; + setScrollPosition(Math.floor(scrolled)); + } + + useEffect(() => { + console.log(scrollPosition); + if (scrollPosition >= 95 && scrollPosition <= 96) { + setSize(size + 1); + } + }, [scrollPosition]); + + return ( + props.setIsOpen(false)} + > + +
+

+ Все комментарии +

+

+ всего: {!isLoadingEnd ? "загрузка..." : data[0].total_count} +

+
+
+
+ {!isLoadingEnd ? ( + + ) : content ? ( + content.map((comment: any) => ( - ))} -
+ )) + ) : ( +

+ Комментариев нет +

+ )}
-
+ {/* TEXT */} + ); };