-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTransactionList.tsx
More file actions
178 lines (164 loc) · 6.74 KB
/
TransactionList.tsx
File metadata and controls
178 lines (164 loc) · 6.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
import React, { useEffect, useState } from 'react';
interface Transaction {
id: number;
description: string;
amount: number;
type: string;
category_rel: {
id: number;
name: string;
};
date: string;
user_id: number;
}
const TransactionList: React.FC = () => {
const [transactions, setTransactions] = useState<Transaction[]>([]);
const [loading, setLoading] = useState<boolean>(true);
const [error, setError] = useState<string | null>(null);
const [filterType, setFilterType] = useState<string>('all');
const [currentPage, setCurrentPage] = useState<number>(1);
const pageSize = 10;
useEffect(() => {
const fetchTransactions = async () => {
try {
setLoading(true);
const response = await fetch('http://localhost:8000/api/v1/transactions/?limit=1000');
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
const data: Transaction[] = await response.json();
setTransactions(data);
} catch (e: any) {
setError(e.message);
} finally {
setLoading(false);
}
};
fetchTransactions();
}, []);
const filteredTransactions = transactions.filter(transaction => {
if (filterType === 'all') return true;
return transaction.type === filterType;
});
const totalPages = Math.ceil(filteredTransactions.length / pageSize);
const startIndex = (currentPage - 1) * pageSize;
const endIndex = startIndex + pageSize;
const paginatedTransactions = filteredTransactions.slice(startIndex, endIndex);
const handleFilterChange = (e: React.ChangeEvent<HTMLSelectElement>) => {
setFilterType(e.target.value);
setCurrentPage(1); // Reset to first page when filter changes
};
const handleNextPage = () => {
if (currentPage < totalPages) {
setCurrentPage(currentPage + 1);
}
};
const handlePreviousPage = () => {
if (currentPage > 1) {
setCurrentPage(currentPage - 1);
}
};
if (loading) {
return <div className="text-gray-700">Loading transactions...</div>;
}
if (error) {
return <div className="text-red-500">Error: {error}</div>;
}
return (
<div className="bg-white shadow overflow-hidden sm:rounded-lg mt-8">
<div className="px-4 py-5 sm:px-6 flex justify-between items-center">
<h3 className="text-lg leading-6 font-medium text-gray-900">Recent Transactions</h3>
<div className="flex items-center gap-2">
<label htmlFor="type-filter" className="text-sm font-medium text-gray-700">
Filter by type:
</label>
<select
id="type-filter"
value={filterType}
onChange={handleFilterChange}
className="border border-gray-300 rounded-md px-3 py-1 text-sm focus:outline-none focus:ring-2 focus:ring-blue-500"
>
<option value="all">All</option>
<option value="credit">Credit</option>
<option value="debit">Debit</option>
</select>
</div>
</div>
<div className="border-t border-gray-200">
<table className="min-w-full divide-y divide-gray-200">
<thead className="bg-gray-50">
<tr>
<th scope="col" className="px-6 py-3 text-center text-xs font-medium text-gray-500 uppercase tracking-wider w-[15%]">Date</th>
<th scope="col" className="px-6 py-3 text-center text-xs font-medium text-gray-500 uppercase tracking-wider w-[10%]">Type</th>
<th scope="col" className="px-6 py-3 text-center text-xs font-medium text-gray-500 uppercase tracking-wider w-[20%]">Category</th>
<th scope="col" className="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider w-[35%]">Description</th>
<th scope="col" className="px-6 py-3 text-center text-xs font-medium text-gray-500 uppercase tracking-wider w-[20%]">Amount</th>
</tr>
</thead>
<tbody className="bg-white divide-y divide-gray-200">
{paginatedTransactions.map((transaction, index) => (
<tr key={transaction.id} className={`${index % 2 === 0 ? 'bg-gray-50' : 'bg-white'}`}>
<td className="px-6 py-4 whitespace-nowrap text-center text-sm text-gray-500">
{new Date(transaction.date).toLocaleDateString()}
</td>
<td className="px-6 py-4 whitespace-nowrap text-center">
<span className={`px-2 inline-flex justify-center text-center text-xs leading-5 font-semibold rounded-full ${
transaction.type === 'credit' ? 'bg-green-100 text-green-800' : 'bg-red-100 text-red-800'
}`}>
{transaction.type}
</span>
</td>
<td className="px-6 py-4 whitespace-nowrap text-center text-sm text-gray-500">
{transaction.category_rel.name}
</td>
<td className="px-6 py-4 whitespace-nowrap text-left text-sm font-medium text-gray-900">
{transaction.description}
</td>
<td className="px-6 py-4 whitespace-nowrap text-center text-sm text-gray-900">
${transaction.amount.toFixed(2)}
</td>
</tr>
))}
{paginatedTransactions.length === 0 && (
<tr>
<td colSpan={5} className="px-6 py-4 text-center text-gray-500">No transactions found.</td>
</tr>
)}
</tbody>
</table>
</div>
{totalPages > 1 && (
<div className="px-4 py-3 bg-gray-50 border-t border-gray-200 sm:px-6 flex items-center justify-between">
<div className="text-sm text-gray-700">
Page {currentPage} of {totalPages} ({filteredTransactions.length} transactions)
</div>
<div className="flex gap-2">
<button
onClick={handlePreviousPage}
disabled={currentPage === 1}
className={`px-3 py-1 rounded text-sm font-medium ${
currentPage === 1
? 'bg-gray-200 text-gray-400 cursor-not-allowed'
: 'bg-blue-500 text-white hover:bg-blue-600'
}`}
>
Previous
</button>
<button
onClick={handleNextPage}
disabled={currentPage === totalPages}
className={`px-3 py-1 rounded text-sm font-medium ${
currentPage === totalPages
? 'bg-gray-200 text-gray-400 cursor-not-allowed'
: 'bg-blue-500 text-white hover:bg-blue-600'
}`}
>
Next
</button>
</div>
</div>
)}
</div>
);
};
export default TransactionList;