import React from "react";

export const useFetchEmailCampaigns = () => {
  const [campaigns, setCampaigns] = useState([]);
  const [loading, setLoading] = useState(false);

  const fetchData = async () => {
    setLoading(true);
    const token = "mtp_GbXcgngXzBqeBnc4vXvR";
    const url = "/v2/api/tools/email-tracking/";
    try {
      const response = await request.get(url, {
        headers: {
          "Content-Type": "application/json",
          Authorization: `Bearer ${token}`,
        },
      });
      setLoading(false);
      console.log("Data posted successfully:", response.data);
      setCampaigns(response?.data?.list);
    } catch (error) {
      setLoading(false);
      if (error.response) {
        console.error("Error response:", error.response.data);
        console.error("Error status:", error.response.status);
        console.error("Error headers:", error.response.headers);
      } else if (error.request) {
        // The request was made but no response was received
        console.error("Error request:", error.request);
      } else {
        // Something happened in setting up the request that triggered an Error
        console.error("Error message:", error.message);
      }
      console.error("Error config:", error.config);
    }
  };

  // useEffect(() => {
  //   fetchData();
  // }, []);

  return { campaigns, loading, refetch: fetchData };
};
