checkout.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. import { updateCart, updateCartIcon, updateTable, displayProductSelectable, updateBreakdown } from './cart.js';
  2. async function processForm(form) {
  3. // handle form by preventing default behaviour
  4. // and instead manipulate data appropriately:
  5. // - send form to backend via POST
  6. // - receive response and update DOM w/o hard-refresh
  7. form.addEventListener('submit', async(e) => {
  8. e.preventDefault();
  9. // -- update form data
  10. const data = await updateCart(form);
  11. // -- update cart icon
  12. updateCartIcon(data);
  13. // -- checkout: update item row
  14. const cart_items = data.cart_items;
  15. const cart_breakdown = data.cart_breakdown;
  16. const cart_item_rows = Array.from(document.querySelectorAll(".cart-table-row"));
  17. updateTable(cart_items, cart_item_rows, cart_breakdown);
  18. // -- product: display product availability
  19. if ('is_product_available' in data) {
  20. const isProductSelectable = data.is_product_selectable;
  21. const isProductAvailable = data.is_product_available;
  22. // -- product: display product availability
  23. const btnSubmit = document.querySelector('.btn-submit');
  24. displayProductSelectable(btnSubmit, isProductSelectable, isProductAvailable);
  25. }
  26. // update shipping cost on amount change
  27. const selectCountry = document.querySelector('select#country');
  28. const shipping_data = prepareShippingCost(selectCountry);
  29. if (shipping_data !== undefined) {
  30. const cart_breakdown_shipping = await updateShippingCost(shipping_data.country, shipping_data.weight, shipping_data.csrftoken);
  31. updateBreakdown(cart_breakdown_shipping);
  32. };
  33. });
  34. };
  35. async function updateShippingCost(country, weight, csrftoken) {
  36. const formData = new FormData();
  37. formData.append("country", country);
  38. formData.append("weight", weight);
  39. formData.append("csrftoken", csrftoken);
  40. const res = await fetch("/checkout/shipping?js=true", {
  41. method: "POST",
  42. body: formData
  43. });
  44. if (res.ok) {
  45. return res.json();
  46. } else {
  47. return {status: res.status, error: res.statusText};
  48. }
  49. }
  50. function prepareShippingCost(selectCountry) {
  51. let weight = document.querySelector('.checkout-shipping-info > input[name="weight"]');
  52. if (weight !== undefined) {
  53. weight = Number(weight.value);
  54. }
  55. let csrftoken = '';
  56. const token = document.querySelector('.checkout-shipping-info > input[name="csrftoken"]');
  57. if (token !== undefined) {
  58. csrftoken = token.value;
  59. }
  60. let country = '';
  61. if (selectCountry.value !== '') {
  62. country = selectCountry.value;
  63. }
  64. if (!isNaN(weight) && csrftoken !== '' && country !== '') {
  65. return {
  66. weight: weight,
  67. csrftoken: csrftoken,
  68. country: country,
  69. };
  70. }
  71. }
  72. async function setShippingCost() {
  73. // on page-load and
  74. // on select-country change,
  75. // get selected item and make a POST request
  76. // to calculate the shipping price + return it
  77. const selectCountry = document.querySelector('select#country');
  78. if (selectCountry !== undefined && selectCountry !== null) {
  79. // update shipping costs on page load
  80. const data = prepareShippingCost(selectCountry);
  81. if (data !== undefined) {
  82. const cart_breakdown = await updateShippingCost(data.country, data.weight, data.csrftoken);
  83. updateBreakdown(cart_breakdown);
  84. };
  85. // update shipping cost on select change
  86. selectCountry.addEventListener('change', async(e) => {
  87. const data = prepareShippingCost(selectCountry);
  88. if (data !== undefined) {
  89. const cart_breakdown = await updateShippingCost(data.country, data.weight, data.csrftoken);
  90. updateBreakdown(cart_breakdown);
  91. };
  92. });
  93. };
  94. }
  95. window.addEventListener('load', () => {
  96. ;(async() => {
  97. const forms = Array.from(document.querySelectorAll('.checkout-item'));
  98. await Promise.all(forms.map(async(form) => processForm(form)));
  99. await setShippingCost();
  100. })();
  101. });