55 lines
1.7 KiB
JavaScript
55 lines
1.7 KiB
JavaScript
// hooks/usePoiTypData.js
|
|
import { useState, useEffect, useRef } from "react";
|
|
import { toast } from "react-toastify"; // Toast für Warnungen importieren
|
|
|
|
const usePoiTypData = (url) => {
|
|
const [poiTypData, setPoiTypData] = useState([]);
|
|
const [isPoiTypLoaded, setIsPoiTypLoaded] = useState(false);
|
|
const retryCountRef = useRef(0);
|
|
const maxRetries = 2;
|
|
|
|
useEffect(() => {
|
|
const fetchPoiTypData = async () => {
|
|
try {
|
|
const response = await fetch(url);
|
|
const data = await response.json();
|
|
|
|
if (!Array.isArray(data)) {
|
|
console.warn(`Unerwartetes Format: ${JSON.stringify(data)}`);
|
|
|
|
if (data.warning) {
|
|
toast.warn(data.warning, { position: "top-center", autoClose: 5000 });
|
|
setPoiTypData([]); // Leeres Array setzen
|
|
setIsPoiTypLoaded(true);
|
|
return;
|
|
}
|
|
|
|
throw new Error("Daten sind kein Array");
|
|
}
|
|
|
|
// Erfolgreich geladen, also Reset des Retry-Zählers
|
|
setPoiTypData(data);
|
|
setIsPoiTypLoaded(true);
|
|
retryCountRef.current = 0;
|
|
} catch (error) {
|
|
console.error("Fehler beim Abrufen der poiTyp-Daten in usePoiTypData.js :", error);
|
|
|
|
if (retryCountRef.current < maxRetries) {
|
|
retryCountRef.current++;
|
|
console.log(`Neuer Versuch (${retryCountRef.current}/${maxRetries}) in 5 Sekunden...`);
|
|
setTimeout(fetchPoiTypData, 5000);
|
|
} else {
|
|
console.error("Maximale Anzahl an Fehlversuchen erreicht. Stoppe weitere Abrufe.");
|
|
setIsPoiTypLoaded(true);
|
|
}
|
|
}
|
|
};
|
|
|
|
fetchPoiTypData();
|
|
}, [url]);
|
|
|
|
return { poiTypData, isPoiTypLoaded };
|
|
};
|
|
|
|
export default usePoiTypData;
|