Files
frontend-app/src/utils/prayerTimes.js
T

75 lines
2.2 KiB
JavaScript
Executable File
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { getCityByName } from "./locations.js";
export async function fetchPrayerTimes(cityName = "Edremit", lat, lon) {
try {
let fetchLat = lat;
let fetchLon = lon;
// Eğer enlem/boylam verilmediyse şehir listemizden koordinatları çekiyoruz
if (fetchLat === undefined || fetchLon === undefined) {
const cityObj = getCityByName(cityName);
if (cityObj) {
fetchLat = cityObj.lat;
fetchLon = cityObj.lon;
}
}
let url;
if (fetchLat !== undefined && fetchLon !== undefined) {
// Diyanet İşleri Başkanlığı Metodu = 13
url = `https://api.aladhan.com/v1/timings?latitude=${fetchLat}&longitude=${fetchLon}&method=13`;
} else {
url = `https://api.aladhan.com/v1/timingsByCity?city=Edremit&country=Turkey&method=13`;
}
const res = await fetch(url);
if (!res.ok) {
throw new Error("Namaz vakitleri alınamadı");
}
const data = await res.json();
const t = data.data.timings;
return {
fajr: t.Fajr,
sunrise: t.Sunrise,
dhuhr: t.Dhuhr,
asr: t.Asr,
maghrib: t.Maghrib,
isha: t.Isha,
imsak: t.Imsak,
currentPrayer: getCurrentPrayer(t),
};
} catch (err) {
console.error("Namaz vakti hatası:", err);
return null;
}
}
function getCurrentPrayer(timings) {
const now = new Date();
const currentMinutes = now.getHours() * 60 + now.getMinutes();
const parseTime = (timeStr) => {
if (!timeStr) return 0;
const [h, m] = timeStr.split(":").map(Number);
return h * 60 + m;
};
const imsak = parseTime(timings.Imsak);
const dhuhr = parseTime(timings.Dhuhr);
const asr = parseTime(timings.Asr);
const maghrib = parseTime(timings.Maghrib);
const isha = parseTime(timings.Isha);
if (currentMinutes >= imsak && currentMinutes < dhuhr)
return { name: "Sabah", key: "fajr" };
if (currentMinutes >= dhuhr && currentMinutes < asr)
return { name: "Öğle", key: "dhuhr" };
if (currentMinutes >= asr && currentMinutes < maghrib)
return { name: "İkindi", key: "asr" };
if (currentMinutes >= maghrib && currentMinutes < isha)
return { name: "Akşam", key: "maghrib" };
return { name: "Yatsı", key: "isha" };
}