woocommerce-paypal-payments/modules/ppcp-axo/resources/js/Views/BillingView.js

127 lines
4.2 KiB
JavaScript
Raw Normal View History

2024-03-12 09:23:42 +00:00
import FormFieldGroup from "../Components/FormFieldGroup";
2024-03-08 17:41:21 +00:00
class BillingView {
2024-03-12 09:23:42 +00:00
constructor(selector, elements) {
this.el = elements;
2024-04-10 18:27:21 +01:00
this.group = new FormFieldGroup({
2024-03-08 17:41:21 +00:00
baseSelector: '.woocommerce-checkout',
contentSelector: selector,
template: (data) => {
const valueOfSelect = (selectSelector, key) => {
2024-03-12 09:23:42 +00:00
if (!key) {
return '';
}
2024-03-08 17:41:21 +00:00
const selectElement = document.querySelector(selectSelector);
2024-04-11 12:03:31 +01:00
if (!selectElement) {
2024-04-11 17:14:22 +01:00
return key;
2024-04-11 12:03:31 +01:00
}
2024-03-08 17:41:21 +00:00
const option = selectElement.querySelector(`option[value="${key}"]`);
return option ? option.textContent : key;
}
if (data.isEmpty()) {
return `
<div style="margin-bottom: 20px;">
2024-03-14 10:54:15 +00:00
<h3>Billing <a href="javascript:void(0)" ${this.el.changeBillingAddressLink.attributes} style="margin-left: 20px;">Edit</a></h3>
2024-03-08 17:41:21 +00:00
<div>Please fill in your billing details.</div>
</div>
`;
}
return `
<div style="margin-bottom: 20px;">
2024-03-14 10:54:15 +00:00
<h3>Billing <a href="javascript:void(0)" ${this.el.changeBillingAddressLink.attributes} style="margin-left: 20px;">Edit</a></h3>
2024-03-12 09:23:42 +00:00
<div>${data.value('email')}</div>
2024-03-08 17:41:21 +00:00
<div>${data.value('company')}</div>
<div>${data.value('firstName')} ${data.value('lastName')}</div>
<div>${data.value('street1')}</div>
<div>${data.value('street2')}</div>
<div>${data.value('postCode')} ${data.value('city')}</div>
<div>${valueOfSelect('#billing_state', data.value('stateCode'))}</div>
<div>${valueOfSelect('#billing_country', data.value('countryCode'))}</div>
</div>
`;
},
fields: {
2024-03-12 09:23:42 +00:00
email: {
'valuePath': 'email',
},
2024-03-08 17:41:21 +00:00
firstName: {
'selector': '#billing_first_name_field',
2024-04-15 17:20:02 +01:00
'valuePath': null
2024-03-08 17:41:21 +00:00
},
lastName: {
'selector': '#billing_last_name_field',
2024-04-15 17:20:02 +01:00
'valuePath': null
2024-03-08 17:41:21 +00:00
},
street1: {
'selector': '#billing_address_1_field',
'valuePath': 'billing.address.addressLine1',
},
street2: {
'selector': '#billing_address_2_field',
'valuePath': null
},
postCode: {
'selector': '#billing_postcode_field',
'valuePath': 'billing.address.postalCode',
},
city: {
'selector': '#billing_city_field',
'valuePath': 'billing.address.adminArea2',
},
stateCode: {
'selector': '#billing_state_field',
'valuePath': 'billing.address.adminArea1',
},
countryCode: {
'selector': '#billing_country_field',
'valuePath': 'billing.address.countryCode',
},
company: {
'selector': '#billing_company_field',
'valuePath': null,
2024-04-11 12:03:31 +01:00
}
2024-03-08 17:41:21 +00:00
}
});
}
2024-03-14 10:54:15 +00:00
isActive() {
2024-04-10 18:27:21 +01:00
return this.group.active;
2024-03-14 10:54:15 +00:00
}
2024-03-08 17:41:21 +00:00
activate() {
2024-04-10 18:27:21 +01:00
this.group.activate();
2024-03-08 17:41:21 +00:00
}
deactivate() {
2024-04-10 18:27:21 +01:00
this.group.deactivate();
2024-03-08 17:41:21 +00:00
}
2024-03-14 10:54:15 +00:00
refresh() {
2024-04-10 18:27:21 +01:00
this.group.refresh();
2024-03-14 10:54:15 +00:00
}
2024-03-08 17:41:21 +00:00
setData(data) {
2024-04-10 18:27:21 +01:00
this.group.setData(data);
2024-03-08 17:41:21 +00:00
}
2024-04-08 11:31:12 +01:00
inputValue(name) {
2024-04-10 18:27:21 +01:00
return this.group.inputValue(name);
2024-04-08 11:31:12 +01:00
}
fullName() {
return `${this.inputValue('firstName')} ${this.inputValue('lastName')}`.trim();
}
2024-04-15 17:20:02 +01:00
toSubmitData(data) {
return this.group.toSubmitData(data);
2024-04-10 18:27:21 +01:00
}
2024-03-08 17:41:21 +00:00
}
export default BillingView;