"use client"; import Image from "next/image"; import { Dispatch, SetStateAction, useCallback, useState, useEffect } from "react"; import BottomFooter from "@components/BottomFooter"; import { ExtendedArtefact } from "@appTypes/ApiTypes"; import { Currency } from "@appTypes/StoreModel"; import { useStoreState } from "@hooks/store"; export default function Shop() { const [artefacts, setArtefacts] = useState([]); const [loading, setLoading] = useState(true); // 3. Fetch from your API route and map data to fit your existing fields useEffect(() => { async function fetchArtefacts() { setLoading(true); try { const res = await fetch("/api/artefacts"); const data = await res.json(); const transformed = data.artefact.map((a: any) => ({ id: a.id, name: a.name, description: a.description, location: a.warehouseArea, // your database earthquakeID: a.earthquakeId?.toString() ?? "", observatory: a.type ?? "", // if you want to display type dateReleased: a.createdAt ? new Date(a.createdAt).toLocaleDateString() : "", image: "/artefactImages/" + (a.imageName || "NoImageFound.PNG"), price: a.shopPrice ?? 100, // fallback price if not in DB })); setArtefacts(transformed); } catch (e) { // Optionally handle error console.error("Failed to fetch artefacts", e); } finally { setLoading(false); } } fetchArtefacts(); }, []); const [currentPage, setCurrentPage] = useState(1); const [selectedArtefact, setSelectedArtefact] = useState(null); const [showPaymentModal, setShowPaymentModal] = useState(false); const [artefactToBuy, setArtefactToBuy] = useState(null); const [showThankYouModal, setShowThankYouModal] = useState(false); const [orderNumber, setOrderNumber] = useState(null); const artefactsPerPage = 12; const indexOfLastArtefact = currentPage * artefactsPerPage; const indexOfFirstArtefact = indexOfLastArtefact - artefactsPerPage; const currentArtefacts = artefacts.slice(indexOfFirstArtefact, indexOfLastArtefact); const selectedCurrency = useStoreState((state) => state.currency.selectedCurrency); const conversionRates = useStoreState((state) => state.currency.conversionRates); const currencyTickers = useStoreState((state) => state.currency.tickers); const convertPrice = useCallback( (price: number, currency: Currency) => (price * conversionRates[currency]).toFixed(2), [conversionRates] ); const handleNextPage = () => { if (indexOfLastArtefact < artefacts.length) setCurrentPage((prev) => prev + 1); }; const handlePreviousPage = () => { if (currentPage > 1) setCurrentPage((prev) => prev - 1); }; function ArtefactCard({ artefact }: { artefact: ExtendedArtefact }) { return (
setSelectedArtefact(artefact)} > {artefact.name}

{artefact.name}

{artefact.location}

{artefact.earthquakeID}

{currencyTickers[selectedCurrency]} {convertPrice(artefact.price, selectedCurrency)}

); } function Modal({ artefact }: { artefact: ExtendedArtefact }) { if (!artefact) return null; const handleOverlayClick = (e: React.MouseEvent) => { if (e.target === e.currentTarget) setSelectedArtefact(null); }; return (

{artefact.name}

{artefact.name}

{currencyTickers[selectedCurrency]} {convertPrice(artefact.price, selectedCurrency)}

{artefact.description}

Location: {artefact.location}

{artefact.earthquakeID}

{artefact.observatory}

{artefact.dateReleased}

); } function PaymentModal({ artefact, onClose }: { artefact: Artefact; onClose: () => void }) { const [cardNumber, setCardNumber] = useState(""); const [expiry, setExpiry] = useState(""); const [cvc, setCvc] = useState(""); const [name, setName] = useState(""); const [email, setEmail] = useState(""); const [remember, setRemember] = useState(false); const [error, setError] = useState(""); function validateEmail(email: string) { return ( email.includes("@") && (email.endsWith(".com") || email.endsWith(".co.uk") || email.endsWith(".org") || email.endsWith(".org.uk")) ); } function validateCardNumber(number: string) { return /^\d{12,19}$/.test(number.replace(/\s/g, "")); // 12-19 digits } function validateCVC(number: string) { return /^\d{3,4}$/.test(number); } function validateExpiry(exp: string) { return /^\d{2}\/\d{2}$/.test(exp); } function handlePay() { setError(""); if (!validateEmail(email)) { setError("Please enter a valid email ending"); return; } if (!validateCardNumber(cardNumber)) { setError("Card number must be 12-19 digits."); return; } if (!validateExpiry(expiry)) { setError("Expiry must be in MM/YY format."); return; } if (!validateCVC(cvc)) { setError("CVC must be 3 or 4 digits."); return; } // todo create receiving api route // todo handle sending to api route // todo remove order number generation - we don't need one // todo add option to save details in new account const genOrder = () => "#" + Math.random().toString(36).substring(2, 10).toUpperCase(); setOrderNumber(genOrder()); onClose(); setShowThankYouModal(true); } const handleOverlayClick = (e: React.MouseEvent) => { if (e.target === e.currentTarget) onClose(); }; return (

Buy {artefact.name}

{/* ...Image... */}
{ e.preventDefault(); handlePay(); }} > setEmail(e.target.value)} type="email" required autoFocus /> setName(e.target.value)} required /> setCardNumber(e.target.value.replace(/\D/g, ""))} maxLength={19} required inputMode="numeric" pattern="\d*" />
setExpiry(e.target.value.replace(/[^0-9/]/g, ""))} maxLength={5} required inputMode="numeric" /> setCvc(e.target.value.replace(/\D/g, ""))} maxLength={4} required inputMode="numeric" />
{error &&

{error}

}
); } function ThankYouModal({ orderNumber, onClose }: { orderNumber: string; onClose: () => void }) { const handleOverlayClick = (e: React.MouseEvent) => { if (e.target === e.currentTarget) onClose(); }; return (

Thank you for your purchase!

Your order number is:

{orderNumber}

); } return (

Artefact Shop

Discover extraordinary artefacts and collectibles from major seismic events from around the world - Previously studied by our scientists, now available for purchase.

{currentArtefacts.map((artefact) => ( ))}

{currentPage}

{selectedArtefact && } {artefactToBuy && showPaymentModal && ( { setShowPaymentModal(false); setArtefactToBuy(null); }} /> )} {showThankYouModal && orderNumber && ( setShowThankYouModal(false)} /> )} {!selectedArtefact && !showPaymentModal && !showThankYouModal && (
)}
);