@@ -1535,14 +1535,15 @@ 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
15381539 this . hideStockSection ( ) ;
15391540 return ;
15401541 }
15411542
15421543 // Get brand from URL path and convert to uppercase
15431544 const pathSegments = window . location . pathname . split ( '/' ) ;
1544- const brandSlug = pathSegments [ 1 ] ; // e.g., "peugeot"
1545- const brandCode = brandSlug ? brandSlug . toUpperCase ( ) : '' ; // Convert to "PEUGEOT"
1545+ const brandSlug = pathSegments [ 1 ] ;
1546+ const brandCode = brandSlug ? brandSlug . toUpperCase ( ) : '' ;
15461547
15471548 // Get model code from model data
15481549 const modelCode = this . currentConfig . model . models_code || '' ;
@@ -1567,15 +1568,25 @@ class ConfiguratorPage {
15671568
15681569 const stockData = await response . json ( ) ;
15691570 console . log ( "Stock data received:" , stockData ) ;
1571+
1572+ // Ensure stockData is an array
1573+ if ( ! Array . isArray ( stockData ) ) {
1574+ console . error ( "Stock data is not an array:" , stockData ) ;
1575+ this . stockData = [ ] ;
1576+ this . hideStockSection ( ) ;
1577+ return ;
1578+ }
15701579
15711580 this . stockData = stockData ;
15721581 this . updateStockDisplay ( stockData ) ;
1573- this . updateStockMatchLevels ( ) ;
1582+
1583+ // Sort by match level after initial display
15741584 this . sortStockDataByMatchLevel ( ) ;
15751585 this . refreshStockDisplay ( ) ;
15761586
15771587 } catch ( error ) {
15781588 console . error ( "Error fetching stock data:" , error ) ;
1589+ this . stockData = [ ] ; // Set to empty array on error
15791590 this . hideStockSection ( ) ;
15801591 }
15811592 }
@@ -2003,7 +2014,10 @@ class ConfiguratorPage {
20032014 }
20042015
20052016 updateStockMatchLevels ( ) {
2006- if ( ! this . stockData || this . stockData . length === 0 ) return ;
2017+ // Add array check
2018+ if ( ! this . stockData || ! Array . isArray ( this . stockData ) || this . stockData . length === 0 ) {
2019+ return ;
2020+ }
20072021
20082022 // Sort the entire stock data array
20092023 this . sortStockDataByMatchLevel ( ) ;
@@ -2025,7 +2039,10 @@ class ConfiguratorPage {
20252039 }
20262040
20272041 sortStockDataByMatchLevel ( ) {
2028- if ( ! this . stockData || this . stockData . length === 0 ) return ;
2042+ // Ensure stockData is an array and has items
2043+ if ( ! this . stockData || ! Array . isArray ( this . stockData ) || this . stockData . length === 0 ) {
2044+ return ;
2045+ }
20292046
20302047 // Calculate match level for each item and sort
20312048 const matchLevelOrder = {
@@ -2046,7 +2063,13 @@ class ConfiguratorPage {
20462063 const listContainer = document . querySelector ( '#models-grid' ) ;
20472064 const templateItem = listContainer ?. querySelector ( '.w-dyn-item:not(.cloned)' ) ;
20482065
2049- if ( ! listContainer || ! templateItem || ! this . stockData ) return ;
2066+ if ( ! listContainer || ! templateItem ) return ;
2067+
2068+ // Check if stockData exists and is an array
2069+ if ( ! this . stockData || ! Array . isArray ( this . stockData ) || this . stockData . length === 0 ) {
2070+ this . hideStockSection ( ) ;
2071+ return ;
2072+ }
20502073
20512074 // Clear existing cloned items
20522075 const clonedItems = listContainer . querySelectorAll ( '.w-dyn-item.cloned' ) ;
0 commit comments