From 171e6a90bea1395cb1d7791126c5afc831733669 Mon Sep 17 00:00:00 2001 From: iontalp Date: Fri, 31 Jul 2026 05:03:10 +0200 Subject: [PATCH] =?UTF-8?q?ilerleme=20=C3=A7ubuklar=C4=B1=20d=C3=BCzeltild?= =?UTF-8?q?i.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/pages/kaza.js | 60 +++++++++++++++++------------------------------ 1 file changed, 21 insertions(+), 39 deletions(-) diff --git a/src/pages/kaza.js b/src/pages/kaza.js index 7f5b9f4..09f99e6 100755 --- a/src/pages/kaza.js +++ b/src/pages/kaza.js @@ -68,7 +68,7 @@ export function renderKaza(pb) {
- 📊 KAZA BORCU DURUMU + 📊 KAZA BORCU TAMAMLAMA TAMAMLANMA ORANI %0 Kılındı
@@ -155,13 +155,12 @@ export function renderKaza(pb) { } else { refreshKazaStats(container, pb); - // Hesapla ve PB'ye Ilk Borc Olarak Kaydet container .querySelector("#calcAndSaveBtn") ?.addEventListener("click", async () => { if ( confirm( - "Doğum tarihinize göre kaza borcunuz hesaplanıp PocketBase veritabanına kaydedilecek. Var olan borç verileriniz sıfırlanır. Onaylıyor musunuz?", + "Doğum tarihinize göre kaza borcunuz hesaplanıp PocketBase veritabanına kaydedilecek. Onaylıyor musunuz?", ) ) { const res = await initAndSaveInitialKaza(pb, birthDate, gender); @@ -169,7 +168,7 @@ export function renderKaza(pb) { await refreshKazaStats(container, pb); showKazaMsg( container.querySelector("#kazaMsg"), - `✓ Borç hesaplandı (${res.days} gün) ve PocketBase'e başarıyla kaydedildi!`, + `✓ Borç hesaplandı (${res.days} gün) ve PocketBase'e kaydedildi!`, true, ); } else { @@ -182,14 +181,12 @@ export function renderKaza(pb) { } }); - // Kildim (Borctan Dus / Eksi Deger) container .querySelector("#subtractKazaBtn") ?.addEventListener("click", async () => { await updateKazaDebt(container, pb, -1); }); - // Yanlislikla dusulduyse borca geri ekle (Artir / Artı Deger) container .querySelector("#addKazaBtn") ?.addEventListener("click", async () => { @@ -200,29 +197,26 @@ export function renderKaza(pb) { }; } -// PB tarafina ilk borclari kaydetme (target_* alanlarina da ilk borcu yedekliyoruz) async function initAndSaveInitialKaza(pb, birthDate, gender) { try { const calc = calculateMissedPrayers(birthDate, gender, 12); - + if (!calc || calc.days <= 0) { - return { - success: false, - error: - "Doğum tarihiniz henüz mükellefiyet yaşından (12 Hicri yıl) küçük.", + return { + success: false, + error: "Doğum tarihiniz henüz mükellefiyet yaşından (12 Hicri yıl) küçük." }; } const userId = pb.authStore.model.id; let userRecord = null; - + try { userRecord = await pb .collection("kaza_logs") .getFirstListItem(`user = "${userId}"`); } catch (e) {} - // Kaza borclarini sakliyoruz (fajr, dhuhr... doğrudan Kalan Borçtur) const initialData = { user: userId, fajr: calc.fajr, @@ -231,7 +225,6 @@ async function initAndSaveInitialKaza(pb, birthDate, gender) { maghrib: calc.maghrib, isha: calc.isha, vitr: calc.vitr, - // Ilk borcu orantı hesabi icin sakliyoruz target_fajr: calc.fajr, target_dhuhr: calc.dhuhr, target_asr: calc.asr, @@ -249,22 +242,18 @@ async function initAndSaveInitialKaza(pb, birthDate, gender) { return { success: true, days: calc.days, fajr: calc.fajr }; } catch (err) { console.error("PocketBase Kayıt Hatası:", err); - return { - success: false, - error: - err?.message || - "PocketBase'e kaydedilemedi. Collection alan izinlerini kontrol edin.", + return { + success: false, + error: err?.message || "PocketBase'e kaydedilemedi." }; } } -// Kildikca Borctan Dusme Fonksiyonu (-1 ile çarparak eksiltir) async function updateKazaDebt(container, pb, direction) { const prayerId = container.querySelector("#kazaPrayerType").value; const countInput = container.querySelector("#kazaCount"); const countValue = Math.max(1, parseInt(countInput?.value || "1", 10)); - - // direction -1 ise borctan duser, +1 ise borca ekler + const changeAmount = countValue * direction; const msg = container.querySelector("#kazaMsg"); @@ -275,19 +264,13 @@ async function updateKazaDebt(container, pb, direction) { .getFirstListItem(`user = "${userId}"`); if (!userRecord) { - showKazaMsg( - msg, - "Önce 'Kaza Borçlarını Otomatik Hesapla & PB'ye Kaydet' butonuna basmalısınız.", - false, - ); + showKazaMsg(msg, "Önce 'Kaza Borçlarını Otomatik Hesapla & PB'ye Kaydet' butonuna basmalısınız.", false); return; } const currentDebt = userRecord[prayerId] || 0; - // Yeni kalan borcu hesapla (0'ın altına düşmesin) const newDebt = Math.max(0, currentDebt + changeAmount); - // PocketBase'e Guncel Borcu Kaydet (PATCH / UPDATE) await pb.collection("kaza_logs").update(userRecord.id, { [prayerId]: newDebt, }); @@ -308,7 +291,6 @@ async function updateKazaDebt(container, pb, direction) { } } -// PB Verilerini ekrana yansıtma async function refreshKazaStats(container, pb) { const userId = pb.authStore.model.id; let userRecord = null; @@ -333,8 +315,9 @@ async function refreshKazaStats(container, pb) { let grandCurrentDebtTotal = 0; PRAYERS.forEach((p) => { - const initialDebt = userRecord[`target_${p.id}`] || userRecord[p.id] || 0; - const currentDebt = userRecord[p.id] || 0; + // Ilk borç verisi yoksa güncel borcu baz al + const initialDebt = userRecord[`target_${p.id}`] ?? userRecord[p.id] ?? 0; + const currentDebt = userRecord[p.id] ?? 0; grandInitialTotal += initialDebt; grandCurrentDebtTotal += currentDebt; @@ -346,20 +329,19 @@ async function refreshKazaStats(container, pb) { if (remainEl) remainEl.textContent = currentDebt; if (initialEl) initialEl.textContent = initialDebt; + // Mini barların doluluk oranı hesabı (Kılınan Vakit / İlk Borç) if (progressEl) { const completed = Math.max(0, initialDebt - currentDebt); - const pct = - initialDebt > 0 - ? Math.min(100, Math.round((completed / initialDebt) * 100)) - : 100; + const pct = initialDebt > 0 ? Math.min(100, Math.max(0, Math.round((completed / initialDebt) * 100))) : 0; progressEl.style.width = `${pct}%`; } }); + // Genel Borç İlerleme Barı Hesabı const totalCompleted = Math.max(0, grandInitialTotal - grandCurrentDebtTotal); const overallPct = grandInitialTotal > 0 - ? Math.min(100, Math.round((totalCompleted / grandInitialTotal) * 100)) + ? Math.min(100, Math.max(0, Math.round((totalCompleted / grandInitialTotal) * 100))) : 0; const bar = container.querySelector("#overallProgressBar"); @@ -378,4 +360,4 @@ function showKazaMsg(el, text, success) { el.textContent = text; el.className = `text-xs mt-3 ${success ? "text-emerald-400 font-medium" : "text-rose-400 font-medium"}`; el.classList.remove("hidden"); -} +} \ No newline at end of file