"use client"; import { useEffect, useState } from "react"; import Settings from "./store-settings"; import { useRouter } from "next/navigation"; import axios from "axios"; const API_URL = "https://ebay.backend.data4autos.com/api/ebay/inventory/locations"; export default function Page() { const router = useRouter() const [locations, setLocations] = useState([]); const [loading, setLoading] = useState(true); const [selectedKey, setSelectedKey] = useState(null); const [showForm, setShowForm] = useState(false); const [payment, setPayment] = useState(null); useEffect(() => { const role = localStorage.getItem("user_role"); const sessionId = localStorage.getItem("payment_session"); // ✅ Admins and Partners can access directly (skip payment check) if (role === "admin" || role === "partner") { return; } // 🚫 If no payment session, redirect to pricing if (!sessionId) { router.push("/pricing"); return; } // ✅ Otherwise, check payment details const fetchPaymentDetails = async () => { try { const res: any = await axios.get( "https://ebay.backend.data4autos.com/api/payment/details", { params: { session_id: sessionId } } ); setPayment(res.data.payment); } catch (err) { console.error("Error fetching payment details:", err); } }; fetchPaymentDetails(); }, [router]); useEffect(() => { const fetchLocations = async () => { try { const userId = typeof window !== 'undefined' ? sessionStorage.getItem('USERID') : null; const EBAYSTOREID = typeof window !== 'undefined' ? sessionStorage.getItem('EBAYSTOREID') : null; const res = await fetch(API_URL, { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ accessToken: "clientaccesstoken", userKey: EBAYSTOREID }), }); const data = await res.json(); setLocations(data.locations || []); } catch (err) { console.error("❌ Error fetching locations:", err); } finally { setLoading(false); } }; fetchLocations(); }, []); const handleSaveExisting = () => { const selected = locations.find((loc) => loc.merchantLocationKey === selectedKey); console.log("✅ Selected Location:", selected); }; const handleSaveNew = (payload: any) => { console.log("📦 New Location Payload:", payload); }; if (loading) return (
⏳ Loading eBay Locations...
); return (

eBay Locations

{!showForm ? ( <> {/* Locations Grid */}
{locations.map((loc, idx) => (
setSelectedKey(loc.merchantLocationKey)} className={`relative p-6 rounded-2xl border cursor-pointer transform transition-all duration-300 ${selectedKey === loc.merchantLocationKey ? "bg-gradient-to-br from-[#00d1ff] to-[#007bff] text-white shadow-lg scale-105" : "bg-white hover:shadow-xl hover:-translate-y-1 border-gray-200" }`} >

{loc.merchantLocationKey || "Unnamed Location"}

{selectedKey === loc.merchantLocationKey && ( Selected )}

📞 {loc.phone || "N/A"}

📍 {loc.location?.address?.addressLine1},{" "} {loc.location?.address?.city},{" "} {loc.location?.address?.stateOrProvince}{" "} {loc.location?.address?.postalCode}

Key: {loc.merchantLocationKey}
))}
{/* Buttons */}
) : ( )}
); }