collection-points.js 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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 = 'collection-point-result';
  12. var sectionData = customerData.get(cacheKey);
  13. return {
  14. getCollectionPoints: function () {
  15. return sectionData()['collection-points'] || [];
  16. },
  17. getMessage: function () {
  18. var collectionPoints = this.getCollectionPoints();
  19. var searchRequest = this.getSearchRequest();
  20. var cpCount = _.size(collectionPoints);
  21. if (_.isEmpty(searchRequest) || searchRequest.pending === true) {
  22. return $.mage.__('Enter country and postal code to search for a collection point.');
  23. } else if (_.isEmpty(collectionPoints) && _.size(searchRequest) > 0) {
  24. return $.mage.__('No collection points found.');
  25. } else {
  26. return $.mage.__('There were %1 results for your search.').replace('%1', cpCount);
  27. }
  28. },
  29. getSearchRequest: function () {
  30. if (_.size(sectionData()['search-request']) > 0) {
  31. return sectionData()['search-request'];
  32. }
  33. return false;
  34. },
  35. getSearchRequestCountryCode: function () {
  36. return this.getSearchRequest().country_id || '';
  37. },
  38. getSearchRequestPostCode: function () {
  39. return this.getSearchRequest().postcode || '';
  40. },
  41. selectCollectionPoint: function (collectionPointId) {
  42. var collectionPoints = this.getCollectionPoints();
  43. var searchRequest = this.getSearchRequest();
  44. _.each(collectionPoints, function (collectionPoint) {
  45. collectionPoint.selected = (collectionPoint.collection_point_id === collectionPointId);
  46. });
  47. customerData.set(cacheKey, {
  48. 'collection-points': collectionPoints,
  49. 'search-request': searchRequest
  50. });
  51. },
  52. getSelectedCollectionPoint: function () {
  53. var collectionPoints = this.getCollectionPoints();
  54. var selectedPoint = collectionPoints.filter(function (element) {
  55. return element.selected;
  56. });
  57. if (selectedPoint.length === 0) {
  58. return false;
  59. } else {
  60. return selectedPoint;
  61. }
  62. },
  63. reloadCheckoutData: function () {
  64. return customerData.reload([cacheKey]);
  65. },
  66. clear: function() {
  67. customerData.set(cacheKey, {
  68. 'collection-points': [],
  69. 'search-request': {}
  70. });
  71. }
  72. };
  73. });