feat: add user token to the search page while fetching data

This commit is contained in:
Kentai Radiquum 2024-07-19 06:07:16 +05:00
parent f066f6296c
commit 32fc2e534d
Signed by: Radiquum
GPG key ID: 858E8EE696525EED

View file

@ -7,6 +7,7 @@ import { useState, useEffect } from "react";
import { useScrollPosition } from "@/app/hooks/useScrollPosition";
import { useRouter } from "next/navigation";
import { useSearchParams } from "next/navigation";
import { useUserStore } from "../store/auth";
const fetcher = async (url) => {
const res = await fetch(url);
@ -25,6 +26,7 @@ export function SearchPage() {
const router = useRouter();
const searchParams = useSearchParams();
const [query, setQuery] = useState(searchParams.get("q") || null);
const token = useUserStore((state) => state.token);
const getKey = (pageIndex, previousPageData) => {
if (previousPageData && !previousPageData.releases.length) return null;
@ -32,6 +34,10 @@ export function SearchPage() {
const url = new URL("/api/search", window.location.origin);
url.searchParams.set("page", pageIndex);
if (token) {
url.searchParams.set("token", token);
}
if (query) {
url.searchParams.set("q", query);
return url.toString();