Skip to content

Commit

Permalink
Lint JavaScript
Browse files Browse the repository at this point in the history
  • Loading branch information
TorbenLundsgaard committed Oct 2, 2023
1 parent 56cf944 commit eff6f8a
Show file tree
Hide file tree
Showing 8 changed files with 307 additions and 347 deletions.
7 changes: 3 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,10 @@
"uglify:edd-checkout": "mkdirp assets/integration && uglifyjs src/js/edd-checkout.js --compress --mangle --output assets/integration/edd-checkout.js",
"uglify:cf7": "mkdirp assets/integration && uglifyjs src/js/contact-form-7.js --compress --mangle --output assets/integration/contact-form-7.js",
"copy:images": "node ./bin/copy-images.js",
"format": "wp-scripts format",
"format": "wp-scripts format ./src/js",
"lint:css": "wp-scripts lint-style",
"lint:js": "wp-scripts lint-js",
"lint:js:src": "wp-scripts lint-js ./src/js",
"lint:js-fix": "wp-scripts lint-js --fix",
"lint:js": "wp-scripts lint-js ./src/js",
"lint:js-fix": "wp-scripts lint-js ./src/js --fix",
"watch": "wp-scripts start src/js/frontend/woocommerce-blocks.js --output-path=build/frontend",
"watch:tailwind": "npx tailwindcss -i ./src/scss/tailwind.scss -o ./src/scss/_tailwind-compiled.scss --watch",
"audit:prod": "npm audit --omit=dev",
Expand Down
8 changes: 4 additions & 4 deletions src/js/contact-form-7.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
document.addEventListener( 'wpcf7mailsent', function ( event ) {
window[ window.gtmkit_settings.datalayer_name ].push( {
document.addEventListener('wpcf7mailsent', function (event) {
window[window.gtmkit_settings.datalayer_name].push({
event: 'gtmkit.CF7MailSent',
formId: event.detail.contactFormId,
response: event.detail.inputs,
} );
} );
});
});
43 changes: 18 additions & 25 deletions src/js/edd-checkout.js
Original file line number Diff line number Diff line change
@@ -1,46 +1,39 @@
// eslint-disable-next-line no-undef
jQuery( document ).ready( function ( $ ) {
$( document.body ).on(
jQuery(document).ready(function ($) {
$(document.body).on(
'change',
'.edd-item-quantity',
gtmkitEddUpdateItemQuantity
);

function gtmkitEddUpdateItemQuantity() {
const $this = $( this ),
quantity = parseInt( $this.val() ),
key = $this.data( 'key' ),
downloadId = $this
.closest( '.edd_cart_item' )
.data( 'download-id' ),
const $this = $(this),
quantity = parseInt($this.val()),
key = $this.data('key'),
downloadId = $this.closest('.edd_cart_item').data('download-id'),
options = JSON.parse(
$this
.parent()
.find(
'input[name="edd-cart-download-' + key + '-options"]'
)
.find('input[name="edd-cart-download-' + key + '-options"]')
.val()
);

const cartItems = Object.entries( window.gtmkit_data.edd.cart_items );
cartItems.forEach( ( item ) => {
if ( item[ 1 ].download.download_id === downloadId ) {
if ( typeof item[ 1 ].download.price_id !== 'undefined' ) {
if ( item[ 1 ].download.price_id === options.price_id ) {
const cartItems = Object.entries(window.gtmkit_data.edd.cart_items);
cartItems.forEach((item) => {
if (item[1].download.download_id === downloadId) {
if (typeof item[1].download.price_id !== 'undefined') {
if (item[1].download.price_id === options.price_id) {
Object.assign(
window.gtmkit_data.edd.cart_items[ item[ 0 ] ],
window.gtmkit_data.edd.cart_items[item[0]],
{ quantity }
);
}
} else {
Object.assign(
window.gtmkit_data.edd.cart_items[ item[ 0 ] ],
{
quantity,
}
);
Object.assign(window.gtmkit_data.edd.cart_items[item[0]], {
quantity,
});
}
}
} );
});
}
} );
});
91 changes: 41 additions & 50 deletions src/js/edd.js
Original file line number Diff line number Diff line change
@@ -1,58 +1,49 @@
// eslint-disable-next-line no-undef
jQuery( document ).ready( function ( $ ) {
jQuery(document).ready(function ($) {
const datalayerName = window.gtmkit_settings.datalayer_name;

$( document.body ).on(
'click.eddAddToCart',
'.edd-add-to-cart',
function ( e ) {
e.preventDefault();
$(document.body).on('click.eddAddToCart', '.edd-add-to-cart', function (e) {
e.preventDefault();

const $this = $( this );
const $this = $(this);

const form = $this.parents( 'form' ).last();
const download = $this.data( 'download-id' );
const variablePrice = $this.data( 'variable-price' );
const itemPriceIds = [];
const form = $this.parents('form').last();
const download = $this.data('download-id');
const variablePrice = $this.data('variable-price');
const itemPriceIds = [];

// eslint-disable-next-line @wordpress/no-unused-vars-before-return
const itemData = JSON.parse(
form.find( '.gtmkit_product_data' ).val()
);
// eslint-disable-next-line @wordpress/no-unused-vars-before-return
const itemData = JSON.parse(form.find('.gtmkit_product_data').val());

// eslint-disable-next-line @wordpress/no-unused-vars-before-return
let quantity = parseInt( form.find( '.edd-item-quantity' ).val() );
// eslint-disable-next-line @wordpress/no-unused-vars-before-return
let quantity = parseInt(form.find('.edd-item-quantity').val());

if ( variablePrice === 'yes' ) {
if (
! form.find(
'.edd_price_option_' + download + ':checked',
form
).length
) {
return false;
}
if (variablePrice === 'yes') {
if (
!form.find('.edd_price_option_' + download + ':checked', form)
.length
) {
return false;
}

const priceMode = $this.data( 'price-mode' );
const priceMode = $this.data('price-mode');

form.find(
'.edd_price_option_' + download + ':checked',
form
).each( function ( index ) {
itemPriceIds[ index ] = $( this ).val();
form.find('.edd_price_option_' + download + ':checked', form).each(
function (index) {
itemPriceIds[index] = $(this).val();

const itemPrice = $( this ).data( 'price' );
if ( itemPrice && itemPrice > 0 ) {
itemData.price = parseFloat( itemPrice );
const itemPrice = $(this).data('price');
if (itemPrice && itemPrice > 0) {
itemData.price = parseFloat(itemPrice);
}

if ( priceMode === 'multi' ) {
if (priceMode === 'multi') {
quantity = parseInt(
form
.find(
'.edd-item-quantity' +
'[name="edd_download_quantity_' +
$( this ).val() +
$(this).val() +
'"]'
)
.val()
Expand All @@ -62,24 +53,24 @@ jQuery( document ).ready( function ( $ ) {
itemData.quantity = quantity;
}

pushDatalayer( itemData );
} );
} else {
itemData.quantity = quantity;
pushDatalayer( itemData );
}
pushDatalayer(itemData);
}
);
} else {
itemData.quantity = quantity;
pushDatalayer(itemData);
}
);
});

function pushDatalayer( itemData ) {
window[ datalayerName ].push( { ecommerce: null } );
window[ datalayerName ].push( {
function pushDatalayer(itemData) {
window[datalayerName].push({ ecommerce: null });
window[datalayerName].push({
event: 'add_to_cart',
ecommerce: {
currency: window.gtmkit_data.edd.currency,
value: itemData.price * itemData.quantity,
items: [ itemData ],
items: [itemData],
},
} );
});
}
} );
});
72 changes: 35 additions & 37 deletions src/js/frontend/woocommerce-blocks/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@ import { shippingInfo, paymentInfo, pushEvent } from './utils';
* Track the shipping rate being set
*/
addAction(
`${ actionPrefix }-checkout-set-selected-shipping-rate`,
`${actionPrefix}-checkout-set-selected-shipping-rate`,
namespace,
( { shippingRateId } ) => {
({ shippingRateId }) => {
window.gtmkit_data.wc.chosen_shipping_method = shippingRateId;

if ( window.gtmkit_settings.wc.add_shipping_info.config === 0 ) return;
if (window.gtmkit_settings.wc.add_shipping_info.config === 0) return;

if ( window.gtmkit_settings.wc.add_shipping_info.config === 2 ) {
if (window.gtmkit_settings.wc.add_shipping_info.config === 2) {
shippingInfo();
}
}
Expand All @@ -30,14 +30,14 @@ addAction(
* Track the payment method being set
*/
addAction(
`${ actionPrefix }-checkout-set-active-payment-method`,
`${actionPrefix}-checkout-set-active-payment-method`,
namespace,
( { value } ) => {
({ value }) => {
window.gtmkit_data.wc.chosen_payment_method = value;

if ( window.gtmkit_settings.wc.add_payment_info.config === 0 ) return;
if (window.gtmkit_settings.wc.add_payment_info.config === 0) return;

if ( window.gtmkit_settings.wc.add_payment_info.config === 2 ) {
if (window.gtmkit_settings.wc.add_payment_info.config === 2) {
paymentInfo();
}
}
Expand All @@ -48,91 +48,89 @@ addAction(
*
* Note, this is used to indicate checkout submission, not `purchase` which is triggered on the thanks page.
*/
addAction( `${ actionPrefix }-checkout-submit`, namespace, () => {
if ( window.gtmkit_settings.wc.add_shipping_info.config !== 0 )
addAction(`${actionPrefix}-checkout-submit`, namespace, () => {
if (window.gtmkit_settings.wc.add_shipping_info.config !== 0)
shippingInfo();
if ( window.gtmkit_settings.wc.add_payment_info.config !== 0 )
paymentInfo();
} );
if (window.gtmkit_settings.wc.add_payment_info.config !== 0) paymentInfo();
});

/**
* Change cart item quantities
*
* @summary Custom change_cart_quantity event.
*/
addAction(
`${ actionPrefix }-cart-set-item-quantity`,
`${actionPrefix}-cart-set-item-quantity`,
namespace,
( { product, quantity = 1 } ) => {
if ( product.quantity < quantity ) {
({ product, quantity = 1 }) => {
if (product.quantity < quantity) {
// quantity increase

const quantityAdded = quantity - product.quantity;
const item = JSON.parse( product.extensions.gtmkit.item );
const item = JSON.parse(product.extensions.gtmkit.item);
item.quantity = quantityAdded;

const eventParams = {
ecommerce: {
currency: window.gtmkit_data.wc.currency,
value: ( product.prices.sale_price / 100 ) * quantityAdded,
items: [ item ],
value: (product.prices.sale_price / 100) * quantityAdded,
items: [item],
},
};

pushEvent( 'add_to_cart', eventParams );
pushEvent('add_to_cart', eventParams);
} else {
// quantity decrease

const quantityRemoved = product.quantity - quantity;
const item = JSON.parse( product.extensions.gtmkit.item );
const item = JSON.parse(product.extensions.gtmkit.item);
item.quantity = quantityRemoved;

const eventParams = {
ecommerce: {
currency: window.gtmkit_data.wc.currency,
value:
( product.prices.sale_price / 100 ) * quantityRemoved,
items: [ item ],
value: (product.prices.sale_price / 100) * quantityRemoved,
items: [item],
},
};

pushEvent( 'remove_from_cart', eventParams );
pushEvent('remove_from_cart', eventParams);
}
}
);

addAction(
`${ actionPrefix }-cart-remove-item`,
`${actionPrefix}-cart-remove-item`,
namespace,
( { product, quantity } ) => {
const item = JSON.parse( product.extensions.gtmkit.item );
({ product, quantity }) => {
const item = JSON.parse(product.extensions.gtmkit.item);

const eventParams = {
ecommerce: {
currency: window.gtmkit_data.wc.currency,
value: ( product.prices.sale_price / 100 ) * quantity,
items: [ item ],
value: (product.prices.sale_price / 100) * quantity,
items: [item],
},
};

pushEvent( 'remove_from_cart', eventParams );
pushEvent('remove_from_cart', eventParams);
}
);

addAction(
`${ actionPrefix }-cart-add-item`,
`${actionPrefix}-cart-add-item`,
namespace,
( { product, quantity = 1 } ) => {
const item = JSON.parse( product.extensions.gtmkit.item );
({ product, quantity = 1 }) => {
const item = JSON.parse(product.extensions.gtmkit.item);

const eventParams = {
ecommerce: {
currency: window.gtmkit_data.wc.currency,
value: ( product.prices.sale_price / 100 ) * quantity,
items: [ item ],
value: (product.prices.sale_price / 100) * quantity,
items: [item],
},
};

pushEvent( 'add_to_cart', eventParams );
pushEvent('add_to_cart', eventParams);
}
);
Loading

0 comments on commit eff6f8a

Please sign in to comment.