Antoni09 commited on
Commit
4d09f4d
verified
1 Parent(s): 00bb689

Upload main.js

Browse files
Files changed (1) hide show
  1. main.js +63 -29
main.js CHANGED
@@ -482,11 +482,11 @@ function updateLogoPreview() {
482
  }
483
  }
484
 
485
- function renderInvoicesTable(invoices) {
486
- invoicesTableBody.innerHTML = "";
487
- if (!Array.isArray(invoices) || invoices.length === 0) {
488
- invoicesEmpty.classList.remove("hidden");
489
- return;
490
  }
491
 
492
  invoicesEmpty.classList.add("hidden");
@@ -515,18 +515,24 @@ function renderInvoicesTable(invoices) {
515
  const actionsWrapper = document.createElement("div");
516
  actionsWrapper.className = "table-actions";
517
 
518
- const editButton = document.createElement("button");
519
- editButton.type = "button";
520
- editButton.textContent = "Edytuj";
521
- editButton.addEventListener("click", () => {
522
- startInvoiceEdit(invoice.invoice_id);
523
- });
524
-
525
- const deleteButton = document.createElement("button");
526
- deleteButton.type = "button";
527
- deleteButton.className = "button secondary";
528
- deleteButton.textContent = "Usu艅";
529
- deleteButton.addEventListener("click", async () => {
 
 
 
 
 
 
530
  clearFeedback(dashboardFeedback);
531
  const shouldDelete = window.confirm(`Usu艅ac faktur臋 ${invoice.invoice_id}?`);
532
  if (!shouldDelete) {
@@ -534,21 +540,22 @@ function renderInvoicesTable(invoices) {
534
  }
535
  await deleteInvoice(invoice.invoice_id);
536
  });
537
-
538
- actionsWrapper.appendChild(editButton);
539
- actionsWrapper.appendChild(deleteButton);
540
- actionsCell.appendChild(actionsWrapper);
 
541
  row.appendChild(actionsCell);
542
 
543
  invoicesTableBody.appendChild(row);
544
  });
545
  }
546
 
547
- function applyInvoiceFilters() {
548
- if (!Array.isArray(invoicesCache)) {
549
- renderInvoicesTable([]);
550
- return;
551
- }
552
 
553
  let filtered = invoicesCache.slice();
554
  const startDate = parseDateInput(filterStartDate?.value);
@@ -572,9 +579,36 @@ function applyInvoiceFilters() {
572
  });
573
  }
574
 
575
- filtered.sort((a, b) => (b.issued_at || "").localeCompare(a.issued_at || ""));
576
- renderInvoicesTable(filtered);
577
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
578
 
579
  async function refreshInvoices() {
580
  if (!authToken) {
 
482
  }
483
  }
484
 
485
+ function renderInvoicesTable(invoices) {
486
+ invoicesTableBody.innerHTML = "";
487
+ if (!Array.isArray(invoices) || invoices.length === 0) {
488
+ invoicesEmpty.classList.remove("hidden");
489
+ return;
490
  }
491
 
492
  invoicesEmpty.classList.add("hidden");
 
515
  const actionsWrapper = document.createElement("div");
516
  actionsWrapper.className = "table-actions";
517
 
518
+ const editButton = document.createElement("button");
519
+ editButton.type = "button";
520
+ editButton.textContent = "Edytuj";
521
+ editButton.addEventListener("click", () => {
522
+ startInvoiceEdit(invoice.invoice_id);
523
+ });
524
+
525
+ const pdfButton = document.createElement("button");
526
+ pdfButton.type = "button";
527
+ pdfButton.className = "button secondary";
528
+ pdfButton.dataset.download = invoice.invoice_id;
529
+ pdfButton.textContent = "PDF";
530
+
531
+ const deleteButton = document.createElement("button");
532
+ deleteButton.type = "button";
533
+ deleteButton.className = "button secondary";
534
+ deleteButton.textContent = "Usu艅";
535
+ deleteButton.addEventListener("click", async () => {
536
  clearFeedback(dashboardFeedback);
537
  const shouldDelete = window.confirm(`Usu艅ac faktur臋 ${invoice.invoice_id}?`);
538
  if (!shouldDelete) {
 
540
  }
541
  await deleteInvoice(invoice.invoice_id);
542
  });
543
+
544
+ actionsWrapper.appendChild(editButton);
545
+ actionsWrapper.appendChild(pdfButton);
546
+ actionsWrapper.appendChild(deleteButton);
547
+ actionsCell.appendChild(actionsWrapper);
548
  row.appendChild(actionsCell);
549
 
550
  invoicesTableBody.appendChild(row);
551
  });
552
  }
553
 
554
+ function applyInvoiceFilters() {
555
+ if (!Array.isArray(invoicesCache)) {
556
+ renderInvoicesTable([]);
557
+ return;
558
+ }
559
 
560
  let filtered = invoicesCache.slice();
561
  const startDate = parseDateInput(filterStartDate?.value);
 
579
  });
580
  }
581
 
582
+ filtered.sort((a, b) => (b.issued_at || "").localeCompare(a.issued_at || ""));
583
+ renderInvoicesTable(filtered);
584
+ }
585
+
586
+ if (invoicesTableBody) {
587
+ invoicesTableBody.addEventListener("click", async (event) => {
588
+ const target = event.target;
589
+ if (!(target instanceof HTMLElement)) {
590
+ return;
591
+ }
592
+ const pdfTrigger = target.closest("[data-download]");
593
+ if (pdfTrigger) {
594
+ const invoiceId = pdfTrigger.getAttribute("data-download");
595
+ if (!invoiceId) {
596
+ return;
597
+ }
598
+ const invoiceData = invoicesCache.find((invoice) => invoice.invoice_id === invoiceId);
599
+ if (!invoiceData || !currentBusiness) {
600
+ showFeedback(dashboardFeedback, "Nie uda艂o si臋 przygotowa膰 PDF. Od艣wie偶 dane i spr贸buj ponownie.");
601
+ return;
602
+ }
603
+ try {
604
+ await generatePdf(currentBusiness, invoiceData, currentLogo);
605
+ } catch (error) {
606
+ console.error(error);
607
+ showFeedback(dashboardFeedback, "Nie uda艂o si臋 wygenerowa膰 PDF-a.");
608
+ }
609
+ }
610
+ });
611
+ }
612
 
613
  async function refreshInvoices() {
614
  if (!authToken) {