Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 25 additions & 8 deletions Expense-Tracker-Frontend/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ function updateValues() {
function addTransactionDOM(transaction) {
const tr = document.createElement("tr");
tr.innerHTML = `
<td>${transaction.date}</td>
<td>${formatDate(transaction.date)}</td>
<td>${transaction.text}</td>
<td class="amount ${
transaction.amount < 0 ? "expense" : ""
Expand All @@ -49,14 +49,20 @@ function addTransactionDOM(transaction) {
}

function removeTransaction(id) {
fetch(`http://localhost:8080/ExpTrack/transactions/${username}/${id}`, {
method: "DELETE",
})
.then(() => {
transactions = transactions.filter((t) => t.id !== id);
updateUI();
const confirmed = confirm(
"Are you sure you want to delete this transaction?"
);

if (confirmed) {
fetch(`http://localhost:8080/ExpTrack/transactions/${username}/${id}`, {
method: "DELETE",
})
.catch((err) => console.error("Delete failed", err));
.then(() => {
transactions = transactions.filter((t) => t.id !== id);
updateUI();
})
.catch((err) => console.error("Delete failed", err));
}
}

function updateUI() {
Expand Down Expand Up @@ -108,3 +114,14 @@ function init() {
}

init();

function formatDate(isoDate) {
const date = new Date(isoDate);

// Get day, month, and year
const day = String(date.getDate()).padStart(2, "0");
const month = String(date.getMonth() + 1).padStart(2, "0");
const year = date.getFullYear();

return `${day}-${month}-${year}`;
}