save-service-selection.js 1.9 KB

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