2024-03-12 09:23:42 +00:00
|
|
|
import FormFieldGroup from "../Components/FormFieldGroup";
|
2024-03-08 17:41:21 +00:00
|
|
|
|
|
|
|
class ShippingView {
|
|
|
|
|
2024-05-07 15:25:06 +02:00
|
|
|
constructor(selector, elements, states) {
|
2024-03-12 09:23:42 +00:00
|
|
|
this.el = elements;
|
2024-05-07 15:25:06 +02:00
|
|
|
this.states = states;
|
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-04-30 14:50:38 +02:00
|
|
|
<div class="axo-checkout-header-section">
|
|
|
|
<h3>Shipping</h3>
|
|
|
|
<a href="javascript:void(0)" ${this.el.changeShippingAddressLink.attributes}>Edit</a>
|
|
|
|
</div>
|
2024-03-08 17:41:21 +00:00
|
|
|
<div>Please fill in your shipping details.</div>
|
|
|
|
</div>
|
|
|
|
`;
|
|
|
|
}
|
2024-05-07 15:25:06 +02:00
|
|
|
const countryCode = data.value('countryCode');
|
|
|
|
const stateCode = data.value('stateCode');
|
|
|
|
const stateName = (this.states[countryCode] && this.states[countryCode][stateCode]) ? this.states[countryCode][stateCode] : stateCode;
|
|
|
|
|
2024-03-08 17:41:21 +00:00
|
|
|
return `
|
|
|
|
<div style="margin-bottom: 20px;">
|
2024-04-30 14:50:38 +02:00
|
|
|
<div class="axo-checkout-header-section">
|
|
|
|
<h3>Shipping</h3>
|
|
|
|
<a href="javascript:void(0)" ${this.el.changeShippingAddressLink.attributes}>Edit</a>
|
|
|
|
</div>
|
2024-04-30 12:13:58 +02: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>
|
2024-05-07 15:25:06 +02:00
|
|
|
<div>${data.value('city')}, ${stateName} ${data.value('postCode')}</div>
|
|
|
|
<div>${valueOfSelect('#billing_country', 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: {
|
2024-04-30 12:13:58 +02:00
|
|
|
email: {
|
|
|
|
'valuePath': 'email',
|
|
|
|
},
|
2024-03-08 17:41:21 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2024-04-15 17:20:02 +01:00
|
|
|
toSubmitData(data) {
|
|
|
|
return this.group.toSubmitData(data);
|
2024-03-08 17:41:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
export default ShippingView;
|