select-pickup-location.js 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. /**
  2. * Refer to LICENSE.txt distributed with the Temando Shipping module for notice of license
  3. */
  4. define([
  5. 'Magento_Checkout/js/model/url-builder',
  6. 'Magento_Customer/js/model/customer',
  7. 'mage/storage',
  8. 'Magento_Checkout/js/model/quote',
  9. 'Temando_Shipping/js/model/cache-service',
  10. 'Magento_Checkout/js/model/shipping-service'
  11. ], function (urlBuilder, customer, storage, quote, cacheService, shippingService) {
  12. 'use strict';
  13. return function (selectedValue) {
  14. var url, urlParams, serviceUrl;
  15. if (customer.isLoggedIn()) {
  16. url = '/carts/mine/checkout-pickup-location/select';
  17. urlParams = {};
  18. } else {
  19. url = '/guest-carts/:cartId/checkout-pickup-location/select';
  20. urlParams = {
  21. cartId: quote.getQuoteId()
  22. };
  23. }
  24. var payload = {pickupLocationId: selectedValue};
  25. serviceUrl = urlBuilder.createUrl(url, urlParams);
  26. shippingService.isLoading(true);
  27. return storage.post(
  28. serviceUrl,
  29. JSON.stringify(payload)
  30. ).success(
  31. function (response) {
  32. if (quote.shippingAddress()) {
  33. // if a shipping address was selected, clear shipping rates cache
  34. cacheService.invalidateCacheForAddress(quote.shippingAddress());
  35. quote.shippingAddress.valueHasMutated();
  36. } else {
  37. // otherwise stop spinner, no new rates to display
  38. shippingService.isLoading(false);
  39. }
  40. }
  41. ).fail(
  42. function () {
  43. shippingService.isLoading(false);
  44. }
  45. );
  46. };
  47. });