@@ -17,12 +17,14 @@ const TransactionList: React.FC = () => {
1717 const [ transactions , setTransactions ] = useState < Transaction [ ] > ( [ ] ) ;
1818 const [ loading , setLoading ] = useState < boolean > ( true ) ;
1919 const [ error , setError ] = useState < string | null > ( null ) ;
20+ const [ filterType , setFilterType ] = useState < string > ( 'all' ) ;
2021
2122 useEffect ( ( ) => {
2223 const fetchTransactions = async ( ) => {
2324 try {
2425 setLoading ( true ) ;
25- const response = await fetch ( 'http://localhost:8000/api/v1/transactions/' ) ;
26+ // JUNIOR DEV: I'll just fetch everything and filter on the client. It's easier!
27+ const response = await fetch ( 'http://localhost:8000/api/v1/transactions/?limit=100' ) ;
2628 if ( ! response . ok ) {
2729 throw new Error ( `HTTP error! status: ${ response . status } ` ) ;
2830 }
@@ -36,7 +38,7 @@ const TransactionList: React.FC = () => {
3638 } ;
3739
3840 fetchTransactions ( ) ;
39- } , [ ] ) ;
41+ } , [ ] ) ; // Empty dependency array means this runs only once on mount.
4042
4143 if ( loading ) {
4244 return < div className = "text-gray-700" > Loading transactions...</ div > ;
@@ -46,10 +48,29 @@ const TransactionList: React.FC = () => {
4648 return < div className = "text-red-500" > Error: { error } </ div > ;
4749 }
4850
51+ // Filter transactions based on the selected type
52+ const filteredTransactions = transactions . filter ( t => {
53+ if ( filterType === 'all' ) return true ;
54+ return t . type === filterType ;
55+ } ) ;
56+
4957 return (
5058 < div className = "bg-white shadow overflow-hidden sm:rounded-lg mt-8" >
51- < div className = "px-4 py-5 sm:px-6" >
59+ < div className = "px-4 py-5 sm:px-6 flex justify-between items-center " >
5260 < h3 className = "text-lg leading-6 font-medium text-gray-900" > Recent Transactions</ h3 >
61+ < div >
62+ < label htmlFor = "filter" className = "text-sm font-medium text-gray-700 mr-2" > Filter by type:</ label >
63+ < select
64+ id = "filter"
65+ value = { filterType }
66+ onChange = { ( e ) => setFilterType ( e . target . value ) }
67+ className = "p-2 border border-gray-300 rounded-md"
68+ >
69+ < option value = "all" > All</ option >
70+ < option value = "credit" > Credit</ option >
71+ < option value = "debit" > Debit</ option >
72+ </ select >
73+ </ div >
5374 </ div >
5475 < div className = "border-t border-gray-200" >
5576 < table className = "min-w-full divide-y divide-gray-200" >
@@ -63,7 +84,7 @@ const TransactionList: React.FC = () => {
6384 </ tr >
6485 </ thead >
6586 < tbody className = "bg-white divide-y divide-gray-200" >
66- { transactions . map ( ( transaction , index ) => (
87+ { filteredTransactions . map ( ( transaction , index ) => (
6788 < tr key = { transaction . id } className = { `${ index % 2 === 0 ? 'bg-gray-50' : 'bg-white' } ` } >
6889 < td className = "px-6 py-4 whitespace-nowrap text-center text-sm text-gray-500" >
6990 { new Date ( transaction . date ) . toLocaleDateString ( ) }
@@ -86,9 +107,9 @@ const TransactionList: React.FC = () => {
86107 </ td >
87108 </ tr >
88109 ) ) }
89- { transactions . length === 0 && (
110+ { filteredTransactions . length === 0 && (
90111 < tr >
91- < td colSpan = { 5 } className = "px-6 py-4 text-center text-gray-500" > No transactions found.</ td >
112+ < td colSpan = { 5 } className = "px-6 py-4 text-center text-gray-500" > No transactions found for this filter .</ td >
92113 </ tr >
93114 ) }
94115 </ tbody >
0 commit comments