-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSearchPage.tsx
More file actions
75 lines (67 loc) · 1.98 KB
/
SearchPage.tsx
File metadata and controls
75 lines (67 loc) · 1.98 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
import React, { useState, FormEvent } from 'react';
import SearchResultsTable from './SearchResultsTable';
export interface SearchResult {
id: string;
name: string;
cardNumber: string;
issuanceDate: string;
currentBalance: number;
nextPaymentDate: string;
overdueBalance: number;
daysOverdue: number;
}
function SearchPage() {
const [firstName, setFirstName] = useState<string>('');
const [lastName, setLastName] = useState<string>('');
const [cardNumber, setCardNumber] = useState<string>('');
const [searchResults, setSearchResults] = useState<SearchResult[]>([]);
const submitForm = async (): Promise<void> => {
try {
const response = await fetch('https://api.afg.com/cardholders/search', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ firstName, lastName, cardNumber }),
});
if (!response.ok) {
throw new Error(`HTTP error! Status: ${response.status}`);
}
const results: SearchResult[] = await response.json();
setSearchResults(results);
} catch (error) {
console.error('Error fetching search results:', error);
}
};
const handleSearch = (e: FormEvent<HTMLFormElement>) => {
e.preventDefault();
submitForm();
};
return (
<div>
<form onSubmit={handleSearch}>
<input
type="text"
placeholder="First Name"
value={firstName}
onChange={(e) => setFirstName(e.target.value)}
/>
<input
type="text"
placeholder="Last Name"
value={lastName}
onChange={(e) => setLastName(e.target.value)}
/>
<input
type="text"
placeholder="Card Number"
value={cardNumber}
onChange={(e) => setCardNumber(e.target.value)}
/>
<button type="submit">Search</button>
</form>
<SearchResultsTable results={searchResults} />
</div>
);
}
export default SearchPage;