import { useEffect, useState } from "react"; import { useParams } from "react-router-dom"; import { collection, getDocs, query, where, orderBy, limit, startAfter, } from "firebase/firestore"; import { db } from "../firebase.config"; import { toast } from "react-toastify"; import Spinner from "../components/Spinner"; import { async } from "@firebase/util"; import Listing from "../components/ListingItem"; import ListingItem from "../components/ListingItem"; function Category() { const [listings, setListings] = useState(null); const [loading, setLoading] = useState(true); const params = useParams(); useEffect(() => { const fetchListings = async () => { try { const listingsRef = collection(db, "listings"); const q = query( listingsRef, where("type", "==", params.categoryName), orderBy("timestamp", "desc"), limit(10) ); const querySnap = await getDocs(q); let listings = []; querySnap.forEach((doc) => { return listings.push({ id: doc.id, data: doc.data(), }); }); setListings(listings); setLoading(false); } catch (error) { console.log(error) toast.error("could not fetch listings"); } }; fetchListings(); }, [params.categoryName]); return (

{params.categoryName === "rent" ? "Places for rent" : "Places for sale"}

{loading ? ( ) : listings && listings.length > 0 ? ( <>
    {listings.map((listing) => ( ))}
) : (

No listings for {params.categoryName}

)}
); } export default Category;