Skip to content

Commit

Permalink
Add psu filters
Browse files Browse the repository at this point in the history
  • Loading branch information
catherinepapad committed Dec 30, 2023
1 parent 6aa6375 commit 0fbf827
Show file tree
Hide file tree
Showing 4 changed files with 311 additions and 8 deletions.
2 changes: 1 addition & 1 deletion web-app/src/builder-gpu.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ document.addEventListener('DOMContentLoaded', function () {

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

function updateDropdown() {
// Get the selected values from all filters
Expand Down
2 changes: 1 addition & 1 deletion web-app/src/builder-mobo.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ document.addEventListener('DOMContentLoaded', function () {

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

function updateDropdown() {
// Get the selected values from all filters
Expand Down
241 changes: 241 additions & 0 deletions web-app/src/builder-psu.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,241 @@
import {config} from './config.js';

document.addEventListener('DOMContentLoaded', function () {
// Get the dropdown element and the static radio button elements
const dropdown = document.getElementById('selectPSU');
const filterRadiosModularity = document.querySelectorAll('input[name="psuModularityFilter"]');
const filterRadiosFormFactor = document.querySelectorAll('input[name="psuFormFactorFilter"]');
const filterRadiosEfficiency = document.querySelectorAll('input[name="psuEfficiencyFilter"]');

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

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

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

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

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

function updateDropdown() {
// Get the selected values from all filters
const selectedManufacturer = document.querySelector('input[name="psuManufacturerFilter"]:checked').value;
const selectedWattage = document.querySelector('input[name="psuWattageFilter"]:checked').value;
const selectedModularity = document.querySelector('input[name="psuModularityFilter"]:checked').value;
const selectedFormFactor = document.querySelector('input[name="psuFormFactorFilter"]:checked').value;
const selectedEfficiency = document.querySelector('input[name="psuEfficiencyFilter"]:checked').value;

// Filter the options based on the selected filters
const filteredOptions = data.filter(item =>
((selectedManufacturer === 'all' || item.Manufacturer_name === selectedManufacturer)) &&
(selectedWattage === 'all' || item.wattage == selectedWattage(selectedWattage,10)) && (selectedModularity === 'all' || item.modularity === selectedModularity) && (selectedEfficiency === 'all' || item.efficiency_certification === selectedEfficiency) && (selectedFormFactor === 'all' || item.form_factor === selectedFormFactor)
);

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('psuManufacturerFilter');

// 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 = 'psuManufacturerFilter_all';
allInput.name = 'psuManufacturerFilter';
allInput.value = 'all';
allInput.checked = true; // default to "All" selected

const allLabel = document.createElement('label');
allLabel.htmlFor = 'psuManufacturerFilter_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 = `psuManufacturerFilter_${manufacturer}`;
input.name = 'psuManufacturerFilter';
input.value = manufacturer;

const label = document.createElement('label');
label.htmlFor = `psuManufacturerFilter_${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="psuManufacturerFilter"]: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('psuManufacturerFilter_all');

// Add a click event listener to the "All" radio button
allManufacturerRadio.addEventListener('click', function () {
const selectedManufacturer = document.querySelector('input[name="psuManufacturerFilter"]: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 createWattageFilter(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 "wattage" property
if (!data.every(item => 'wattage' in item)) {
console.error('Data items should have a "wattage" property.');
return;
}

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

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

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

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

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

filterRadiosWattages.appendChild(allInput);
filterRadiosWattages.appendChild(allLabel);

// Create radio buttons dynamically
uniqueWattages.forEach(wattage => {
const input = document.createElement('input');
input.type = 'radio';
input.id = `psuWattageFilter_${wattage}`;
input.name = 'psuWattageFilter';
input.value = wattage;

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

filterRadiosWattages.appendChild(input);
filterRadiosWattages.appendChild(label);

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

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

// Add a click event listener to the "All" radio button
allWattageRadio.addEventListener('click', function () {
const selectedWattage = document.querySelector('input[name="psuWattageFilter"]:checked');
if (selectedWattage) {
const filteredOptions = data.filter(item => selectedWattage.value === 'all' || item.wattage === selectedWattage.value);
populateDropdown(filteredOptions);
} else {
// Handle case when no wattage 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);
});
}

});
74 changes: 68 additions & 6 deletions web-app/views/builder.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
</div>


<label for="dropdownCPU">Select a CPU:</label>
<label for="selectCPU">Select a CPU:</label>
<select id="selectCPU" name="selectCPU">
<!-- Options will be dynamically added here -->
</select>
Expand Down Expand Up @@ -119,7 +119,7 @@
</div>


<label for="dropdownMOBO">Select a Motherboard:</label>
<label for="selectMOBO">Select a Motherboard:</label>
<select id="selectMOBO" name="selectMONO">
<!-- Options will be dynamically added here -->
</select>
Expand Down Expand Up @@ -173,7 +173,7 @@
</div>


<label for="dropdownRAM">Select a Memory:</label>
<label for="selectRAM">Select a Memory:</label>
<select id="selectRAM" name="selecRAM">
<!-- Options will be dynamically added here -->
</select>
Expand Down Expand Up @@ -220,7 +220,7 @@
</div>


<label for="dropdownGPU">Select a Graphics Card:</label>
<label for="selectGPU">Select a Graphics Card:</label>
<select id="selectGPU" name="selecGPU">
<!-- Options will be dynamically added here -->
</select>
Expand Down Expand Up @@ -285,8 +285,69 @@
</div>


<label for="dropdownstorage">Select Storage:</label>
<select id="selectStorage" name="selecStorage">
<label for="selectStorage">Select Storage:</label>
<select id="selectStorage" name="selectStorage">
<!-- Options will be dynamically added here -->
</select>

</div>

<!--------------------------------------------------------------------->
<div class="componentContainer" id="psuContainer">
<h3>Choose Power Supply</h3>
<label class="filterLabel">Manufacturer:</label>
<div id="psuManufacturerFilter">
<!-- Dynamically added here -->
</div>

<label class="filterLabel">Wattage:</label>
<div id="psuWattageFilter">
<!-- Dynamically added here -->
</div>

<label class="filterLabel">Modularity:</label>
<div>
<input type="radio" id="psuModularityAll" name="psuModularityFilter" value="all" checked>
<label for="psuModularityAll">All</label>
<input type="radio" id="psuModularityFull" name="psuModularityFilter" value="Modular">
<label for="psuModularityFull">Modular</label>
<input type="radio" id="psuModularitySemi" name="psuModularityFilter" value="Semi-Modular">
<label for="psuModularitySemi">Semi Modular</label>
<input type="radio" id="psuModularityNon" name="psuModularityFilter" value="Non-Modular">
<label for="psuModularityNon">Non Modular</label>
</div>

<label class="filterLabel">Form Factor:</label>
<div>
<input type="radio" id="psuFormFactorAll" name="psuFormFactorFilter" value="all" checked>
<label for="psuFormFactorAll">All</label>
<input type="radio" id="psuFormFactorATX" name="psuFormFactorFilter" value="ATX">
<label for="psuFormFactorATX">ATX</label>
<input type="radio" id="psuFormFactorEPS" name="psuFormFactorFilter" value="EPS">
<label for="psuFormFactorEPS">EPS</label>
</div>

<label class="filterLabel">Efficiency Certification:</label>
<div>
<input type="radio" id="psuEfficiencyAll" name="psuEfficiencyFilter" value="all" checked>
<label for="psuEfficiencyAll">All</label>
<input type="radio" id="psuEfficiencyStandard" name="psuEfficiencyFilter" value="80 PLUS Standard">
<label for="psuEfficiencyStandard">80 PLUS Standard</label>
<input type="radio" id="psuEfficiencyBronze" name="psuEfficiencyFilter" value="80 PLUS Bronze">
<label for="psuEfficiencyBronze">80 PLUS Bronze</label>
<input type="radio" id="psuEfficiencySilver" name="psuEfficiencyFilter" value="80 PLUS Silver">
<label for="psuEfficiencySilver">80 PLUS Silver</label>
<input type="radio" id="psuEfficiencyGold" name="psuEfficiencyFilter" value="80 PLUS Gold">
<label for="psuEfficiencyGold">80 PLUS Gold</label>
<input type="radio" id="psuEfficiencyPlatinum" name="psuEfficiencyFilter" value="80 PLUS Platinum">
<label for="psuEfficiencyPlatinum">80 PLUS Platinum</label>
<input type="radio" id="psuEfficiencyTitanium" name="psuEfficiencyFilter" value="80 PLUS Titanium">
<label for="psuEfficiencyTitanium">80 PLUS Titanium</label>
</div>


<label for="selectPSU">Select Power Supply:</label>
<select id="selectPSU" name="selectPSU">
<!-- Options will be dynamically added here -->
</select>

Expand All @@ -301,6 +362,7 @@
<script src="../src/builder-ram.js" type="module"></script>
<script src="../src/builder-gpu.js" type="module"></script>
<script src="../src/builder-storage.js" type="module"></script>
<script src="../src/builder-psu.js" type="module"></script>
<link rel="stylesheet" href="../public/builder.css">

</body>
Expand Down

0 comments on commit 0fbf827

Please sign in to comment.