diff --git a/TODO.md b/TODO.md index 0273c61..338c4f1 100644 --- a/TODO.md +++ b/TODO.md @@ -16,7 +16,7 @@ ### Коллекции -- [ ] Добавление \ Удаление аниме в\из коллекции +... ### Страница аниме тайтла diff --git a/app/api/config.ts b/app/api/config.ts index 1640239..934b0f1 100644 --- a/app/api/config.ts +++ b/app/api/config.ts @@ -24,6 +24,7 @@ export const ENDPOINTS = { collection: { base: `${API_PREFIX}/collection`, list: `${API_PREFIX}/collection/list`, + addRelease: `${API_PREFIX}/collectionMy/release/add`, create: `${API_PREFIX}/collectionMy/create`, delete: `${API_PREFIX}/collectionMy/delete`, edit: `${API_PREFIX}/collectionMy/edit`, diff --git a/app/components/ReleaseInfo/ReleaseInfo.UserList.tsx b/app/components/ReleaseInfo/ReleaseInfo.UserList.tsx index fa0b419..54f2399 100644 --- a/app/components/ReleaseInfo/ReleaseInfo.UserList.tsx +++ b/app/components/ReleaseInfo/ReleaseInfo.UserList.tsx @@ -1,6 +1,8 @@ -import { Card, Dropdown, Button } from "flowbite-react"; +import { Card, Dropdown, Button, Modal } from "flowbite-react"; import { ENDPOINTS } from "#/api/config"; import Link from "next/link"; +import useSWRInfinite from "swr/infinite"; +import { useCallback, useEffect, useState } from "react"; const lists = [ { list: 0, name: "Не смотрю" }, @@ -25,7 +27,10 @@ export const ReleaseInfoUserList = (props: { setUserList: any; setIsFavorite: any; collection_count: number; + profile_id: number; }) => { + const [AddReleaseToCollectionModalOpen, setAddReleaseToCollectionModalOpen] = + useState(false); function _addToFavorite() { if (props.token) { props.setIsFavorite(!props.isFavorite); @@ -62,7 +67,12 @@ export const ReleaseInfoUserList = (props: { {props.token && ( - @@ -103,6 +113,135 @@ export const ReleaseInfoUserList = (props: {

Войдите что-бы добавить в список, избранное или коллекцию

)} + ); }; + +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 AddReleaseToCollectionModal = (props: { + isOpen: boolean; + setIsOpen: (isopen: boolean) => void; + release_id: number; + token: string; + profile_id: number; +}) => { + const getKey = (pageIndex: number, previousPageData: any) => { + if (previousPageData && !previousPageData.content.length) return null; + return `${ENDPOINTS.collection.userCollections}/${props.profile_id}/${pageIndex}?token=${props.token}`; + }; + + const { data, error, isLoading, size, setSize } = useSWRInfinite( + getKey, + fetcher, + { initialSize: 2 } + ); + + const [currentRef, setCurrentRef] = useState(null); + const modalRef = useCallback((ref) => { + setCurrentRef(ref); + }, []); + + const [content, setContent] = useState([]); + useEffect(() => { + if (data) { + let allReleases = []; + for (let i = 0; i < data.length; i++) { + allReleases.push(...data[i].content); + } + setContent(allReleases); + } + }, [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(() => { + if (scrollPosition >= 95 && scrollPosition <= 96) { + setSize(size + 1); + } + }, [scrollPosition]); + + function _addToCollection(collection_id: number) { + if (props.token) { + fetch( + `${ENDPOINTS.collection.addRelease}/${collection_id}?release_id=${props.release_id}&token=${props.token}` + ) + .then((res) => { + if (!res.ok) { + alert("Ошибка добавления релиза в коллекцию."); + } else { + return res.json(); + } + }) + .then((data) => { + if (data.code != 0) { + alert( + "Не удалось добавить релиз в коллекцию, возможно он уже в ней находится." + ); + } else { + props.setIsOpen(false); + } + }); + } + } + + return ( + props.setIsOpen(false)} + > + Выбор коллекции +
+ {content && content.length > 0 + ? content.map((collection) => ( + + )) + : "коллекций не найдено"} +
+
+ ); +}; diff --git a/app/pages/Release.tsx b/app/pages/Release.tsx index 353bcc4..0661e76 100644 --- a/app/pages/Release.tsx +++ b/app/pages/Release.tsx @@ -18,7 +18,7 @@ import { CommentsMain } from "#/components/Comments/Comments.Main"; import { InfoLists } from "#/components/InfoLists/InfoLists"; export const ReleasePage = (props: any) => { - const token = useUserStore((state) => state.token); + const userStore = useUserStore(); const [userList, setUserList] = useState(0); const [userFavorite, setUserFavorite] = useState(false); @@ -26,8 +26,8 @@ export const ReleasePage = (props: any) => { let url: string; url = `/api/release/${id}`; - if (token) { - url += `?token=${token}`; + if (userStore.token) { + url += `?token=${userStore.token}`; } const { data, isLoading, error } = useSWR(url, fetcher); return [data, isLoading, error]; @@ -84,7 +84,8 @@ export const ReleasePage = (props: any) => { userList={userList} isFavorite={userFavorite} release_id={data.release.id} - token={token} + token={userStore.token} + profile_id={userStore.user.id} setUserList={setUserList} setIsFavorite={setUserFavorite} collection_count={data.release.collection_count} @@ -100,7 +101,7 @@ export const ReleasePage = (props: any) => { {