fix
This commit is contained in:
@@ -10,6 +10,7 @@ import { ShoppingCart, RefreshCcw } from "lucide-react"
|
||||
import { Button } from "@/components/ui/button"
|
||||
import { useToast } from "@/components/ui/use-toast"
|
||||
import { Skeleton } from "@/components/ui/skeleton"
|
||||
import { clientFetch } from "@/lib/client-utils"
|
||||
|
||||
interface ContentProps {
|
||||
username: string
|
||||
@@ -90,35 +91,11 @@ export default function Content({ username, orderStats }: ContentProps) {
|
||||
const [randomQuote, setRandomQuote] = useState(getRandomQuote())
|
||||
|
||||
const fetchTopProducts = async () => {
|
||||
setIsLoading(true)
|
||||
setError(null)
|
||||
|
||||
try {
|
||||
// Get the auth token from cookies
|
||||
const authToken = document.cookie
|
||||
.split("; ")
|
||||
.find((row) => row.startsWith("Authorization="))
|
||||
?.split("=")[1];
|
||||
setIsLoading(true)
|
||||
|
||||
if (!authToken) {
|
||||
throw new Error("Authentication token not found")
|
||||
}
|
||||
|
||||
// Use the API URL from environment variables
|
||||
const apiUrl = `${process.env.NEXT_PUBLIC_API_URL}/orders/top-products`;
|
||||
|
||||
const response = await fetch(apiUrl, {
|
||||
headers: {
|
||||
Authorization: `Bearer ${authToken}`,
|
||||
'Content-Type': 'application/json'
|
||||
}
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error(`API request failed: ${response.status} ${response.statusText}`)
|
||||
}
|
||||
|
||||
const data = await response.json();
|
||||
// Use clientFetch to handle URL routing and authentication properly
|
||||
const data = await clientFetch('/orders/top-products');
|
||||
setTopProducts(data);
|
||||
} catch (err) {
|
||||
console.error("Error fetching top products:", err);
|
||||
|
||||
Reference in New Issue
Block a user