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

135 lines
4.8 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 ShippingView {
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>Shipping <a href="javascript:void(0)" ${this.el.changeShippingAddressLink.attributes} style="margin-left: 20px;">Edit</a></h3>
2024-03-08 17:41:21 +00:00
<div>Please fill in your shipping details.</div>
</div>
`;
}
return `
<div style="margin-bottom: 20px;">
2024-03-14 10:54:15 +00:00
<h3>Shipping <a href="javascript:void(0)" ${this.el.changeShippingAddressLink.attributes} style="margin-left: 20px;">Edit</a></h3>
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('#shipping_state', data.value('stateCode'))}</div>
<div>${valueOfSelect('#shipping_country', data.value('countryCode'))}</div>
2024-04-11 12:03:31 +01:00
<div>${data.value('phone')}</div>
2024-03-08 17:41:21 +00:00
</div>
`;
},
fields: {
firstName: {
'key': 'firstName',
'selector': '#shipping_first_name_field',
'valuePath': 'shipping.name.firstName',
},
lastName: {
'selector': '#shipping_last_name_field',
'valuePath': 'shipping.name.lastName',
},
street1: {
'selector': '#shipping_address_1_field',
'valuePath': 'shipping.address.addressLine1',
},
street2: {
'selector': '#shipping_address_2_field',
'valuePath': null
},
postCode: {
'selector': '#shipping_postcode_field',
'valuePath': 'shipping.address.postalCode',
},
city: {
'selector': '#shipping_city_field',
'valuePath': 'shipping.address.adminArea2',
},
stateCode: {
'selector': '#shipping_state_field',
'valuePath': 'shipping.address.adminArea1',
},
countryCode: {
'selector': '#shipping_country_field',
'valuePath': 'shipping.address.countryCode',
},
company: {
'selector': '#shipping_company_field',
'valuePath': null,
},
shipDifferentAddress: {
'selector': '#ship-to-different-address',
'valuePath': null,
2024-04-11 12:03:31 +01:00
},
phone: {
2024-04-11 17:14:22 +01:00
//'selector': '#billing_phone_field', // There is no shipping phone field.
2024-04-11 12:03:31 +01:00
'valueCallback': function (data) {
let phone = '';
const cc = data?.shipping?.phoneNumber?.countryCode;
const number = data?.shipping?.phoneNumber?.nationalNumber;
if (cc) {
phone = `+${cc} `;
}
phone += number;
return phone;
}
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);
}
copyDataToForm() {
return this.group.copyDataToForm();
2024-03-08 17:41:21 +00:00
}
}
export default ShippingView;