-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpopulateGunCards.js
More file actions
85 lines (74 loc) · 3.28 KB
/
populateGunCards.js
File metadata and controls
85 lines (74 loc) · 3.28 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
document.addEventListener('DOMContentLoaded', () => {
// Fetch gun data from the API
fetch('/api/guns')
.then(response => response.json())
.then(data => {
// Get the gun cards container element
const gunCardsContainer = document.getElementById('gunCardsContainer');
// Populate the gun cards initially with all guns
displayFilteredGuns(data);
})
.catch(error => {
console.error('Error fetching gun data:', error);
// Handle errors if needed
});
// Add event listeners to dropdown menus
document.getElementById('sizeDropdown').addEventListener('change', handleDropdownChange);
document.getElementById('availabilityDropdown').addEventListener('change', handleDropdownChange);
document.getElementById('typeDropdown').addEventListener('change', handleDropdownChange);
document.getElementById('ammoDropdown').addEventListener('change', handleDropdownChange);
document.getElementById('colorDropdown').addEventListener('change', handleDropdownChange);
});
async function handleDropdownChange() {
// Fetch filtered data from the server based on selected options
const size = document.getElementById('sizeDropdown').value;
const availability = document.getElementById('availabilityDropdown').value;
const type = document.getElementById('typeDropdown').value;
const ammo = document.getElementById('ammoDropdown').value;
const colors = document.getElementById('colorDropdown').value;
try {
const response = await fetch(`/api/guns?size=${size}&availability=${availability}&type=${type}&ammo=${ammo}&colors=${colors}`);
if (!response.ok) {
throw new Error('Error fetching filtered guns');
}
const data = await response.json();
displayFilteredGuns(data);
} catch (error) {
console.error('Error:', error.message);
// Handle error
}
console.log('Dropdown changed');
}
function displayFilteredGuns(guns) {
// Clear existing gun display
const gunCardsContainer = document.getElementById('gunCardsContainer');
gunCardsContainer.innerHTML = '';
// Display the filtered guns
guns.forEach(gun => {
const gunCard = createGunCard(gun);
gunCardsContainer.appendChild(gunCard);
});
}
function createGunCard(gun) {
// Create a new gun card element
const gunCard = document.createElement('div');
gunCard.classList.add('col-md-4', 'mb-4');
// Populate the gun card with gun information
gunCard.innerHTML = `
<div class="card">
<img src="${gun.gun_img}" class="card-img-top" alt="Gun Image">
<div class="card-body">
<h5 class="card-title">${gun.gun_name}</h5>
<p class="card-text">${gun.description}</p>
<p class="card-text">Colors: ${gun.colors}</p>
<p class="card-text">Availability: ${gun.availability}</p>
<p class="card-text">Price: ${gun.price}</p>
<p class="card-text">Size: ${gun.size}</p>
<p class="card-text">Ammo: ${gun.ammo}</p>
<p class="card-text">Type: ${gun.type}</p>
<!-- Add more information as needed -->
</div>
</div>
`;
return gunCard;
}