Skip to content

Commit 812f797

Browse files
authored
Update single-config-stock.js
1 parent 669f09c commit 812f797

File tree

1 file changed

+67
-11
lines changed

1 file changed

+67
-11
lines changed

single-config-stock.js

Lines changed: 67 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1535,17 +1535,14 @@ class ConfiguratorPage {
15351535
try {
15361536
if (!this.currentConfig.model || !dealerName) {
15371537
console.log("Missing model or dealer information for stock fetch");
1538-
this.stockData = []; // Set to empty array
1538+
this.stockData = [];
15391539
this.hideStockSection();
15401540
return;
15411541
}
15421542

1543-
// Get brand from URL path and convert to uppercase
15441543
const pathSegments = window.location.pathname.split('/');
15451544
const brandSlug = pathSegments[1];
15461545
const brandCode = brandSlug ? brandSlug.toUpperCase() : '';
1547-
1548-
// Get model code from model data
15491546
const modelCode = this.currentConfig.model.models_code || '';
15501547

15511548
console.log("Fetching stock data:", { brand: brandCode, model: modelCode, dealerName });
@@ -1571,7 +1568,15 @@ class ConfiguratorPage {
15711568

15721569
// Ensure stockData is an array
15731570
if (!Array.isArray(stockData)) {
1574-
console.error("Stock data is not an array:", stockData);
1571+
console.warn("Stock data is not an array, treating as empty:", stockData);
1572+
this.stockData = [];
1573+
this.hideStockSection();
1574+
return;
1575+
}
1576+
1577+
// Check if array is empty
1578+
if (stockData.length === 0) {
1579+
console.log("No stock data available for this dealer");
15751580
this.stockData = [];
15761581
this.hideStockSection();
15771582
return;
@@ -1586,7 +1591,7 @@ class ConfiguratorPage {
15861591

15871592
} catch (error) {
15881593
console.error("Error fetching stock data:", error);
1589-
this.stockData = []; // Set to empty array on error
1594+
this.stockData = [];
15901595
this.hideStockSection();
15911596
}
15921597
}
@@ -1696,9 +1701,12 @@ class ConfiguratorPage {
16961701

16971702
loadMoreStock() {
16981703
const listContainer = document.querySelector('#models-grid');
1699-
const templateItem = listContainer.querySelector('.w-dyn-item:not(.cloned)');
1704+
const templateItem = listContainer?.querySelector('.w-dyn-item:not(.cloned)');
17001705

17011706
if (!listContainer || !templateItem) return;
1707+
1708+
// Check if stockData is valid
1709+
if (!this.stockData || !Array.isArray(this.stockData)) return;
17021710

17031711
// Calculate how many more to show
17041712
const remainingItems = this.stockData.length - this.currentStockDisplayed;
@@ -1716,9 +1724,51 @@ class ConfiguratorPage {
17161724

17171725
// Update button visibility
17181726
this.updateLoadMoreButton();
1727+
}
1728+
1729+
sortVisibleCards() {
1730+
const listContainer = document.querySelector('#models-grid');
1731+
if (!listContainer) return;
17191732

1720-
// Re-sort after adding new items
1721-
this.updateStockMatchLevels();
1733+
// Get all cloned cards
1734+
const cards = Array.from(listContainer.querySelectorAll('.w-dyn-item.cloned'));
1735+
1736+
// Create array of cards with their match level
1737+
const cardData = cards.map(card => {
1738+
const vin = card.getAttribute('data-vin');
1739+
const stockItem = this.stockData.find(item => item.vin === vin);
1740+
1741+
if (!stockItem) return null;
1742+
1743+
const matchLevel = this.calculateMatchLevel(stockItem);
1744+
1745+
// Update the tag
1746+
const tagElement = card.querySelector('[data-element="tag"]');
1747+
if (tagElement) {
1748+
tagElement.textContent = matchLevel;
1749+
}
1750+
1751+
return {
1752+
card: card,
1753+
matchLevel: matchLevel
1754+
};
1755+
}).filter(item => item !== null);
1756+
1757+
// Sort by match level
1758+
const matchLevelOrder = {
1759+
'完全符合': 1,
1760+
'相同車款': 2,
1761+
'相似車款': 3
1762+
};
1763+
1764+
cardData.sort((a, b) => {
1765+
return matchLevelOrder[a.matchLevel] - matchLevelOrder[b.matchLevel];
1766+
});
1767+
1768+
// Re-append in sorted order
1769+
cardData.forEach(item => {
1770+
listContainer.appendChild(item.card);
1771+
});
17221772
}
17231773

17241774
// Helper method to update card elements
@@ -2022,8 +2072,14 @@ class ConfiguratorPage {
20222072
// Sort the entire stock data array
20232073
this.sortStockDataByMatchLevel();
20242074

2025-
// Refresh the display with sorted data
2026-
this.refreshStockDisplay();
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+
}
20272083
}
20282084

20292085
updateLoadMoreButton() {

0 commit comments

Comments
 (0)