pickup-locations.js 2.44 KB
Newer Older
Ketan's avatar
Ketan committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
/**
 * Refer to LICENSE.txt distributed with the Temando Shipping module for notice of license
 */

define([
    'underscore',
    'jquery',
    'Magento_Customer/js/customer-data',
    'mage/translate'
], function (_, $, customerData) {
    'use strict';

    var cacheKey = 'pickup-location-result';
    var sectionData = customerData.get(cacheKey);


    return {
        getPickupLocations: function () {
            return sectionData()['pickup-locations'] || [];
        },

        getMessage: function () {
            var locations = this.getPickupLocations();
            var searchRequest = this.getSearchRequest();
            var locationsCount = _.size(locations);

            if (_.isEmpty(searchRequest)) {
                return $.mage.__('Please wait.');
            } else if (_.isEmpty(locations) && _.size(searchRequest) > 0) {
                return $.mage.__('No pickup locations found.');
            } else {
                return $.mage.__('There were %1 results for your search.').replace('%1', locationsCount);
            }
        },

        getSearchRequest: function () {
            if (_.size(sectionData()['search-request']) > 0) {
                return sectionData()['search-request'];
            }
            return false;
        },

        selectPickupLocation: function (pickupLocationId) {
            var pickupLocations = this.getPickupLocations();
            var searchRequest = this.getSearchRequest();

            _.each(pickupLocations, function (pickupLocation) {
                pickupLocation.selected = (pickupLocation.pickup_location_id === pickupLocationId);
            });

            customerData.set(cacheKey, {
                'pickup-locations': pickupLocations,
                'search-request': searchRequest
            });
        },

        getSelectedPickupLocation: function () {
            var locations = this.getPickupLocations();
            var selectedLocation = locations.filter(function (element) {
                return element.selected;
            });

            if (selectedLocation.length === 0) {
                return false;
            } else {
                return selectedLocation;
            }
        },

        reloadCheckoutData: function () {
            return customerData.reload([cacheKey]);
        },

        clear: function() {
            customerData.set(cacheKey, {
                'pickup-locations': [],
                'search-request': {}
            });
        }
    };
});