Open MuskanNazim opened 2 months ago
The plan to solve the bug involves addressing issues in multiple files that interact to display the expenses. The main areas of focus are:
page.jsx
file.ExpenseListTable.jsx
component correctly receives and renders the expenses list, with proper error handling and loading states.The bug is likely caused by a combination of issues:
dbConfig.jsx
could lead to connection failures.getBudgetInfo
and getExpensesList
functions in page.jsx
lack error handling, which means any failure in fetching data will result in empty state variables.ExpenseListTable.jsx
component may not be receiving the expensesList
prop correctly or handling it properly, leading to no expenses being displayed.utils/dbConfig.jsx
)import { neon } from "@neondatabase/serverless";
import { drizzle } from "drizzle-orm/neon-http";
import * as schema from "./schema";
const sql = neon(process.env.DATABASE_URL);
try {
export const db = drizzle(sql, { schema });
} catch (error) {
console.error("Failed to connect to the database:", error);
}
utils/schema.jsx
)createdAt
field and ensure data integrity.export const Expenses = pgTable("expenses", {
id: serial("id").primaryKey(),
name: varchar("name").notNull(),
amount: numeric("amount").notNull().default(0),
budgetId: integer("budgetId").references(() => Budgets.id),
createdAt: timestamp("createdAt").notNull(), // Changed to timestamp
});
app/(routes)/dashboard/expenses/[id]/page.jsx
)useEffect(() => {
if (user) {
getBudgetInfo().catch(error => {
console.error("Failed to fetch budget info:", error);
// Optionally set an error state here
});
}
}, [user]);
const getBudgetInfo = async () => {
try {
const result = await db
.select({
...getTableColumns(Budgets),
totalSpend: sql`sum(${Expenses.amount})`.mapWith(Number),
totalItem: sql`count(${Expenses.id})`.mapWith(Number),
})
.from(Budgets)
.leftJoin(Expenses, eq(Budgets.id, Expenses.budgetId))
.where(eq(Budgets.createdBy, user?.primaryEmailAddress?.emailAddress))
.where(eq(Budgets.id, params.id))
.groupBy(Budgets.id);
setbudgetInfo(result[0]);
await getExpensesList();
} catch (error) {
console.error("Error fetching budget info:", error);
// Optionally set an error state here
}
};
const getExpensesList = async () => {
try {
const result = await db
.select()
.from(Expenses)
.where(eq(Expenses.budgetId, params.id))
.orderBy(desc(Expenses.id));
setExpensesList(result);
console.log(result);
} catch (error) {
console.error("Error fetching expenses list:", error);
// Optionally set an error state here
}
};
app/(routes)/dashboard/expenses/_components/ExpenseListTable.jsx
)import PropTypes from 'prop-types';
import { useState } from 'react';
ExpenseListTable.propTypes = {
expensesList: PropTypes.array.isRequired,
refreshData: PropTypes.func.isRequired,
};
function ExpenseListTable({ expensesList, refreshData }) {
const [loading, setLoading] = useState(false);
const deleteExpense = async (expense) => {
setLoading(true);
try {
const result = await db
.delete(Expenses)
.where(eq(Expenses.id, expense.id))
.returning();
if (result) {
toast("Expense Deleted!");
refreshData();
}
} catch (error) {
toast.error("Failed to delete expense.");
} finally {
setLoading(false);
}
};
return (
<div className="mt-3">
<h2 className="font-bold text-lg">Latest Expenses</h2>
{loading ? (
<p>Loading...</p>
) : (
<div>
{expensesList.length === 0 ? (
<p>No expenses available.</p>
) : (
<div>
<div className="grid grid-cols-4 rounded-tl-xl rounded-tr-xl bg-slate-200 p-2 mt-3">
<h2 className="font-bold">Name</h2>
<h2 className="font-bold">Amount</h2>
<h2 className="font-bold">Date</h2>
<h2 className="font-bold">Action</h2>
</div>
{expensesList.map((expenses, index) => (
<div key={index} className="grid grid-cols-4 bg-slate-50 rounded-bl-xl rounded-br-xl p-2">
<h2>{expenses.name}</h2>
<h2>{expenses.amount}</h2>
<h2>{expenses.createdAt}</h2>
<h2
onClick={() => deleteExpense(expenses)}
className="text-red-500 cursor-pointer"
>
Delete
</h2>
</div>
))}
</div>
)}
</div>
)}
</div>
);
}
dbConfig.jsx
.By implementing the above changes, the application should handle database connections more securely, fetch data reliably with error handling, and render the expenses list correctly with proper user feedback.
Click here to create a Pull Request with the proposed solution
Files used for this task:
No expenses available.
) : (Loading...
) : (
error on expenses...nothing is showing on expenses