-
Notifications
You must be signed in to change notification settings - Fork 14
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Update Checkout block UI #117
Changes from 9 commits
a9225ff
1f14a57
82928b6
c3ef110
b0deadd
d227152
9db4630
a96967f
9691145
4c4ebc6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,43 +2,41 @@ const KomojuPaymentModule = (() => { | |
const { useEffect, useCallback, useRef, createElement } = window.wp.element; | ||
|
||
function registerPaymentMethod(paymentMethod) { | ||
let name = `${paymentMethod.id}` | ||
const name = `${paymentMethod.id}` | ||
const settings = window.wc.wcSettings.getSetting(`${name}_data`, {}); | ||
|
||
const komojuFields = | ||
settings.inlineFields | ||
? createElement('komoju-fields', { | ||
'token': '', | ||
'name': 'komoju_payment_token', | ||
'komoju-api': settings.komojuApi, | ||
'publishable-key': settings.publishableKey, | ||
'session': settings.session, | ||
'payment-type': settings.paymentType, | ||
'locale': settings.locale, | ||
style: { display: 'none' }, | ||
}) | ||
: null; | ||
const description = window.wp.htmlEntities.decodeEntities(settings.description || window.wp.i18n.__('title', 'komoju_woocommerce')); | ||
const descriptionDiv = createElement('div', | ||
{ | ||
id: `${name}_description`, | ||
style: { display: 'none', alignItems: 'center', justifyContent: 'center', width: '100%' } | ||
}, | ||
description | ||
); | ||
|
||
const label = createElement('div', { | ||
style: { display: 'block', alignItems: 'center', justifyContent: 'center', width: '100%' } | ||
style: { display: 'flex', alignItems: 'center', justifyContent: 'space-between', width: '95%', flexWrap: 'wrap' } | ||
}, | ||
window.wp.htmlEntities.decodeEntities(settings.title || window.wp.i18n.__('NULL GATEWAY', 'test_komoju_gateway')), | ||
createElement('span', { style: { width: 'auto' } }, window.wp.htmlEntities.decodeEntities(settings.title || window.wp.i18n.__('title', 'komoju_woocommerce'))), | ||
settings.icon ? | ||
createElement('img', { | ||
src: settings.icon, | ||
alt: settings.title || 'Payment Method Icon', | ||
style: { display: 'flex', alignItems: 'center', justifyContent: 'center', marginLeft: '10px' } | ||
}) : null, | ||
komojuFields | ||
descriptionDiv | ||
); | ||
|
||
const KomojuComponent = ({ activePaymentMethod, emitResponse, eventRegistration }) => { | ||
const { onPaymentSetup } = eventRegistration; | ||
const komojuFieldEnabledMethods = ['komoju_credit_card', 'komoju_konbini', 'komoju_bank_transfer'] | ||
|
||
useEffect(() => { | ||
if (paymentMethod.id != activePaymentMethod) return; | ||
|
||
const komojuField = document.querySelector(`komoju-fields[payment-type='${paymentMethod.paymentType}']`); | ||
komojuField.style.display = 'block'; | ||
if (komojuFieldEnabledMethods.includes(paymentMethod.id)) komojuField.style.display = 'block'; | ||
const descriptionElement = document.getElementById(`${name}_description`); | ||
descriptionElement.style.display = 'block'; | ||
|
||
const unsubscribe = onPaymentSetup(async () => { | ||
if (paymentMethod.id != activePaymentMethod) return; | ||
|
@@ -51,7 +49,7 @@ const KomojuPaymentModule = (() => { | |
}; | ||
} | ||
|
||
function submitFields(fields) { | ||
async function submitFields(fields) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Great catch! I just thought we need to carefully test both successful payments and failing payments, because making this function really There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thank you for the check! I've updated the submitFields function based on the feedback. I removed the unnecessary async keyword from the Promise constructor to clarify the function's control flow and error handling, which should help avoid any potential timing issues. |
||
return new Promise(async (resolve, reject) => { | ||
fields.addEventListener("komoju-invalid", reject); | ||
const token = await fields.submit(); | ||
|
@@ -80,13 +78,30 @@ const KomojuPaymentModule = (() => { | |
|
||
return () => { | ||
komojuField.style.display = 'none'; | ||
descriptionElement.style.display = 'none'; | ||
unsubscribe(); | ||
}; | ||
}, [ | ||
activePaymentMethod, | ||
emitResponse.responseTypes.ERROR, | ||
emitResponse.responseTypes.SUCCESS | ||
]); | ||
|
||
const komojuFields = | ||
settings.inlineFields | ||
? createElement('komoju-fields', { | ||
'token': '', | ||
'name': 'komoju_payment_token', | ||
'komoju-api': settings.komojuApi, | ||
'publishable-key': settings.publishableKey, | ||
'session': settings.session, | ||
'payment-type': settings.paymentType, | ||
'locale': settings.locale, | ||
style: { display: 'none' }, | ||
}) | ||
: null; | ||
|
||
return komojuFields; | ||
}; | ||
|
||
const Block_Gateway = { | ||
|
@@ -106,9 +121,7 @@ const KomojuPaymentModule = (() => { | |
return { | ||
init: () => { | ||
const paymentMethodData = window.wc.wcSettings.getSetting('paymentMethodData', {}); | ||
Object.values(paymentMethodData).forEach((value) => { | ||
registerPaymentMethod(value); | ||
}); | ||
Object.values(paymentMethodData).forEach(registerPaymentMethod); | ||
} | ||
}; | ||
})(); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could we set
descriptionDiv
tonull
ifdescription
is falsy (i.e., zero-length string), maybe?AIUI
description
can be empty, notably due to the default configurations for plugin versions <= 3.0.9. In such casesdescriptoinDiv
will be an empty<div>
, which just adds a void blank line under the payment name - I fear that doesn't look good.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I set
descriptionDiv
to null when it is empty.I also changed the server-side behaviour how it sets the description. Previously, it was setting default description when the description is empty. Now user are able to empty description.
Since description div & komojuField can be null, null checks have been added in the code too.