Open MuskanNazim opened 2 weeks ago
The primary issue seems to be the lack of error handling and feedback when the expenses list is empty. Additionally, there might be issues with the user object not being correctly populated or the database query failing silently. To solve this, we need to implement error handling, conditional rendering for empty states, and ensure the user object is correctly populated before making the database query.
The bug is likely caused by a combination of factors:
user
object is not correctly populated, the getAllExpenses
function will not execute, leading to an empty expenses page.expensesList
will remain empty.expensesList
is empty, providing no feedback to the user.utils/dbConfig.jsx
Move the database connection string to environment variables to enhance security.
import { neon } from "@neondatabase/serverless";
import { drizzle } from "drizzle-orm/neon-http";
import * as schema from "./schema";
const sql = neon(process.env.DATABASE_URL);
export const db = drizzle(sql, { schema });
app/(routes)/dashboard/expenses/page.jsx
Add error handling, conditional rendering, and ensure the user object is correctly populated.
import React, { useEffect, useState } from 'react';
import { useUser } from '@clerk/nextjs';
import { db } from '../../../utils/dbConfig';
import { Expenses, Budgets } from '../../../utils/schema';
import ExpenseListTable from './_components/ExpenseListTable';
import { eq, desc } from 'drizzle-orm';
const ExpensesPage = () => {
const { user } = useUser();
const [expensesList, setExpensesList] = useState([]);
const getAllExpenses = async () => {
try {
const result = await db.select({
id: Expenses.id,
name: Expenses.name,
amount: Expenses.amount,
createdAt: Expenses.createdAt
}).from(Budgets)
.rightJoin(Expenses, eq(Budgets.id, Expenses.budgetId))
.where(eq(Budgets.createdBy, user?.primaryEmailAddress.emailAddress))
.orderBy(desc(Expenses.id));
setExpensesList(result);
} catch (error) {
console.error("Failed to fetch expenses:", error);
}
};
useEffect(() => {
if (user && user.primaryEmailAddress) {
getAllExpenses();
}
}, [user]);
return (
<div className='p-10'>
<h2 className='font-bold text-3xl'>My Expenses</h2>
{expensesList.length > 0 ? (
<ExpenseListTable refreshData={() => getAllExpenses()} expensesList={expensesList} />
) : (
<p>No expenses found.</p>
)}
</div>
);
};
export default ExpensesPage;
app/(routes)/dashboard/expenses/_components/ExpenseListTable.jsx
Add conditional rendering for empty data, error handling for deletion, and loading states.
import React, { useState } from "react";
import { toast } from "react-toastify";
import { db } from "../../../utils/dbConfig";
import { Expenses } from "../../../utils/schema";
import { eq } from "drizzle-orm";
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. Please try again.");
} finally {
setLoading(false);
}
};
return (
<div className="mt-3">
<h2 className="font-bold text-lg">Latest Expenses</h2>
{loading ? (
<p>Loading...</p>
) : expensesList && expensesList.length > 0 ? (
<>
<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>
))}
</>
) : (
<p>No expenses to display.</p>
)}
</div>
);
}
export default ExpenseListTable;
By implementing the above changes, we should be able to handle errors gracefully, provide feedback when no expenses are found, and ensure the user object is correctly populated before making the database query. This should resolve the issue where the expenses page shows nothing.
Click here to create a Pull Request with the proposed solution
Files used for this task:
No expenses found.
)}No expenses to display.
)}Loading...
) : expensesList && expensesList.length > 0 ? ( <>No expenses to display.
)}
not showing any error....but expenses page shows nothing