pickup-locations.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. /**
  2. * Refer to LICENSE.txt distributed with the Temando Shipping module for notice of license
  3. */
  4. define([
  5. 'underscore',
  6. 'jquery',
  7. 'Magento_Customer/js/customer-data',
  8. 'mage/translate'
  9. ], function (_, $, customerData) {
  10. 'use strict';
  11. var cacheKey = 'pickup-location-result';
  12. var sectionData = customerData.get(cacheKey);
  13. return {
  14. getPickupLocations: function () {
  15. return sectionData()['pickup-locations'] || [];
  16. },
  17. getMessage: function () {
  18. var locations = this.getPickupLocations();
  19. var searchRequest = this.getSearchRequest();
  20. var locationsCount = _.size(locations);
  21. if (_.isEmpty(searchRequest)) {
  22. return $.mage.__('Please wait.');
  23. } else if (_.isEmpty(locations) && _.size(searchRequest) > 0) {
  24. return $.mage.__('No pickup locations found.');
  25. } else {
  26. return $.mage.__('There were %1 results for your search.').replace('%1', locationsCount);
  27. }
  28. },
  29. getSearchRequest: function () {
  30. if (_.size(sectionData()['search-request']) > 0) {
  31. return sectionData()['search-request'];
  32. }
  33. return false;
  34. },
  35. selectPickupLocation: function (pickupLocationId) {
  36. var pickupLocations = this.getPickupLocations();
  37. var searchRequest = this.getSearchRequest();
  38. _.each(pickupLocations, function (pickupLocation) {
  39. pickupLocation.selected = (pickupLocation.pickup_location_id === pickupLocationId);
  40. });
  41. customerData.set(cacheKey, {
  42. 'pickup-locations': pickupLocations,
  43. 'search-request': searchRequest
  44. });
  45. },
  46. getSelectedPickupLocation: function () {
  47. var locations = this.getPickupLocations();
  48. var selectedLocation = locations.filter(function (element) {
  49. return element.selected;
  50. });
  51. if (selectedLocation.length === 0) {
  52. return false;
  53. } else {
  54. return selectedLocation;
  55. }
  56. },
  57. reloadCheckoutData: function () {
  58. return customerData.reload([cacheKey]);
  59. },
  60. clear: function() {
  61. customerData.set(cacheKey, {
  62. 'pickup-locations': [],
  63. 'search-request': {}
  64. });
  65. }
  66. };
  67. });