From 24e6009b81a3e1b31beb2f037d9ba631d34fa6d9 Mon Sep 17 00:00:00 2001 From: Kentai Radiquum Date: Wed, 20 Nov 2024 19:52:43 +0500 Subject: [PATCH] feat: add search in lists on search page --- app/pages/Search.tsx | 152 ++++++++++++++++++++++++++++++++++++++----- 1 file changed, 136 insertions(+), 16 deletions(-) diff --git a/app/pages/Search.tsx b/app/pages/Search.tsx index fa3db4c..387bedb 100644 --- a/app/pages/Search.tsx +++ b/app/pages/Search.tsx @@ -8,7 +8,7 @@ import { useScrollPosition } from "#/hooks/useScrollPosition"; import { useRouter } from "next/navigation"; import { useSearchParams } from "next/navigation"; import { useUserStore } from "../store/auth"; -import { Button, Modal } from "flowbite-react"; +import { Button, Dropdown, Modal } from "flowbite-react"; const fetcher = async (url: string) => { const res = await fetch(url); @@ -24,6 +24,29 @@ const fetcher = async (url: string) => { return res.json(); }; +const ListsMapping = { + watching: { + name: "Смотрю", + id: 1 + }, + planned: { + name: "В планах", + id: 2 + }, + watched: { + name: "Просмотрено", + id: 3 + }, + delayed: { + name: "Отложено", + id: 4 + }, + abandoned: { + name: "Заброшено", + id: 5 + }, +}; + export function SearchPage() { const router = useRouter(); const searchParams = useSearchParams(); @@ -33,10 +56,10 @@ export function SearchPage() { const [searchBy, setSearchBy] = useState( searchParams.get("searchBy") || null ); - const [list, setList] = useState(searchParams.get("list") || null); + const [list, setList] = useState(searchParams.get("list") || "watching"); const [filtersModalOpen, setFiltersModalOpen] = useState(false); - const token = useUserStore((state) => state.token); + const userStore = useUserStore(); const getKey = (pageIndex: number, previousPageData: any) => { if (where == "list") { @@ -48,8 +71,8 @@ export function SearchPage() { const url = new URL("/api/search", window.location.origin); url.searchParams.set("page", pageIndex.toString()); - if (token) { - url.searchParams.set("token", token); + if (userStore.token) { + url.searchParams.set("token", userStore.token); } if (where) { @@ -60,8 +83,8 @@ export function SearchPage() { url.searchParams.set("searchBy", searchBy); } - if (list) { - url.searchParams.set("list", list); + if (where == "list" && list && ListsMapping.hasOwnProperty(list)) { + url.searchParams.set("list", ListsMapping[list].id); } if (query) { @@ -170,7 +193,13 @@ export function SearchPage() { - +
{data && data[0].related && } @@ -215,20 +244,111 @@ export function SearchPage() { ) : ( "" )} - + ); } -const FiltersModal = (props: {isOpen: boolean; setIsOpen: any}) => { +const FiltersModal = (props: { + isOpen: boolean; + setIsOpen: any; + where: string; + setWhere: any; + list: string; + setList: any; + isAuth: boolean; +}) => { + + const router = useRouter(); + const [where, setWhere] = useState(props.where); + const [list, setList] = useState(props.list); + + function _cancel() { + setWhere(props.where); + setList(props.list); + props.setIsOpen(false) + } + + function _accept() { + const Params = new URLSearchParams(window.location.search); + + if (props.where != where) { + Params.set("where", where); + props.setWhere(where); + } + + if (where == "list") { + Params.set("list", list); + props.setList(list); + } else { + Params.delete("list"); + } + + const url = new URL(`/search?${Params.toString()}`, window.location.origin); + router.push(url.toString()); + } + return ( - props.setIsOpen(false)} - size={"7xl"} - > + _cancel()}> Фильтры + +
+
+

Искать в

+ + + + {/* */} + +
+
+ {props.isAuth && where == "list" && ListsMapping.hasOwnProperty(list) ? ( +
+
+

Список

+ + setList("watching")}>Смотрю + setList("planned")}>В планах + setList("watched")}>Просмотрено + setList("delayed")}>Отложено + setList("abandoned")}>Брошено + +
+
+ ) : ( + "" + )} +
+ +
+ + +
+
); };