Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: total value shows NaN when an expense with 0 amount #1574

Merged
merged 1 commit into from
Nov 25, 2024
Merged
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
15 changes: 10 additions & 5 deletions src/plays/expenses-tracker/ExpensesTracker.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,14 @@ function ExpensesTracker(props: any) {

const handleNewExpense = () => {
const expense = localStoreExpenses[localStoreExpenses.length - 1];
data['id'] = expense !== undefined ? parseInt(expense.id) + 1 : 1;
setLocalStoreExpenses([...localStoreExpenses, data]);
setLocalStoreTotal(parseFloat(localStoreTotal) + parseFloat(data.amount));
// Ensure `amount` has a default value of 0 if not present
const sanitizedData = {
...data,
amount: data.amount || 0
};
sanitizedData['id'] = expense !== undefined ? parseInt(expense.id) + 1 : 1;
setLocalStoreExpenses([...localStoreExpenses, sanitizedData]);
setLocalStoreTotal(parseFloat(localStoreTotal) + parseFloat(sanitizedData.amount));
setOpen(false);
setData(null);
};
Expand Down Expand Up @@ -97,14 +102,14 @@ function ExpensesTracker(props: any) {
<td className="p-1 text-center">{expense.date}</td>
<td className="p-1 text-center">{expense.amount}</td>
<td className="p-1 flex justify-center space-x-2">
<div className="bg-green-500 rounded-full p-[6px]">
<div className="bg-green-500 cursor-pointer rounded-full p-[6px]">
<FiEdit
className="text-white"
size={16}
onClick={() => openEditModal(expense)}
/>
</div>
<div className="bg-red-500 rounded-full p-[6px]">
<div className="bg-red-500 cursor-pointer rounded-full p-[6px]">
<AiOutlineDelete
className="text-white"
size={16}
Expand Down
Loading