Skip to content

Commit

Permalink
Merge pull request #9 from gento-arg/persistencia_seleccion
Browse files Browse the repository at this point in the history
Agregado de persistencia de seleccion de sucursal
  • Loading branch information
manuelcanepa authored May 23, 2021
2 parents 4815fc0 + 841d29c commit 24ab1e3
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 6 deletions.
3 changes: 2 additions & 1 deletion view/frontend/layout/checkout_index_index.xml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@
<item name="component" xsi:type="string">uiComponent</item>
<item name="displayArea" xsi:type="string">shippingAdditional</item>
<item name="children" xsi:type="array">
<item name="shipping-option-wrapper" xsi:type="array">
<item name="shipping-oca-branch-selector-wrapper"
xsi:type="array">
<item name="component" xsi:type="string">Gento_Oca/js/view/branch-selector</item>
<item name="provider" xsi:type="string">checkoutProvider</item>
</item>
Expand Down
59 changes: 59 additions & 0 deletions view/frontend/web/js/oca-data.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
define([
'jquery',
'Magento_Customer/js/customer-data',
'jquery/jquery-storageapi'
], function (
$,
storage
) {
'use strict';

var cacheKey = 'oca-data',

/**
* @param {Object} data
*/
saveData = function (data) {
storage.set(cacheKey, data);
},

/**
* @return {*}
*/
initData = function () {
return {
'selectedOcaBranch': null
};
},

/**
* @return {*}
*/
getData = function () {
var data = storage.get(cacheKey)();

if ($.isEmptyObject(data)) {
data = $.initNamespaceStorage('mage-cache-storage').localStorage.get(cacheKey);

if ($.isEmptyObject(data)) {
data = initData();
saveData(data);
}
}

return data;
};

return {
setSelectedOcaBranch: function (data) {
var obj = getData();

obj.selectedOcaBranch = data;
saveData(obj);
},

getSelectedOcaBranch: function () {
return getData().selectedOcaBranch;
},
}
})
25 changes: 20 additions & 5 deletions view/frontend/web/js/view/branch-selector.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,16 @@ define([
'ko',
'Magento_Checkout/js/model/quote',
'Gento_Oca/js/service/branch',
'Gento_Oca/js/oca-data',
'mage/translate'
], function ($,
Component,
ko,
quote,
serviceBranch) {
], function (
$,
Component,
ko,
quote,
serviceBranch,
ocaData
) {
'use strict';

var useIdci = window.checkoutConfig.oca.useBranches;
Expand All @@ -26,6 +30,7 @@ define([
hasWarning: ko.observable(true),
initObservable: function () {
this._super();

quote.shippingAddress.subscribe(() => {
this.loadBranches(quote.shippingAddress().postcode)
});
Expand Down Expand Up @@ -55,6 +60,7 @@ define([
shippingAddress['extension_attributes']['gento_oca_branch'] = ocaBranch;
shippingAddress['extension_attributes']['gento_oca_branch_description'] = description;
quote.shippingAddress(shippingAddress)
ocaData.setSelectedOcaBranch(ocaBranch);
})
return this;
},
Expand Down Expand Up @@ -97,6 +103,15 @@ define([
return 0;
});
this.branchList(this.branchesCache[postcode]);
let ocaBranch = ocaData.getSelectedOcaBranch();
let selectedBranch = null;
if (this.branchList().length == 1) {
selectedBranch = this.branchList()[0]
}
if (selectedBranch == null) {
selectedBranch = this.branchList().find(e => e.code == ocaBranch);
}
this.selectedBranch(selectedBranch)
})
},

Expand Down

0 comments on commit 24ab1e3

Please sign in to comment.