Skip to content

Commit 0ecb6a6

Browse files
authored
Update single-config-stock.js
1 parent 812f797 commit 0ecb6a6

File tree

1 file changed

+37
-10
lines changed

1 file changed

+37
-10
lines changed

single-config-stock.js

Lines changed: 37 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ class ConfiguratorPage {
2020
interior: null,
2121
};
2222
this.stockData = [];
23+
this.originalStockData = [];
2324
this.currentDealerName = null;
2425
this.stockDisplayLimit = 1; // Change to 3 for production
2526
this.currentStockDisplayed = 0;
@@ -1186,6 +1187,7 @@ class ConfiguratorPage {
11861187
this.updateColorOptionsForYear(yearObj.year);
11871188
this.updatePriceDisplays(); // Update price when year changes
11881189
this.updateSummary();
1190+
this.updateStockMatchLevels();
11891191
}
11901192
});
11911193

@@ -1569,6 +1571,7 @@ class ConfiguratorPage {
15691571
// Ensure stockData is an array
15701572
if (!Array.isArray(stockData)) {
15711573
console.warn("Stock data is not an array, treating as empty:", stockData);
1574+
this.originalStockData = []; // Clear original
15721575
this.stockData = [];
15731576
this.hideStockSection();
15741577
return;
@@ -1577,20 +1580,24 @@ class ConfiguratorPage {
15771580
// Check if array is empty
15781581
if (stockData.length === 0) {
15791582
console.log("No stock data available for this dealer");
1583+
this.originalStockData = []; // Clear original
15801584
this.stockData = [];
15811585
this.hideStockSection();
15821586
return;
15831587
}
1584-
1588+
1589+
// Store the original data
1590+
this.originalStockData = [...stockData]; // Keep a clean copy
15851591
this.stockData = stockData;
15861592
this.updateStockDisplay(stockData);
15871593

15881594
// Sort by match level after initial display
15891595
this.sortStockDataByMatchLevel();
15901596
this.refreshStockDisplay();
1591-
1597+
15921598
} catch (error) {
15931599
console.error("Error fetching stock data:", error);
1600+
this.originalStockData = []; // Clear original
15941601
this.stockData = [];
15951602
this.hideStockSection();
15961603
}
@@ -2069,17 +2076,14 @@ class ConfiguratorPage {
20692076
return;
20702077
}
20712078

2079+
// Re-filter duplicates first (in case data changed)
2080+
this.filterDuplicateStock();
2081+
20722082
// Sort the entire stock data array
20732083
this.sortStockDataByMatchLevel();
20742084

2075-
// If we already have items displayed, just sort the visible cards
2076-
// Otherwise, refresh the display
2077-
const displayedCards = document.querySelectorAll('.w-dyn-item.cloned');
2078-
if (displayedCards.length > 0) {
2079-
this.sortVisibleCards();
2080-
} else {
2081-
this.refreshStockDisplay();
2082-
}
2085+
// Always refresh the display when configuration changes
2086+
this.refreshStockDisplay();
20832087
}
20842088

20852089
updateLoadMoreButton() {
@@ -2147,6 +2151,29 @@ class ConfiguratorPage {
21472151
this.updateLoadMoreButton();
21482152
}
21492153

2154+
filterDuplicateStock() {
2155+
if (!this.originalStockData || !Array.isArray(this.originalStockData)) return;
2156+
2157+
const uniqueStockMap = new Map();
2158+
const filteredData = this.originalStockData.filter(stockItem => {
2159+
if (!stockItem.vehicle_code || !stockItem.color_code || !stockItem.year_code) {
2160+
return true;
2161+
}
2162+
2163+
const uniqueKey = `${stockItem.vehicle_code}-${stockItem.color_code}-${stockItem.year_code}`;
2164+
2165+
if (!uniqueStockMap.has(uniqueKey)) {
2166+
uniqueStockMap.set(uniqueKey, true);
2167+
return true;
2168+
}
2169+
2170+
return false;
2171+
});
2172+
2173+
console.log(`Re-filtered ${this.originalStockData.length} items to ${filteredData.length} unique items`);
2174+
this.stockData = [...filteredData]; // Create a new array
2175+
}
2176+
21502177
}
21512178

21522179
document.addEventListener("DOMContentLoaded", () => {

0 commit comments

Comments
 (0)