save-search-request.js 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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. 'Temando_Shipping/js/model/collection-points'
  12. ], function (urlBuilder, customer, storage, quote, cacheService, shippingService, collectionPoints) {
  13. 'use strict';
  14. return function (postCode, countryId) {
  15. shippingService.isLoading(true);
  16. var url, urlParams, serviceUrl, payload;
  17. if (customer.isLoggedIn()) {
  18. url = '/carts/mine/collection-point/search-request';
  19. urlParams = {};
  20. } else {
  21. url = '/guest-carts/:cartId/collection-point/search-request';
  22. urlParams = {
  23. cartId: quote.getQuoteId()
  24. };
  25. }
  26. payload = {postcode: postCode, countryId: countryId};
  27. serviceUrl = urlBuilder.createUrl(url, urlParams);
  28. return storage.put(
  29. serviceUrl,
  30. JSON.stringify(payload)
  31. ).success(
  32. function (response) {
  33. if (quote.shippingAddress()) {
  34. // if a shipping address was selected, clear shipping rates cache
  35. cacheService.invalidateCacheForAddress(quote.shippingAddress());
  36. quote.shippingAddress.valueHasMutated();
  37. } else {
  38. // otherwise stop spinner, no new rates to display
  39. shippingService.isLoading(false);
  40. }
  41. var subscription = shippingService.getShippingRates().subscribe(function() {
  42. shippingService.isLoading(true);
  43. collectionPoints.reloadCheckoutData().always(function () {
  44. shippingService.isLoading(false);
  45. });
  46. subscription.dispose();
  47. });
  48. }
  49. ).fail(
  50. function () {
  51. shippingService.isLoading(false);
  52. }
  53. );
  54. };
  55. });