+
Son Kaza Geçmişi
@@ -124,146 +119,69 @@ export function renderKaza(pb) {
return {
html,
initEvents: (container) => {
- const closeBtn = container.querySelector("#closeBtn");
- if (closeBtn) {
- closeBtn.addEventListener("click", () => {
- const event = new CustomEvent("navigate", { detail: "desktop" });
- window.dispatchEvent(event);
- });
- }
+ container.querySelector("#closeBtn")?.addEventListener("click", () => {
+ window.dispatchEvent(new CustomEvent("navigate", { detail: "desktop" }));
+ });
if (!birthDate) {
- const form = container.querySelector("#profileForm");
- const genderSelect = container.querySelector("#gender");
- if (gender && genderSelect) {
- genderSelect.value = gender;
- }
- if (form) {
- form.addEventListener("submit", async (e) => {
- e.preventDefault();
- const birthDateInput = container.querySelector("#birthDate").value;
- const genderInput = container.querySelector("#gender").value;
+ container.querySelector("#profileForm")?.addEventListener("submit", async (e) => {
+ e.preventDefault();
+ const bInput = container.querySelector("#birthDateInput").value;
+ const gInput = container.querySelector("#genderInput").value;
- if (!birthDateInput) {
- alert("Lütfen doğum tarihinizi girin.");
- return;
- }
-
- try {
- await pb.collection("users").update(pb.authStore.model.id, {
- birthDate: birthDateInput,
- gender: genderInput,
- });
- } catch (err) {
- console.error("PocketBase kaydedilemedi:", err);
- }
-
- profile = {
- gender: genderInput,
- logs: profile?.logs || [],
- todayLogs: profile?.todayLogs || [],
- createdAt: profile?.createdAt || new Date().toISOString(),
- };
- localStorage.setItem("kaza_profile", JSON.stringify(profile));
-
- const event = new CustomEvent("navigate", { detail: "kaza" });
- window.dispatchEvent(event);
- });
- }
+ try {
+ await pb.collection("users").update(pb.authStore.model.id, {
+ birthDate: bInput,
+ gender: gInput,
+ });
+ pb.authStore.model.birthDate = bInput;
+ pb.authStore.model.gender = gInput;
+ window.dispatchEvent(new CustomEvent("navigate", { detail: "kaza" }));
+ } catch (err) {
+ alert("Profil kaydedilemedi. PocketBase bağlantısını kontrol edin.");
+ }
+ });
} else {
- refreshKazaStats(container, pb, birthDate, gender, profile);
+ refreshKazaStats(container, pb, birthDate, gender);
- const addBtn = container.querySelector("#addKazaBtn");
- const removeBtn = container.querySelector("#removeKazaBtn");
+ container.querySelector("#addKazaBtn")?.addEventListener("click", async () => {
+ await handleKazaLog(container, pb, birthDate, gender, 1);
+ });
- if (addBtn) {
- addBtn.addEventListener("click", async () => {
- const prayerId = container.querySelector("#kazaPrayerType").value;
- const countInput = container.querySelector("#kazaCount");
- const count = Math.max(1, parseInt(countInput?.value || "1", 10));
- const msg = container.querySelector("#kazaMsg");
-
- try {
- await pb.collection("kaza_logs").create({
- user: pb.authStore.model.id,
- prayerId,
- count,
- date: new Date().toISOString().split("T")[0],
- });
-
- showKazaMsg(msg, `${count} kaza namaz eklendi!`, true);
- if (countInput) countInput.value = "1";
- await refreshKazaStats(container, pb, birthDate, gender, profile);
- } catch (err) {
- console.error("Kaza eklenemedi:", err);
- showKazaMsg(
- msg,
- "Eklenemedi. PocketBase bağlantısını kontrol et.",
- false,
- );
- }
- });
- }
-
- if (removeBtn) {
- removeBtn.addEventListener("click", async () => {
- const prayerId = container.querySelector("#kazaPrayerType").value;
- const countInput = container.querySelector("#kazaCount");
- const count = Math.max(1, parseInt(countInput?.value || "1", 10));
- const msg = container.querySelector("#kazaMsg");
-
- try {
- const records = await pb.collection("kaza_logs").getFullList({
- filter: `user = "${pb.authStore.model.id}" && prayerId = "${prayerId}"`,
- sort: "-createdAt",
- });
-
- let remaining = count;
- for (const record of records) {
- if (remaining <= 0) break;
- const recordCount = record.count || 1;
- if (recordCount > remaining) {
- await pb.collection("kaza_logs").update(record.id, {
- count: recordCount - remaining,
- });
- remaining = 0;
- } else {
- await pb.collection("kaza_logs").delete(record.id);
- remaining -= recordCount;
- }
- }
-
- showKazaMsg(msg, `${count} kaza namaz çıkarıldı!`, true);
- if (countInput) countInput.value = "1";
- await refreshKazaStats(container, pb, birthDate, gender, profile);
- } catch (err) {
- console.error("Kaza çıkarılamadı:", err);
- showKazaMsg(
- msg,
- "Çıkarılamadı. PocketBase bağlantısını kontrol et.",
- false,
- );
- }
- });
- }
+ container.querySelector("#removeKazaBtn")?.addEventListener("click", async () => {
+ await handleKazaLog(container, pb, birthDate, gender, -1);
+ });
}
},
};
}
-async function refreshKazaStats(container, pb, birthDate, gender, profile) {
- if (!birthDate) return;
+async function handleKazaLog(container, pb, birthDate, gender, multiplier) {
+ const prayerId = container.querySelector("#kazaPrayerType").value;
+ const countInput = container.querySelector("#kazaCount");
+ const count = Math.max(1, parseInt(countInput?.value || "1", 10)) * multiplier;
+ const msg = container.querySelector("#kazaMsg");
- const calc = calculateMissedPrayers(birthDate, gender);
- const hijriBirth = getHijriBirthDate(birthDate);
+ try {
+ await pb.collection("kaza_logs").create({
+ user: pb.authStore.model.id,
+ prayerId,
+ count,
+ date: new Date().toISOString().split("T")[0],
+ });
- const birthDateEl = container.querySelector("#hijriBirthDateDisplay");
- if (birthDateEl) {
- birthDateEl.textContent = `${hijriBirth.day} ${hijriBirth.monthName} ${hijriBirth.year}`;
+ showKazaMsg(msg, count > 0 ? `🎉 ${count} vakit kaza kılındı olarak işlendi!` : `${Math.abs(count)} vakit düşüldü.`, true);
+ if (countInput) countInput.value = "1";
+ await refreshKazaStats(container, pb, birthDate, gender);
+ } catch (err) {
+ showKazaMsg(msg, "İşlem kaydedilemedi! PocketBase izinlerini veya bağlantısını kontrol edin.", false);
}
+}
- let logs = [];
+async function refreshKazaStats(container, pb, birthDate, gender) {
+ const calc = calculateMissedPrayers(birthDate, gender);
let logsByPrayer = {};
+ let logs = [];
try {
const records = await pb.collection("kaza_logs").getFullList({
@@ -271,60 +189,73 @@ async function refreshKazaStats(container, pb, birthDate, gender, profile) {
sort: "-createdAt",
});
- logs = records.map((r) => ({
- id: r.id,
- prayerId: r.prayerId,
- count: r.count || 1,
- date: r.date,
- createdAt: r.createdAt,
- }));
-
- logs.forEach((l) => {
- logsByPrayer[l.prayerId] =
- (logsByPrayer[l.prayerId] || 0) + (l.count || 1);
+ logs = records;
+ records.forEach((l) => {
+ logsByPrayer[l.prayerId] = (logsByPrayer[l.prayerId] || 0) + (l.count || 0);
});
} catch (err) {
- console.error("Loglar yüklenemedi:", err);
+ console.error("Loglar çekilemedi:", err);
}
+ let grandTotalMissed = 0;
+ let grandTotalCompleted = 0;
+
PRAYERS.forEach((p) => {
- const totalEl = container.querySelector(`#total-${p.id}`);
- const remainEl = container.querySelector(`#remaining-${p.id}`);
const missed = calc[p.id] || 0;
- const completed = logsByPrayer[p.id] || 0;
+ const completed = Math.max(0, logsByPrayer[p.id] || 0);
const remaining = Math.max(0, missed - completed);
- if (totalEl) totalEl.textContent = missed;
+ grandTotalMissed += missed;
+ grandTotalCompleted += completed;
+
+ const remainEl = container.querySelector(`#remaining-${p.id}`);
+ const progressEl = container.querySelector(`#progress-${p.id}`);
+
if (remainEl) remainEl.textContent = remaining;
+ if (progressEl) {
+ const pct = missed > 0 ? Math.min(100, Math.round((completed / missed) * 100)) : 100;
+ progressEl.style.width = `${pct}%`;
+ }
});
- const todayKey = new Date().toISOString().split("T")[0];
- const todayLogs = logs.filter((l) => l.date === todayKey);
- if (profile) {
- profile.todayLogs = todayLogs;
- localStorage.setItem("kaza_profile", JSON.stringify(profile));
- }
+ // Genel İlerleme Çubuğu ve Yüzde Hesaplama
+ const overallPct = grandTotalMissed > 0
+ ? Math.min(100, Math.round((grandTotalCompleted / grandTotalMissed) * 100))
+ : 100;
+ const bar = container.querySelector("#overallProgressBar");
+ const pctText = container.querySelector("#overallPercentage");
+ const compText = container.querySelector("#totalCompletedText");
+ const remText = container.querySelector("#totalRemainingText");
+
+ if (bar) bar.style.width = `${overallPct}%`;
+ if (pctText) pctText.textContent = `%${overallPct}`;
+ if (compText) compText.textContent = grandTotalCompleted;
+ if (remText) remText.textContent = Math.max(0, grandTotalMissed - grandTotalCompleted);
+
+ // İşlem Geçmişi
const logsContainer = container.querySelector("#logsContainer");
if (logsContainer) {
logsContainer.innerHTML =
logs
- .slice(0, 20)
+ .slice(0, 10)
.map((log) => {
const prayer = PRAYERS.find((p) => p.id === log.prayerId);
+ const isPlus = log.count > 0;
return `
-
-
${prayer?.name || log.prayerId} x${log.count} - ${log.date}
-
+${log.count}
+
+ ${prayer?.name || log.prayerId} - ${log.date || ''}
+ ${isPlus ? '+' : ''}${log.count}
`;
})
- .join("") || '
Henüz kayıt yok.
';
+ .join("") || '
Henüz kayıt bulunmuyor.
';
}
}
function showKazaMsg(el, text, success) {
+ if (!el) return;
el.textContent = text;
- el.className = `text-xs mt-2 ${success ? "text-emerald-400" : "text-rose-400"}`;
+ el.className = `text-xs mt-2.5 ${success ? "text-emerald-400 font-medium" : "text-rose-400"}`;
el.classList.remove("hidden");
-}
+}
\ No newline at end of file
diff --git a/src/utils/prayerTimes.js b/src/utils/prayerTimes.js
index b29f534..be90775 100755
--- a/src/utils/prayerTimes.js
+++ b/src/utils/prayerTimes.js
@@ -2,24 +2,16 @@ export async function fetchPrayerTimes(cityName = "Edremit", lat, lon) {
try {
let url;
if (lat !== undefined && lon !== undefined) {
- url =
- "https://api.aladhan.com/v1/timings?latitude=" +
- encodeURIComponent(lat) +
- "&longitude=" +
- encodeURIComponent(lon) +
- "&method=13";
+ url = `https://api.aladhan.com/v1/timings?latitude=${encodeURIComponent(lat)}&longitude=${encodeURIComponent(lon)}&method=13`;
} else {
- url =
- "https://api.aladhan.com/v1/timingsByCity?city=" +
- encodeURIComponent(cityName) +
- "&country=Turkey&method=2";
+ url = `https://api.aladhan.com/v1/timingsByCity?city=${encodeURIComponent(cityName)}&country=Turkey&method=13`;
}
const res = await fetch(url);
- if (!res.ok) {
- throw new Error("Namaz vakitleri alınamadı");
- }
+ 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,
@@ -28,9 +20,36 @@ export async function fetchPrayerTimes(cityName = "Edremit", lat, lon) {
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) => {
+ 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" };
+}