Skip to content

Commit 8509c00

Browse files
committed
Make Alpine.store('LocalStorage').get() and refresh() asynchronous
1 parent 46fae91 commit 8509c00

1 file changed

Lines changed: 14 additions & 14 deletions

File tree

view/base/templates/script/store/localstorage-store.phtml

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ $sectionConfig = $block->getSectionConfig();
6767
save() {
6868
localStorage.setItem(this.key, JSON.stringify(this.data), true);
6969
},
70-
refresh(sections, forceNewSectionTimestamp) {
70+
async refresh(sections, forceNewSectionTimestamp) {
7171
let url = new URL(BASE_URL + '/customer/section/load');
7272
if (sections) {
7373
url.searchParams.append('sections', sections);
@@ -79,22 +79,22 @@ $sectionConfig = $block->getSectionConfig();
7979
url.searchParams.append('_', Math.floor(Date.now() / 1000));
8080
}
8181

82-
fetch(url, {
82+
const response = await fetch(url, {
8383
headers: {
8484
'X-Requested-With': 'XMLHttpRequest',
8585
},
86-
})
87-
.then((response) => {
88-
return response.json();
89-
})
90-
.then(newData => {
91-
if (typeof newData === 'object') {
92-
this.data = Object.assign(this.data, newData);
93-
this.save();
94-
}
95-
})
86+
});
87+
88+
const newData = await response.json();
89+
90+
if (typeof newData === 'object') {
91+
this.data = Object.assign(this.data, newData);
92+
this.save();
93+
}
94+
95+
return newData;
9696
},
97-
get(key) {
97+
async get(key) {
9898
if (!this.data) {
9999
return {};
100100
}
@@ -104,7 +104,7 @@ $sectionConfig = $block->getSectionConfig();
104104
}
105105

106106
if (!this.data[key] ) {
107-
this.refresh(key);
107+
this.data[key] = await this.refresh(key);
108108
}
109109

110110
return this.data[key];

0 commit comments

Comments
 (0)