checkout-7PTSF3BQ.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. // static/js/cart.js
  2. async function updateCart(form) {
  3. const formData = new FormData(form);
  4. const res = await fetch("/checkout?js=true", {
  5. method: "POST",
  6. body: formData
  7. });
  8. if (res.ok) {
  9. return res.json();
  10. } else {
  11. return { status: res.status, error: res.statusText };
  12. }
  13. }
  14. function updateCartIcon(data) {
  15. const cart_icon = document.querySelector(".cart-icon");
  16. if (cart_icon.length !== null) {
  17. let cart_icon_amount_list = cart_icon.querySelector(".cart-icon--amount");
  18. if (cart_icon_amount_list.length !== null) {
  19. cart_icon_amount_list.innerText = data.cart_breakdown.item_amount;
  20. }
  21. cart_icon.dataset.status = "busy";
  22. cart_icon.style.color = "#FF7070";
  23. cart_icon.style.textDecorationColor = "#FF7070";
  24. setTimeout(() => {
  25. cart_icon.dataset.status = "free";
  26. cart_icon.style.color = "#fff";
  27. cart_icon.style.textDecorationColor = "#fff";
  28. }, "2000");
  29. }
  30. }
  31. function updateItemRow(item_row, cart_items, item_amount) {
  32. let item = cart_items.find((item2) => item2.product_id == item_row.id);
  33. if (item) {
  34. item_row.querySelector("#quantity_manual").value = item.quantity;
  35. let buttonPlus = item_row.querySelector("#button-plus");
  36. if (!item.is_selectable) {
  37. buttonPlus.setAttribute("disabled", "disabled");
  38. buttonPlus.classList.add("cura");
  39. } else {
  40. buttonPlus.removeAttribute("disabled");
  41. buttonPlus.classList.remove("cura");
  42. }
  43. let total = item_row.querySelector(".item-total");
  44. total.querySelector(".item-total--text").textContent = "".concat(item.total);
  45. total.querySelector("#price").value = item.total;
  46. } else {
  47. item_row.remove();
  48. }
  49. }
  50. function updateBreakdown(cart_breakdown) {
  51. let subtotal_amount = Array.from(document.querySelectorAll(".subtotal--amount > .amount"));
  52. subtotal_amount.forEach((subtotal) => {
  53. subtotal.textContent = "".concat(cart_breakdown.subtotal);
  54. });
  55. let shipping = document.querySelectorAll(".subtotal--shipping > .amount");
  56. shipping.forEach((shipping2) => {
  57. shipping2.textContent = "".concat(cart_breakdown.shipping.toFixed(1));
  58. });
  59. let subtotal_total = document.querySelector(".subtotal--total > .amount");
  60. if (subtotal_total) {
  61. subtotal_total.textContent = "".concat(cart_breakdown.total.toFixed(1));
  62. }
  63. ;
  64. }
  65. function removeCheckoutTable() {
  66. document.querySelector(".checkout-table-header").remove();
  67. document.querySelector(".checkout-shipping-info").remove();
  68. document.querySelector(".checkout-empty-message").classList.remove("dn");
  69. }
  70. function updateTable(cart_items, cart_item_rows, cart_breakdown) {
  71. cart_item_rows.map((item_row) => {
  72. updateItemRow(item_row, cart_items, cart_breakdown["item_amount"]);
  73. });
  74. if (cart_items.length > 0) {
  75. updateBreakdown(cart_breakdown);
  76. } else {
  77. removeCheckoutTable();
  78. }
  79. }
  80. function displayProductSelectable(btnSubmit, isProductSelectable, isProductAvailable) {
  81. if (!btnSubmit) {
  82. return;
  83. }
  84. btnSubmit.classList.remove(
  85. "btn-product-available",
  86. "btn-product-available",
  87. "btn-product-unselectable"
  88. );
  89. if (!isProductAvailable) {
  90. btnSubmit.setAttribute("disabled", "disabled");
  91. btnSubmit.classList.add("btn-product-soldout");
  92. btnSubmit.value = "sold out";
  93. } else if (!isProductSelectable) {
  94. btnSubmit.setAttribute("disabled", "disabled");
  95. btnSubmit.classList.add("btn-product-unselectable");
  96. btnSubmit.value = "add to cart";
  97. } else {
  98. btnSubmit.removeAttribute("disabled");
  99. btnSubmit.classList.add("btn-product-available");
  100. btnSubmit.value = "add to cart";
  101. }
  102. }
  103. // static/js/checkout.js
  104. async function processForm(form) {
  105. form.addEventListener("submit", async (e) => {
  106. e.preventDefault();
  107. const data = await updateCart(form);
  108. updateCartIcon(data);
  109. const cart_items = data.cart_items;
  110. const cart_breakdown = data.cart_breakdown;
  111. const cart_item_rows = Array.from(document.querySelectorAll(".cart-table-row"));
  112. updateTable(cart_items, cart_item_rows, cart_breakdown);
  113. if ("is_product_available" in data) {
  114. const isProductSelectable = data.is_product_selectable;
  115. const isProductAvailable = data.is_product_available;
  116. const btnSubmit = document.querySelector(".btn-submit");
  117. displayProductSelectable(btnSubmit, isProductSelectable, isProductAvailable);
  118. }
  119. ;
  120. });
  121. }
  122. async function updateShippingCost(country, weight, csrftoken) {
  123. const formData = new FormData();
  124. formData.append("country", country);
  125. formData.append("weight", weight);
  126. formData.append("csrftoken", csrftoken);
  127. const res = await fetch("/checkout/shipping?js=true", {
  128. method: "POST",
  129. body: formData
  130. });
  131. if (res.ok) {
  132. return res.json();
  133. } else {
  134. return { status: res.status, error: res.statusText };
  135. }
  136. }
  137. function prepareShippingCost(selectCountry) {
  138. let weight = document.querySelector('.checkout-shipping-info > input[name="weight"]');
  139. if (weight !== void 0) {
  140. weight = Number(weight.value);
  141. }
  142. let csrftoken = "";
  143. const token = document.querySelector('.checkout-shipping-info > input[name="csrftoken"]');
  144. if (token !== void 0) {
  145. csrftoken = token.value;
  146. }
  147. let country = "";
  148. if (selectCountry.value !== "") {
  149. country = selectCountry.value;
  150. }
  151. if (!isNaN(weight) && csrftoken !== "" && country !== "") {
  152. return {
  153. weight,
  154. csrftoken,
  155. country
  156. };
  157. }
  158. }
  159. async function setShippingCost() {
  160. const selectCountry = document.querySelector("select#country");
  161. if (selectCountry !== void 0 && selectCountry !== null) {
  162. const data = prepareShippingCost(selectCountry);
  163. if (data !== void 0) {
  164. const cart_breakdown = await updateShippingCost(data.country, data.weight, data.csrftoken);
  165. updateBreakdown(cart_breakdown);
  166. }
  167. ;
  168. selectCountry.addEventListener("change", async (e) => {
  169. const data2 = prepareShippingCost(selectCountry);
  170. if (data2 !== void 0) {
  171. const cart_breakdown = await updateShippingCost(data2.country, data2.weight, data2.csrftoken);
  172. updateBreakdown(cart_breakdown);
  173. }
  174. ;
  175. });
  176. }
  177. ;
  178. }
  179. window.addEventListener("load", () => {
  180. ;
  181. (async () => {
  182. const forms = Array.from(document.querySelectorAll(".checkout-item"));
  183. await Promise.all(forms.map(async (form) => processForm(form)));
  184. await setShippingCost();
  185. })();
  186. });