Skip to content

Commit

Permalink
Add storage filters and fix comments
Browse files Browse the repository at this point in the history
  • Loading branch information
catherinepapad committed Dec 30, 2023
1 parent eb8179f commit 6aa6375
Show file tree
Hide file tree
Showing 6 changed files with 328 additions and 26 deletions.
6 changes: 2 additions & 4 deletions web-app/src/builder-cpu.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {config} from './config.js';

document.addEventListener('DOMContentLoaded', function () {
// Get the dropdown element
// Get the dropdown element and the static radio button elements
const dropdown = document.getElementById('selectCPU');
const filterRadiosManufacturers = document.querySelectorAll('input[name="cpuManufacturerFilter"]');
const filterRadiosIntegrated = document.querySelectorAll('input[name="cpuIntegratedFilter"]');
Expand Down Expand Up @@ -65,11 +65,9 @@ document.addEventListener('DOMContentLoaded', function () {
return;
}

// Extract unique values for the Socket property
// Extract unique values for the socket property
const uniqueSockets = [...new Set(data.map(item => item.socket))];

console.log(uniqueSockets);

// Assuming filterRadiosSocket is an existing container element
const filterRadiosSocket = document.getElementById('cpuSocketFilter');

Expand Down
6 changes: 3 additions & 3 deletions web-app/src/builder-gpu.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {config} from './config.js';

document.addEventListener('DOMContentLoaded', function () {
// Get the dropdown element
// Get the dropdown element and the static radio button elements
const dropdown = document.getElementById('selectGPU');
const filterRadiosMemory = document.querySelectorAll('input[name="gpuGDDRFilter"]');
const filterRadiosPCIE = document.querySelectorAll('input[name="gpuPCIEFilter"]');
Expand Down Expand Up @@ -146,7 +146,7 @@ document.addEventListener('DOMContentLoaded', function () {
const uniqueWattages = [...new Set(data.map(item => item.min_psu_wattage))];
uniqueWattages.sort((a, b) => a - b);

// Assuming filterRadiosManufacturer is an existing container element
// Assuming filterRadiosWattages is an existing container element
const filterRadiosWattages = document.getElementById('gpuWattageFilter');

// Check if the container element exists
Expand Down Expand Up @@ -192,7 +192,7 @@ document.addEventListener('DOMContentLoaded', function () {
const filteredOptions = data.filter(item => selectedWattage.value === 'all' || item.min_psu_wattage == selectedWattage.value);
populateDropdown(filteredOptions);
} else {
// Handle case when no manufacturer is selected
// Handle case when no wattage is selected
populateDropdown(data);
}
});
Expand Down
13 changes: 7 additions & 6 deletions web-app/src/builder-mobo.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {config} from './config.js';

document.addEventListener('DOMContentLoaded', function () {
// Get the dropdown element
// Get the dropdown element and the static radio button elements
const dropdown = document.getElementById('selectMOBO');
const filterRadiosMemory = document.querySelectorAll('input[name="moboSlotsFilter"]');
const filterRadiosDDR = document.querySelectorAll('input[name="moboDDRFilter"]');
Expand All @@ -15,15 +15,18 @@ document.addEventListener('DOMContentLoaded', function () {
// Populate the dropdown with all options
populateDropdown(data);

// Attach event listeners to the memory slots filter radios
filterRadiosMemory.forEach(radio => {
radio.addEventListener('change', updateDropdown);
});

filterRadiosDDR.forEach(radio => { // Updated this line
// Attach event listeners to the ddr generation filter radios
filterRadiosDDR.forEach(radio => {
radio.addEventListener('change', updateDropdown);
});

filterRadiosFormFactor.forEach(radio => { // Updated this line
// Attach event listeners to the form factor filter radios
filterRadiosFormFactor.forEach(radio => {
radio.addEventListener('change', updateDropdown);
});

Expand Down Expand Up @@ -148,11 +151,9 @@ document.addEventListener('DOMContentLoaded', function () {
return;
}

// Extract unique values for the Socket property
// Extract unique values for the socket property
const uniqueSockets = [...new Set(data.map(item => item.socket))];

//console.log(uniqueSockets);

// Assuming filterRadiosSocket is an existing container element
const filterRadiosSocket = document.getElementById('MoboSocketFilter');

Expand Down
12 changes: 7 additions & 5 deletions web-app/src/builder-ram.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {config} from './config.js';

document.addEventListener('DOMContentLoaded', function () {
// Get the dropdown element
// Get the dropdown element and the static radio button elements
const dropdown = document.getElementById('selectRAM');
const filterRadiosDDR = document.querySelectorAll('input[name="ramDDRFilter"]');
const filterRadiosModules = document.querySelectorAll('input[name="ramModulesFilter"]');
Expand All @@ -15,20 +15,22 @@ document.addEventListener('DOMContentLoaded', function () {
// Populate the dropdown with all options
populateDropdown(data);

filterRadiosDDR.forEach(radio => { // Updated this line
// Attach event listeners to the ddr generation filter radios
filterRadiosDDR.forEach(radio => {
radio.addEventListener('change', updateDropdown);
});

filterRadiosModules.forEach(radio => { // Updated this line
// Attach event listeners to the modules filter radios
filterRadiosModules.forEach(radio => {
radio.addEventListener('change', updateDropdown);
});

filterRadiosCapacity.forEach(radio => { // Updated this line
// Attach event listeners to the ddr generation filter radios
filterRadiosCapacity.forEach(radio => {
radio.addEventListener('change', updateDropdown);
});

// Create filters dynamically
//createSocketFilter(data);
createManufacturerFilter(data)

function updateDropdown() {
Expand Down
242 changes: 242 additions & 0 deletions web-app/src/builder-storage.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,242 @@
import {config} from './config.js';

document.addEventListener('DOMContentLoaded', function () {
// Get the dropdown element and the static radio button elements
const dropdown = document.getElementById('selectStorage');
const filterRadiosType = document.querySelectorAll('input[name="storageTypeFilter"]');
const filterRadiosFormFactor = document.querySelectorAll('input[name="storageFormFactorFilter"]');
const filterRadiosConnectivity = document.querySelectorAll('input[name="storageConnectivityFilter"]');


// Fetch data from the API endpoint
var url = config.backendUrl + '/components/storages';
fetch(url)
.then(response => response.json())
.then(data => {
// Populate the dropdown with all options
populateDropdown(data);

// Attach event listeners to the storage type filter radios
filterRadiosType.forEach(radio => {
radio.addEventListener('change', updateDropdown);
});

// Attach event listeners to the storage form factor filter radios
filterRadiosFormFactor.forEach(radio => {
radio.addEventListener('change', updateDropdown);
});

// Attach event listeners to the storage connectivity filter radios
filterRadiosConnectivity.forEach(radio => {
radio.addEventListener('change', updateDropdown);
});

// Create filters dynamically
createManufacturerFilter(data);
createCapacityFilter(data);

function updateDropdown() {
// Get the selected values from all filters
const selectedManufacturer = document.querySelector('input[name="storageManufacturerFilter"]:checked').value;
const selectedCapacity = document.querySelector('input[name="storageCapacityFilter"]:checked').value;
const selectedType = document.querySelector('input[name="storageTypeFilter"]:checked').value;
const selectedFormFactor = document.querySelector('input[name="storageFormFactorFilter"]:checked').value;
const selectedConnectivity = document.querySelector('input[name="storageConnectivityFilter"]:checked').value;

// Filter the options based on the selected filters
const filteredOptions = data.filter(item =>
(selectedManufacturer === 'all' || item.Manufacturer_name === selectedManufacturer) && (selectedCapacity === 'all' || item.capacity == selectedCapacity) && (selectedType === 'all' || item.type === selectedType) && (selectedFormFactor === 'all' || item.form_factor === selectedFormFactor) && (selectedConnectivity === 'all' || item.connectivity === selectedConnectivity)
);

populateDropdown(filteredOptions);
}


function createManufacturerFilter(data) {
// Check if data is an array and not empty
if (!Array.isArray(data) || data.length === 0) {
console.error('Invalid or empty data array.');
return;
}

// Check if each item in data has the "Manufacturer_name" property
if (!data.every(item => 'Manufacturer_name' in item)) {
console.error('Data items should have a "Manufacturer_name" property.');
return;
}

// Extract unique values for the Manufacturer_name property
const uniqueManufacturers = [...new Set(data.map(item => item.Manufacturer_name))];

// Assuming filterRadiosManufacturer is an existing container element
const filterRadiosManufacturer = document.getElementById('storageManufacturerFilter');

// Check if the container element exists
if (!filterRadiosManufacturer) {
console.error('Container element not found.');
return;
}

// Create "All" option
const allInput = document.createElement('input');
allInput.type = 'radio';
allInput.id = 'storageManufacturerFilter_all';
allInput.name = 'storageManufacturerFilter';
allInput.value = 'all';
allInput.checked = true; // default to "All" selected

const allLabel = document.createElement('label');
allLabel.htmlFor = 'storageManufacturerFilter_all';
allLabel.textContent = 'All';

filterRadiosManufacturer.appendChild(allInput);
filterRadiosManufacturer.appendChild(allLabel);

// Create radio buttons dynamically
uniqueManufacturers.forEach(manufacturer => {
const input = document.createElement('input');
input.type = 'radio';
input.id = `storageManufacturerFilter_${manufacturer}`;
input.name = 'storageManufacturerFilter';
input.value = manufacturer;

const label = document.createElement('label');
label.htmlFor = `storageManufacturerFilter_${manufacturer}`;
label.textContent = manufacturer;

filterRadiosManufacturer.appendChild(input);
filterRadiosManufacturer.appendChild(label);

// Attach event listener to each radio button
input.addEventListener('change', function () {
const selectedManufacturer = document.querySelector('input[name="storageManufacturerFilter"]:checked');
if (selectedManufacturer) {
const filteredOptions = data.filter(item => selectedManufacturer.value === 'all' || item.Manufacturer_name === selectedManufacturer.value);
populateDropdown(filteredOptions);
} else {
// Handle case when no manufacturer is selected
populateDropdown(data);
}
});
});

// Find the "All" radio button
const allManufacturerRadio = document.getElementById('storageManufacturerFilter_all');

// Add a click event listener to the "All" radio button
allManufacturerRadio.addEventListener('click', function () {
const selectedManufacturer = document.querySelector('input[name="storageManufacturerFilter"]:checked');
if (selectedManufacturer) {
const filteredOptions = data.filter(item => selectedManufacturer.value === 'all' || item.Manufacturer_name === selectedManufacturer.value);
populateDropdown(filteredOptions);
} else {
// Handle case when no manufacturer is selected
populateDropdown(data);
}
});
}

function createCapacityFilter(data) {
// Check if data is an array and not empty
if (!Array.isArray(data) || data.length === 0) {
console.error('Invalid or empty data array.');
return;
}

// Check if each item in data has the "capacity" property
if (!data.every(item => 'capacity' in item)) {
console.error('Data items should have a "capacity" property.');
return;
}

// Extract unique values for the capacity property
const uniqueCapacities = [...new Set(data.map(item => item.capacity))];
uniqueCapacities.sort((a, b) => a - b);

// Assuming filterRadiosCapacity is an existing container element
const filterRadiosCapacity = document.getElementById('storageCapacityFilter');

// Check if the container element exists
if (!filterRadiosCapacity) {
console.error('Container element not found.');
return;
}

// Create "All" option
const allInput = document.createElement('input');
allInput.type = 'radio';
allInput.id = 'storageCapacityFilter_all';
allInput.name = 'storageCapacityFilter';
allInput.value = 'all';
allInput.checked = true; // default to "All" selected

const allLabel = document.createElement('label');
allLabel.htmlFor = 'storageCapacityFilter_all';
allLabel.textContent = 'All';

filterRadiosCapacity.appendChild(allInput);
filterRadiosCapacity.appendChild(allLabel);

// Create radio buttons dynamically
uniqueCapacities.forEach(capacity => {
const input = document.createElement('input');
input.type = 'radio';
input.id = `storageCapacityFilter_${capacity}`;
input.name = 'storageCapacityFilter';
input.value = capacity;

const label = document.createElement('label');
label.htmlFor = `storageCapacityFilter_${capacity}`;
label.textContent = capacity;

filterRadiosCapacity.appendChild(input);
filterRadiosCapacity.appendChild(label);

// Attach event listener to each radio button
input.addEventListener('change', function () {
const selectedCapacity = document.querySelector('input[name="storageCapacityFilter"]:checked');
if (selectedCapacity) {
const filteredOptions = data.filter(item => selectedCapacity.value === 'all' || item.capacity == selectedCapacity.value);
populateDropdown(filteredOptions);
} else {
// Handle case when no capacity is selected
populateDropdown(data);
}
});
});

// Find the "All" radio button
const allCapacityRadio = document.getElementById('storageCapacityFilter_all');

// Add a click event listener to the "All" radio button
allCapacityRadio.addEventListener('click', function () {
const selectedCapacity = document.querySelector('input[name="storageCapacityFilter"]:checked');
if (selectedCapacity) {
const filteredOptions = data.filter(item => selectedCapacity.value === 'all' || item.capacity === selectedCapacity.value);
populateDropdown(filteredOptions);
} else {
// Handle case when no capacity is selected
populateDropdown(data);
}
});
}


})
.catch(error => console.error('Error fetching data:', error));


function populateDropdown(options) {
// Clear existing options
dropdown.innerHTML = '';

// Populate the dropdown with values from the "name" key
options.forEach(item => {
const option = document.createElement('option');
option.value = item.name;
option.textContent = item.name;
dropdown.appendChild(option);
});
}

});
Loading

0 comments on commit 6aa6375

Please sign in to comment.