-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdonate.js
41 lines (36 loc) · 1.31 KB
/
donate.js
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
async function fetchTotalDonated() {
try {
const response = await fetch("http://localhost:3000/total-donated");
if (!response.ok) {
throw new Error("Error fetching total donations");
}
const data = await response.json();
document.getElementById("total-donated").innerText = `Total Donations: $${data.total.toFixed(2)}`;
} catch (error) {
console.error("Error fetching total donations:", error);
}
}
async function submitDonation() {
const amount = document.getElementById("donation-amount").value;
try {
const response = await fetch("http://localhost:3000/donate", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({ amount: parseFloat(amount) }), // Send amount as JSON
});
const data = await response.json();
if (response.ok) {
alert(data.message);
fetchTotalDonated(); // Refresh total donations after a successful donation
} else {
alert(data.message); // Show error message from server
}
} catch (error) {
console.error("Error:", error);
alert("Failed to donate. Please try again.");
}
}
// Fetch total donations on page load
fetchTotalDonated();