Skip to content

Commit

Permalink
fix: Fix logic bug in get and save endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
Yanni8 committed Dec 15, 2024
1 parent 91e4d53 commit f675851
Showing 1 changed file with 10 additions and 6 deletions.
16 changes: 10 additions & 6 deletions src/service/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,19 @@ import type { Transaction } from "@/models/transaction";

export function saveTransaction(transaction: Transaction): number {
let transactions = JSON.parse(localStorage.getItem("transactions") || "{}")

let maxIndex = 0;

Object.keys(transaction).map(key => maxIndex = Math.max(+key, maxIndex));
Object.keys(transactions).map(key => {
if (+key > maxIndex) {
maxIndex = +key;
}
});

console.log(maxIndex)
transaction.id = maxIndex + 1;

transactions.push(transaction);
transactions[transaction.id.toString()] = transaction;

localStorage.setItem("transactions", JSON.stringify(transactions));

Expand All @@ -20,12 +25,11 @@ export function deleteTransaction(id: number) {
let transactions = JSON.parse(localStorage.getItem("transactions") || "{}")

delete transactions[id]
localStorage.setItem("transactions", JSON.stringify(transactions));


localStorage.setItem("transactions", JSON.stringify(transactions));
}

export function getTransactions(): Array<Transaction> {
return JSON.parse(localStorage.getItem("transactions") || "{}")
return Object.values(JSON.parse(localStorage.getItem("transactions") || "{}"))
}

0 comments on commit f675851

Please sign in to comment.