import { useEffect, useState } from "react";
import getAllEvents from "../../utils/events/getAllEvents";

function EventsCounter() {
  const [eventsCount, setEventsCount] = useState(0);
  const [loading, setLoading] = useState(true);
  useEffect(() => {
    const fetchEvents = async () => {
      try {
        const events = await getAllEvents();
        if (Array.isArray(events)) {
          setEventsCount(events.length);
        } else {
          setEventsCount(0);
        }
      } catch (err) {
        console.error(err);
        setEventsCount(0);
      } finally {
        setLoading(false);
      }
    };
    fetchEvents();
  }, []);
  if (loading) return <p>Chargement...</p>;
  return <p>Nombre d’événements : {eventsCount}</p>;
}
export default EventsCounter;
