Skip to content

Commit

Permalink
fix: handle errors, add loader
Browse files Browse the repository at this point in the history
  • Loading branch information
bashunaimiroy committed Mar 8, 2024
1 parent afaf19c commit 2d1debb
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 9 deletions.
4 changes: 4 additions & 0 deletions theme/assets/css/templates/store/item.css
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,10 @@
margin-bottom: var(--space-x3);
}

.afterpay-badge {
--theme-loader-size: 32px;
}

/* stylelint-disable-next-line selector-type-no-unknown */
afterpay-placement {
margin-top: 0;
Expand Down
21 changes: 13 additions & 8 deletions theme/assets/js/templates/store/item.js
Original file line number Diff line number Diff line change
Expand Up @@ -500,6 +500,7 @@ document.addEventListener('alpine:init', () => {
const createAfterpayBadgeData = () => ({
payments: null,
paymentAmount: null,
isLoadingAfterpayBadge: false,
selector: '.afterpay-placement',
/**
* Initialize Square Web Payments SDK
Expand All @@ -510,7 +511,7 @@ document.addEventListener('alpine:init', () => {
*/
initWebPaymentsSDK(applicationId, locationId) {
if (!this.webPaymentsSDKLoaded()) {
return null;
throw new Error('Square Web Payments SDK is not loaded');
}

const Square = window.Square;
Expand All @@ -530,9 +531,6 @@ document.addEventListener('alpine:init', () => {
const locationId = Alpine.store('product').locationId;
const storeLocale = Alpine.store('global').locale;
const payments = this.initWebPaymentsSDK(applicationId, locationId);
if (!payments) {
return;
}
payments.setLocale(storeLocale);
this.payments = payments;

Expand All @@ -548,10 +546,17 @@ document.addEventListener('alpine:init', () => {
this.attachAfterpayMessaging();
},
async attachAfterpayMessaging() {
const paymentRequest = this.buildPaymentRequest(this.paymentAmount);
const afterpay = await this.payments.afterpayClearpay(paymentRequest);
this.cleanupAfterpayMessaging();
await afterpay.attachMessaging(this.selector, Constants.AFTERPAY_MESSAGING_OPTIONS);
this.isLoadingAfterpayBadge = true;
try {
const paymentRequest = this.buildPaymentRequest(this.paymentAmount);
const afterpay = await this.payments.afterpayClearpay(paymentRequest);
this.cleanupAfterpayMessaging();
await afterpay.attachMessaging(this.selector, Constants.AFTERPAY_MESSAGING_OPTIONS);
} catch (err) {
throw new Error('Failed to attach afterpay messaging');
} finally {
this.isLoadingAfterpayBadge = false;
}
},

/**
Expand Down
7 changes: 6 additions & 1 deletion theme/components/sections/store/item/main.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,13 @@

{% if item_price_eligible and item_type_valid %}
<script src="https://web.squarecdn.com/v1/square.js"></script>
<div x-data="productAfterpayBadge()">
<div x-data="productAfterpayBadge()" class="afterpay-badge">
<div x-ref="container">
<div
x-show="isLoadingAfterpayBadge"
>
{{ include('partials/ui/loader') }}
</div>
<div class="afterpay-placement"></div>
</div>
</div>
Expand Down

0 comments on commit 2d1debb

Please sign in to comment.