-
-
Notifications
You must be signed in to change notification settings - Fork 58
Expand file tree
/
Copy pathupdate_currency.js
More file actions
57 lines (52 loc) · 2.15 KB
/
Copy pathupdate_currency.js
File metadata and controls
57 lines (52 loc) · 2.15 KB
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
import { dispatchCustomEvent } from './helper/dispatch_custom_event';
import { createHtmlElementFromString, findParentBySelector, replaceHtmlElementByIdentifier } from './helper/html_helper';
document.addEventListener('DOMContentLoaded', () => {
function updateCurrency (currencyCode, actionUrl, reloadOnly = false) {
const formData = new FormData();
formData.append('tx_cart_cart[currencyCode]', currencyCode);
fetch(actionUrl, {
method: 'POST',
body: formData
})
.then((response) => response.text())
.then((response) => {
// Reload the current page
if (reloadOnly) {
window.location.reload();
} else {
const responseAsHtml = createHtmlElementFromString(response);
replaceHtmlElementByIdentifier(responseAsHtml, '#form-cart');
replaceHtmlElementByIdentifier(responseAsHtml, '#checkout-step-shipping-method');
replaceHtmlElementByIdentifier(responseAsHtml, '#checkout-step-payment-method');
replaceHtmlElementByIdentifier(responseAsHtml, '#checkout-step-coupon');
replaceHtmlElementByIdentifier(responseAsHtml, '#checkout-step-summary');
}
dispatchCustomEvent(
'extcode:currency-updated',
{
response
}
);
});
}
/**
* Listen to changes of cart form field for currency. (in `Partials/Cart/CurrencyForm.html`)
*/
const cartCurrencySelector = document.querySelector('.cart-currency-selector');
if (cartCurrencySelector) {
cartCurrencySelector.addEventListener('change', function updateCurrencyOnChange () {
const form = findParentBySelector(this, 'form');
updateCurrency(this.value, form.getAttribute('action'));
});
}
/**
* Listen to changes of cart form field for currency. (in `Templates/Cart/Currency/Edit.html`)
*/
const currencySelector = document.querySelector('.currency-selector');
if (currencySelector) {
currencySelector.addEventListener('change', function updateCurrencyOnChange () {
const form = findParentBySelector(this, 'form');
updateCurrency(this.value, form.getAttribute('action'));
});
}
});