From 84622698f6c0e9d76ebe434c00df587908a37015 Mon Sep 17 00:00:00 2001 From: Biswa Kalyan Bhuyan Date: Sat, 26 Apr 2025 01:05:59 +0530 Subject: finance/frontend: fix: updated the pages and layouts to be compatible with the backend --- frontend/src/app/(main)/loans/page.tsx | 47 +++++++++++++++++++++++++++++++--- 1 file changed, 44 insertions(+), 3 deletions(-) (limited to 'frontend/src/app/(main)/loans/page.tsx') diff --git a/frontend/src/app/(main)/loans/page.tsx b/frontend/src/app/(main)/loans/page.tsx index 07c1428..ef263fd 100644 --- a/frontend/src/app/(main)/loans/page.tsx +++ b/frontend/src/app/(main)/loans/page.tsx @@ -13,6 +13,8 @@ import { loanApi, Loan, LoanInput } from '@/lib/api'; export default function LoansPage() { const queryClient = useQueryClient(); const [isAddDialogOpen, setIsAddDialogOpen] = useState(false); + const [isDeleteDialogOpen, setIsDeleteDialogOpen] = useState(false); + const [loanToDelete, setLoanToDelete] = useState(null); const [selectedLoan, setSelectedLoan] = useState(null); // Form state @@ -29,7 +31,15 @@ export default function LoansPage() { queryKey: ['loans'], queryFn: async () => { const response = await loanApi.getLoans(); - return response.loans as Loan[]; + console.log('API Response:', response); // Debug the response structure + + // The response might be the array directly rather than nested under a 'loans' property + if (Array.isArray(response)) { + return response as Loan[]; + } + + // Or it might be nested under a 'loans' or 'data' property + return (response.loans || response.data || []) as Loan[]; } }); @@ -51,6 +61,8 @@ export default function LoansPage() { mutationFn: (id: number) => loanApi.deleteLoan(id), onSuccess: () => { queryClient.invalidateQueries({ queryKey: ['loans'] }); + setIsDeleteDialogOpen(false); + setLoanToDelete(null); } }); @@ -92,8 +104,13 @@ export default function LoansPage() { }; const handleDeleteLoan = (id: number) => { - if (window.confirm('Are you sure you want to delete this loan?')) { - deleteLoanMutation.mutate(id); + setLoanToDelete(id); + setIsDeleteDialogOpen(true); + }; + + const confirmDeleteLoan = () => { + if (loanToDelete !== null) { + deleteLoanMutation.mutate(loanToDelete); } }; @@ -221,6 +238,30 @@ export default function LoansPage() { + {/* Delete Confirmation Dialog */} + + + + Confirm Deletion + +
+

Are you sure you want to delete this loan? This action cannot be undone.

+
+
+ + +
+
+
+ {data && data.length > 0 ? ( -- cgit v1.2.3-59-g8ed1b