Skip to content

Instantly share code, notes, and snippets.

@shanwixcode
Last active June 28, 2020 04:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shanwixcode/cd5fca0e61054f453d3f4fdd96e28414 to your computer and use it in GitHub Desktop.
Save shanwixcode/cd5fca0e61054f453d3f4fdd96e28414 to your computer and use it in GitHub Desktop.
import wixData from 'wix-data';
import wixUsers from 'wix-users';
export function dataset2_ready() {
$w("#forex").value = 'USD';
}
export function repeater1_itemReady($item, itemData, index) {
$item("#price").text = "USD " + itemData.price;
}
export function forex_change(event) {
calculateForex();
}
function calculateForex() {
wixData.query("forex")
.eq('currency', $w("#forex").value)
.find()
.then((results) => {
let Item = results.items[0];
getFinalRates(Item);
});
}
function getFinalRates(Item) {
$w("#repeater1").onItemReady( ($item, itemData, index) => {
let newprice = Math.round((Number(Item.rate)) * (Number(itemData.price))).toFixed(0);
$item('#price').text = String($w("#forex").value + ' ' + newprice);
$item("#actualprice").text = String(newprice);
});
}
export async function addToCart_click(event) {
let $item = $w.at(event.context);
$item("#addToCart").disable();
let user = wixUsers.currentUser.id;
let clickedItem = $item("#dataset1").getCurrentItem();
let data = {
productName: clickedItem.name,
image: clickedItem.mainMedia,
quantity: 1,
price: Number($item("#actualprice").text),
currency: $w("#forex").value,
userId: user
};
await wixData.insert('cart', data);
$item("#addToCart").enable();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment