diff --git a/app/api/search/route.js b/app/api/search/route.js index f96747d..6f3912f 100644 --- a/app/api/search/route.js +++ b/app/api/search/route.js @@ -12,7 +12,7 @@ export async function GET(request) { } const data = { query, searchBy: 0 }; - const response = await fetchDataViaPost(url.toString(), data); + const response = await fetchDataViaPost(url.toString(), data, true); if (!response) { return NextResponse.json({ message: "Bad request" }, { status: 400 }); } diff --git a/app/api/utils.js b/app/api/utils.js index 3d44158..5c6b1ee 100644 --- a/app/api/utils.js +++ b/app/api/utils.js @@ -4,7 +4,10 @@ export const HEADERS = { "Content-Type": "application/json; charset=UTF-8", }; -export const fetchDataViaGet = async (url) => { +export const fetchDataViaGet = async (url, API_V2) => { + if (API_V2) { + HEADERS["API-Version"] = "v2"; + } try { const response = await fetch(url, { headers: HEADERS, @@ -19,7 +22,10 @@ export const fetchDataViaGet = async (url) => { } }; -export const fetchDataViaPost = async (url, body) => { +export const fetchDataViaPost = async (url, body, API_V2) => { + if (API_V2) { + HEADERS["API-Version"] = "v2"; + } try { const response = await fetch(url, { method: "POST", @@ -66,3 +72,11 @@ export function getJWT() { export function removeJWT() { localStorage.removeItem("JWT"); } + +export function numberDeclension(number, one, two, five) { + if (number > 10 && [11, 12, 13, 14].includes(number%100)) return five; + let last_num = number%10; + if (last_num == 1) return one; + if ([2,3,4].includes(last_num)) return two; + if ([5,6,7,8,9, 0].includes(last_num)) return five; +} diff --git a/app/components/RelatedSection/RelatedSection.jsx b/app/components/RelatedSection/RelatedSection.jsx new file mode 100644 index 0000000..2154764 --- /dev/null +++ b/app/components/RelatedSection/RelatedSection.jsx @@ -0,0 +1,41 @@ +import { numberDeclension } from "@/app/api/utils"; +import Link from "next/link"; + +export const RelatedSection = (props) => { + const declension = numberDeclension( + props.release_count, + "релиз", + "релиза", + "релизов" + ); + return ( +
+
+
+ {props.images.map((item) => { + return ( + + ); + })} +
+
+

{props.name_ru}

+

+ {props.release_count} {declension} во франшизе +

+ +
+

Перейти

+ +
+ +
+
+
+ ); +}; diff --git a/app/pages/Search.jsx b/app/pages/Search.jsx index 047a62d..32d5767 100644 --- a/app/pages/Search.jsx +++ b/app/pages/Search.jsx @@ -1,6 +1,7 @@ "use client"; import useSWRInfinite from "swr/infinite"; import { ReleaseSection } from "@/app/components/ReleaseSection/ReleaseSection"; +import { RelatedSection } from "@/app/components/RelatedSection/RelatedSection"; import { Spinner } from "@/app/components/Spinner/Spinner"; import { useState, useEffect } from "react"; import { useScrollPosition } from "@/app/hooks/useScrollPosition"; @@ -26,7 +27,7 @@ export function SearchPage() { const [query, setQuery] = useState(searchParams.get("q") || null); const getKey = (pageIndex, previousPageData) => { - if (previousPageData && !previousPageData.content.length) return null; + if (previousPageData && !previousPageData.releases.length) return null; const url = new URL("/api/search", window.location.origin); url.searchParams.set("page", pageIndex); @@ -49,7 +50,7 @@ export function SearchPage() { if (data) { let allReleases = []; for (let i = 0; i < data.length; i++) { - allReleases.push(...data[i].content); + allReleases.push(...data[i].releases); } setContent(allReleases); } @@ -117,6 +118,7 @@ export function SearchPage() {
+ {data && data[0].related && } {content ? ( content.length > 0 ? ( @@ -140,7 +142,7 @@ export function SearchPage() {
)} - {data && data[data.length - 1].content.length == 25 && ( + {data && data[data.length - 1].releases.length == 25 && (