Skip to content

Commit

Permalink
Unit price observation queue improvements (use a map to make sure all…
Browse files Browse the repository at this point in the history
… unit price elements are replaced even though the same product may be shown twice on the same page).
  • Loading branch information
dennisnissle committed Oct 4, 2023
1 parent bb91ee9 commit 61dfc30
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 32 deletions.
75 changes: 46 additions & 29 deletions assets/js/unit-price-observer-queue.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ window.germanized = window.germanized || {};
observer = current.observer,
priceData = current.priceData,
priceSelector = current.priceSelector,
isPrimary = current.isPrimary;
isPrimary = current.isPrimary,
unitPrices = self.getUnitPricesFromMap( priceData.unit_price );

if ( observer ) {
if ( data.products.hasOwnProperty( queueId ) ) {
Expand All @@ -64,45 +65,35 @@ window.germanized = window.germanized || {};
*/
if ( parseInt( response.product_id ) === observer.getCurrentProductId( observer ) ) {
if ( response.hasOwnProperty( 'unit_price_html' ) ) {
observer.unsetUnitPriceLoading( observer, priceData.unit_price, response.unit_price_html );
observer.unsetUnitPriceLoading( observer, unitPrices, response.unit_price_html );
} else {
observer.unsetUnitPriceLoading( observer, priceData.unit_price );
observer.unsetUnitPriceLoading( observer, unitPrices );
}
} else {
observer.unsetUnitPriceLoading( observer, priceData.unit_price );
observer.unsetUnitPriceLoading( observer, unitPrices );
}

observer.startObserver( observer, priceSelector, isPrimary );
} else {
observer.stopObserver( observer, priceSelector );
observer.unsetUnitPriceLoading( observer, priceData.unit_price );
observer.unsetUnitPriceLoading( observer, unitPrices );
observer.startObserver( observer, priceSelector, isPrimary );
}
}
});

Object.keys( data.products ).forEach( function( responseProductId ) {
if ( currentQueue.hasOwnProperty( responseProductId ) ) {
var current = currentQueue[ responseProductId ],
$unitPrice = $( current.priceData.unit_price );

if ( $unitPrice.data( 'unitPriceObserver' ) ) {
$unitPrice.data( 'unitPriceObserver' )
}
}
});
},
error: function() {
Object.keys( currentQueue ).forEach( function( queueId ) {
var current = currentQueue[ queueId ],
observer = current.observer,
priceData = current.priceData,
priceSelector = current.priceSelector,
isPrimary = current.isPrimary;
isPrimary = current.isPrimary,
unitPrices = self.getUnitPricesFromMap( priceData.unit_price );

if ( observer ) {
observer.stopObserver( observer, priceSelector );
observer.unsetUnitPriceLoading( observer, priceData.unit_price );
observer.unsetUnitPriceLoading( observer, unitPrices );
observer.startObserver( observer, priceSelector, isPrimary );
}
});
Expand All @@ -111,21 +102,47 @@ window.germanized = window.germanized || {};
} );
},

getQueueKey: function( productId, selector ) {
return ( productId + selector ).replace( /[^a-zA-Z0-9]/g, '' );
getUnitPricesFromMap: function( unitPriceMap ) {
let unitPrices = [];

unitPriceMap.forEach( function( unitPrice ) {
unitPrices = $.merge( unitPrices, $( unitPrice ) );
} );

return $( unitPrices );
},

getQueueKey: function( productId ) {
return ( productId + '' ).replace( /[^a-zA-Z0-9]/g, '' );
},

add: function( observer, productId, priceData, priceSelector, isPrimary ) {
var self = germanized.unit_price_observer_queue,
queueKey = self.getQueueKey( productId, priceSelector );

self.queue[ queueKey ] = {
'productId' : productId,
'observer' : observer,
'priceData' : priceData,
'priceSelector': priceSelector,
'isPrimary' : isPrimary
};
queueKey = self.getQueueKey( productId );

if ( self.queue.hasOwnProperty( queueKey ) ) {
priceData['unit_price'].each( function( i, obj ) {
if ( ! self.queue[ queueKey ]['priceData']['unit_price'].has( obj ) ) {
self.queue[ queueKey ]['priceData']['unit_price'].set( obj, obj );
}
});
} else {
var unitPrices = new Map();

priceData['unit_price'].each( function( i, obj ) {
unitPrices.set( obj, obj );
});

priceData['unit_price'] = unitPrices;

self.queue[ queueKey ] = {
'productId' : productId,
'observer' : observer,
'priceData' : priceData,
'priceSelector': priceSelector,
'isPrimary' : isPrimary
};
}

clearTimeout( self.timeout );
self.timeout = setTimeout( self.execute, 500 );
Expand Down
2 changes: 1 addition & 1 deletion assets/js/unit-price-observer-queue.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions assets/js/unit-price-observer.js
Original file line number Diff line number Diff line change
Expand Up @@ -444,6 +444,10 @@
$( function() {
if ( typeof wc_gzd_unit_price_observer_params !== 'undefined' ) {
$( wc_gzd_unit_price_observer_params.wrapper ).each( function() {
if ( $( this ).is( 'body' ) ) {
return;
}

$( this ).wc_germanized_unit_price_observer();
});
}
Expand Down
Loading

0 comments on commit 61dfc30

Please sign in to comment.