-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_addresses.js
84 lines (68 loc) · 2.91 KB
/
_addresses.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
function renderAddress(element, address = null) {
element.innerHTML += `{% include '_address_details.html' %}`;
const form = document.querySelector('#ReChargeForm_address');
let actionUrl = ReCharge.Endpoints.list_addresses_url();
if (address) {
actionUrl = ReCharge.Endpoints.show_address_url(address.id);
ReCharge.Forms.populateAddressData(address);
ReCharge.Novum.Utils.getZipLabel(address.country);
} else {
ReCharge.Novum.Utils.getZipLabel();
}
form.setAttribute("action", actionUrl);
}
function addAddressHandler(event) {
event.preventDefault();
ReCharge.Novum.sidebarHeading.innerHTML = `{{ 'cp_add_shipping_address_label' | t }}`;
ReCharge.Novum.sidebarContent.innerHTML = ``;
renderAddress(ReCharge.Novum.sidebarContent);
document.querySelector(".address-btn").innerHTML = `
{{ 'cp_create_shipping_address_header_label' | t }}
`;
ReCharge.Novum.toggleSidebar();
getShippingBillingCountries('shipping');
}
function renderAddressDetailsHandler(event) {
event.preventDefault();
const addresses = ReCharge.Novum.addresses || [];
const addressId = event.target.closest(".js-address-edit").dataset.id;
const address = addresses.find((address) => address.id == addressId);
ReCharge.Novum.sidebarHeading.innerHTML = `{{ 'cp_edit_shipping_address_label' | t }}`;
ReCharge.Novum.sidebarContent.innerHTML = `
<p class="text-center address-info-msg">
{{ 'cp_update_shipping_addrress_update_associated_products' | t }}
</p>
`;
renderAddress(ReCharge.Novum.sidebarContent, address);
getShippingBillingCountries('shipping');
ReCharge.Novum.toggleSidebar();
}
async function getShippingBillingCountries(type) {
let countries = JSON.parse(sessionStorage.getItem('rc_shipping_countries')) || [];
if (type === 'billing') {
countries = JSON.parse(sessionStorage.getItem('rc_billing_countries')) || [];
}
if (countries.length > 0) {
ReCharge.Forms.buildCountries(type);
ReCharge.Forms.updateProvinces(document.querySelector("#country"));
} else {
try {
const schema = ReCharge.Schemas.addresses.countries();
const url = `${ReCharge.Endpoints.request_objects()}&schema=${schema}`;
const response = await axios(url);
ReCharge.Novum.Helpers.validateResponseData(response.data, 'countries');
sessionStorage.setItem(
'rc_shipping_countries',
JSON.stringify(response.data.shipping_countries)
);
sessionStorage.setItem(
'rc_billing_countries',
JSON.stringify(response.data.billing_countries)
);
ReCharge.Forms.buildCountries(type);
ReCharge.Forms.updateProvinces(document.querySelector("#country"));
} catch (error) {
console.error(error);
}
}
}