Advertisement
Guest User

Untitled

a guest
Sep 3rd, 2023
479
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.47 KB | None | 0 0
  1. onSubmitHandler(evt) {
  2. evt.preventDefault();
  3. if (this.submitButton.getAttribute('aria-disabled') === 'true') return;
  4.  
  5. this.handleErrorMessage();
  6.  
  7. this.submitButton.setAttribute('aria-disabled', true);
  8. this.submitButton.classList.add('loading');
  9. this.querySelector('.loading-overlay__spinner').classList.remove('hidden');
  10.  
  11. const config = fetchConfig('javascript');
  12. config.headers['X-Requested-With'] = 'XMLHttpRequest';
  13. delete config.headers['Content-Type'];
  14.  
  15. const formData = new FormData(this.form);
  16. if (this.cart) {
  17. formData.append('sections', this.cart.getSectionsToRender().map((section) => section.id));
  18. formData.append('sections_url', window.location.pathname);
  19. this.cart.setActiveElement(document.activeElement);
  20. }
  21. config.body = formData;
  22. console.log("Form data: " + formData);
  23.  
  24. fetch(`${routes.cart_add_url}`, config)
  25. .then((response) => response.json())
  26. .then((response) => {
  27. if (response.status) {
  28. this.handleErrorMessage(response.description);
  29.  
  30. const soldOutMessage = this.submitButton.querySelector('.sold-out-message');
  31. if (!soldOutMessage) return;
  32. this.submitButton.setAttribute('aria-disabled', true);
  33. this.submitButton.querySelector('span').classList.add('hidden');
  34. soldOutMessage.classList.remove('hidden');
  35. this.error = true;
  36. return;
  37. } else if (!this.cart) {
  38. window.location = window.routes.cart_url;
  39. return;
  40. }
  41.  
  42. this.error = false;
  43. const quickAddModal = this.closest('quick-add-modal');
  44. if (quickAddModal) {
  45. document.body.addEventListener('modalClosed', () => {
  46. setTimeout(() => { this.cart.renderContents(response) });
  47. }, { once: true });
  48. quickAddModal.hide(true);
  49. } else {
  50. this.cart.renderContents(response);
  51. }
  52. })
  53. .catch((e) => {
  54. console.error(e);
  55. })
  56. .finally(() => {
  57. this.submitButton.classList.remove('loading');
  58. if (this.cart && this.cart.classList.contains('is-empty')) this.cart.classList.remove('is-empty');
  59. if (!this.error) this.submitButton.removeAttribute('aria-disabled');
  60. this.querySelector('.loading-overlay__spinner').classList.add('hidden');
  61. });
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement