Ошибка
-//-// Произошла ошибка поиска. Попробуйте обновить страницу или зайдите -// позже. -//
-//diff --git a/api-prox/index.ts b/api-prox/index.ts index b653ef0..7bcd03c 100644 --- a/api-prox/index.ts +++ b/api-prox/index.ts @@ -18,7 +18,7 @@ app.use(function (req, res, next) { res.header("Access-Control-Allow-Origin", req.headers.origin || "*"); res.header( "Access-Control-Allow-Headers", - "Origin, X-Requested-With, Content-Type, Accept, Sign, Allow, User-Agent" + "Origin, X-Requested-With, Content-Type, Accept, Sign, Allow, User-Agent, Api-Version" ); res.header("Access-Control-Allow-Methods", "GET,HEAD,POST,OPTIONS"); next(); diff --git a/api-prox/shared.ts b/api-prox/shared.ts index 36144e0..f241fd3 100644 --- a/api-prox/shared.ts +++ b/api-prox/shared.ts @@ -1,7 +1,7 @@ export const corsHeaders = { "Access-Control-Allow-Origin": "*", "Access-Control-Allow-Headers": - "Origin, X-Requested-With, Content-Type, Accept, Sign, Allow, User-Agent", + "Origin, X-Requested-With, Content-Type, Accept, Sign, Allow, User-Agent, Api-Version", "Access-Control-Allow-Methods": "GET,HEAD,POST,OPTIONS", "Cache-Control": "no-cache", }; diff --git a/app/api/config.ts b/app/api/config.ts index 957a4c9..317ea1f 100644 --- a/app/api/config.ts +++ b/app/api/config.ts @@ -52,11 +52,12 @@ export const ENDPOINTS = { }, filter: `${API_PREFIX}/filter`, search: { - profileList: `${API_PREFIX}/search/profile/list/`, - profileHistory: `${API_PREFIX}/search/history/`, - profileFavoriteCollection: `${API_PREFIX}/search/favoriteCollections/`, - profiles: `${API_PREFIX}/search/profiles/`, - releases: `${API_PREFIX}/search/releases/`, + profileList: `${API_PREFIX}/search/profile/list`, + profileHistory: `${API_PREFIX}/search/history`, + profileFavoriteCollection: `${API_PREFIX}/search/favoriteCollections`, + profileFavorites: `${API_PREFIX}/search/favorites`, + profiles: `${API_PREFIX}/search/profiles`, + releases: `${API_PREFIX}/search/releases`, }, statistic: { addHistory: `${API_PREFIX}/history/add`, diff --git a/app/pages/Search.tsx b/app/pages/Search.tsx index 8f880b9..47d61d3 100644 --- a/app/pages/Search.tsx +++ b/app/pages/Search.tsx @@ -4,16 +4,35 @@ import { useState, useEffect } from "react"; import { useSearchParams } from "next/navigation"; import { useRouter } from "next/navigation"; -import { - Button, - Dropdown, - DropdownItem, - Modal, - ModalBody, - ModalFooter, - ModalHeader, -} from "flowbite-react"; +import { Dropdown, DropdownItem } from "flowbite-react"; import { useUserStore } from "#/store/auth"; +import { ENDPOINTS } from "#/api/config"; +import { tryCatchAPI } from "#/api/utils"; +import useSWRInfinite from "swr/infinite"; +import { Spinner } from "#/components/Spinner/Spinner"; +import { ReleaseSection } from "#/components/ReleaseSection/ReleaseSection"; +import { UserSection } from "#/components/UserSection/UserSection"; +import { CollectionsSection } from "#/components/CollectionsSection/CollectionsSection"; +import { useScrollPosition } from "#/hooks/useScrollPosition"; +import { RelatedSection } from "#/components/RelatedSection/RelatedSection"; + +const postFetcher = async (url: string, payload: string) => { + const { data, error } = await tryCatchAPI( + fetch(url, { + method: "POST", + headers: { + "Api-Version": "v2", + "Content-Type": "application/json", + }, + body: payload, + }) + ); + + if (error) { + throw error; + } + return data; +}; const whereMapping = [ { @@ -112,7 +131,9 @@ export function SearchPage() { const userStore = useUserStore(); const [query, setQuery] = useState(searchParams.get("query") || ""); const [params, setParams] = useState(null); - const [filtersModalOpen, setFiltersModalOpen] = useState(false); + const [content, setContent] = useState(null); + + const [HeaderH, setHeaderH] = useState(null); useEffect(() => { const queryParams = searchParams.get("params"); @@ -132,6 +153,11 @@ export function SearchPage() { searchBy: "name", }); } + + if (window) { + setHeaderH(document.querySelector("header").clientHeight); + } + // eslint-disable-next-line react-hooks/exhaustive-deps }, []); @@ -139,17 +165,115 @@ export function SearchPage() { if (!params) return; const url = new URL(`/search`, window.location.origin); + url.searchParams.set("query", query); url.searchParams.set("params", JSON.stringify(params)); router.replace(url.toString()); + setContent(null); // eslint-disable-next-line react-hooks/exhaustive-deps }, [params]); + useEffect(() => { + setContent(null); + + const url = new URL(`/search`, window.location.origin); + url.searchParams.set("query", query); + url.searchParams.set("params", JSON.stringify(params)); + router.replace(url.toString()); + }, [query]); + + const getKey = (pageIndex: number, previousPageData: any) => { + if (!params) return null; + if (!query) return null; + + if (previousPageData) { + if (params.where == "releases") { + if (!previousPageData.releases.length) return null; + } else { + if (!previousPageData.content.length) return null; + } + } + + let url = null; + switch (params.where) { + case "releases": + url = `${ENDPOINTS.search.releases}/${pageIndex}`; + break; + case "profiles": + url = `${ENDPOINTS.search.profiles}/${pageIndex}`; + break; + case "list": + const list = searchByMapping[params.where].find( + (item) => item.id == params.searchBy + ); + if (!list) break; + url = `${ENDPOINTS.search.profileList}/${list.value}/${pageIndex}`; + break; + case "history": + url = `${ENDPOINTS.search.profileHistory}/${pageIndex}`; + break; + case "favorites": + url = `${ENDPOINTS.search.profileFavorites}/${pageIndex}`; + break; + case "collections": + url = `${ENDPOINTS.search.profileFavoriteCollection}/${pageIndex}`; + break; + } + + if (userStore.token) { + url += `?token=${userStore.token}`; + } + + let searchBy = null; + const _sbym = searchByMapping[params.where]; + if (_sbym) { + searchBy = _sbym.find((item) => item.id == params.searchBy).value; + } else { + searchBy = searchByMapping["none"][0].value; + } + + return [url, JSON.stringify({ query, searchBy })]; + }; + + const { data, error, isLoading, size, setSize, mutate } = useSWRInfinite( + getKey, + ([url, payload]) => postFetcher(url, payload), + { initialSize: 2 } + ); + + useEffect(() => { + if (data) { + let _content = []; + if (params.where == "releases") { + for (let i = 0; i < data.length; i++) { + _content.push(...data[i].releases); + } + } else { + for (let i = 0; i < data.length; i++) { + _content.push(...data[i].content); + } + } + setContent(_content); + } + // eslint-disable-next-line react-hooks/exhaustive-deps + }, [data]); + + const scrollPosition = useScrollPosition(); + useEffect(() => { + if (scrollPosition >= 98 && scrollPosition <= 99) { + setSize(size + 1); + } + // eslint-disable-next-line react-hooks/exhaustive-deps + }, [scrollPosition]); + if (!params) return <>>; return (
query: {query}
-params: {JSON.stringify(params)}
+Произошла ошибка поиска
+Странно, аниме не найдено, попробуйте другой запрос...
+Введите ваш запрос что-бы найти любимый тайтл
+-// Произошла ошибка поиска. Попробуйте обновить страницу или зайдите -// позже. -//
-//Странно, аниме не найдено, попробуйте другой запрос...
-//Введите ваш запрос что-бы найти любимый тайтл
-//Искать в
-//Список
-//Искать по
-//