"use client" import { useState, useEffect } from "react" import OrderStats from "./order-stats" import { getGreeting } from "@/lib/utils" import { statsConfig } from "@/config/dashboard" import type { OrderStatsData } from "@/lib/types" import { Card, CardContent, CardDescription, CardHeader, CardTitle } from "@/components/ui/card" import { ShoppingCart } from "lucide-react" import { ArrowUpCircle, CreditCard, DollarSign, Users } from "lucide-react" import { Skeleton } from "@/components/ui/skeleton" interface ContentProps { username: string orderStats: OrderStatsData } interface TopProduct { id: string; name: string; price: number; image: string; count: number; revenue: number; } // Add quotes array const businessQuotes = [ { text: "Your most unhappy customers are your greatest source of learning.", author: "Bill Gates" }, { text: "The secret of getting ahead is getting started.", author: "Mark Twain" }, { text: "Success is not final; failure is not fatal: It is the courage to continue that counts.", author: "Winston Churchill" }, { text: "The way to get started is to quit talking and begin doing.", author: "Walt Disney" }, { text: "Opportunities don't happen. You create them.", author: "Chris Grosser" }, { text: "The best way to predict the future is to create it.", author: "Peter Drucker" }, { text: "Don't watch the clock; do what it does. Keep going.", author: "Sam Levenson" }, { text: "The future belongs to those who believe in the beauty of their dreams.", author: "Eleanor Roosevelt" }, { text: "Entrepreneurship is living a few years of your life like most people won't so you can spend the rest of your life like most people can't.", author: "Anonymous" }, { text: "Your work is going to fill a large part of your life, and the only way to be truly satisfied is to do what you believe is great work.", author: "Steve Jobs" } ]; export default function Content({ username, orderStats }: ContentProps) { const [greeting, setGreeting] = useState("") const [topProducts, setTopProducts] = useState([]) const [isLoading, setIsLoading] = useState(true) const [randomQuote, setRandomQuote] = useState(() => { const randomIndex = Math.floor(Math.random() * businessQuotes.length) return businessQuotes[randomIndex] }) useEffect(() => { setGreeting(getGreeting()) // Pick a random quote when component mounts const randomIndex = Math.floor(Math.random() * businessQuotes.length) setRandomQuote(businessQuotes[randomIndex]) // Fetch top products for the vendor const fetchTopProducts = async () => { try { // Check if we're in development or production const isDev = process.env.NODE_ENV === 'development'; // Use the internal API URL seen in the console const apiBaseUrl = 'https://internal-api.inboxi.ng/api'; console.log('Using API URL:', apiBaseUrl); // Get the auth token from cookies const cookies = document.cookie.split(';'); console.log('Cookies:', cookies); const tokenCookie = cookies.find(cookie => cookie.trim().startsWith('Authorization=')); let token = ''; if (tokenCookie) { // Extract just the token value after "Authorization=" token = tokenCookie.trim().substring(14); // 'Authorization='.length is 14 // Fix any potential malformed token (seen in your screenshot) if (token.startsWith('ization=')) { token = token.substring(9); // Remove the 'ization=' prefix if present } } console.log('Using token:', token); const headers = { 'Authorization': `Bearer ${token}` }; console.log('Request headers:', headers); const response = await fetch(`${apiBaseUrl}/orders/top-products`, { credentials: 'include', headers }); if (response.ok) { const data = await response.json(); setTopProducts(data); } else { const errorText = await response.text(); console.error(`Failed to fetch top products: ${response.status} ${response.statusText}`); console.error('Error details:', errorText); } } catch (error) { console.error("Error fetching top products:", error); } finally { setIsLoading(false); } }; fetchTopProducts(); }, []) return (

{greeting}, {username}!

"The secret of getting ahead is getting started." — Mark Twain

{statsConfig.map((stat) => ( ))}
{/* Best Selling Products Section */}
Your Best Selling Products Products with the highest sales from your store {isLoading ? (
{[...Array(5)].map((_, i) => (
))}
) : topProducts.length > 0 ? (
{topProducts.map(product => (
{product.image ? ( {product.name} ) : ( )}

{product.name}

{product.count} sold

))}
) : (

No sales data available yet

Your best-selling products will appear here once you have orders

)}
) }