22declare (strict_types=1 );
33
44
5+ use Loki \Base \ViewModel \SectionConfig ;
56use Magento \Framework \View \Element \Template ;
67
78/** @var Template $block */
9+ /** @var SectionConfig $sectionConfig */
810
9- // @todo: Invalidate sections based on data_id
11+ $ sectionConfig = $ block -> getSectionConfig ();
1012?>
1113<script>
1214 document.addEventListener('alpine:init', () => {
1315 Alpine.store('LokiLocalStorage', {
1416 key: 'mage-cache-storage',
17+ sectionLifetime: <?= (int )$ sectionConfig ->getSectionDataLifeTime () * 60 ?> ,
1518 data: {},
1619 init() {
1720 const storedData = localStorage.getItem(this.key);
@@ -22,25 +25,41 @@ use Magento\Framework\View\Element\Template;
2225 }
2326
2427 let changed = false;
28+ const cookieSections = LokiCookies.get('section_data_ids') || {};
29+
2530 Object.entries(this.data).forEach(([key, value]) => {
26- if (value.data_id && value.data_id < Date. now()) {
31+ const isSectionExpired = !value.data_id || parseInt(value.data_id) + this.sectionLifetime < this.getCurrentTimestamp();
32+ const isCookieSectionExpired = cookieSections[key] && cookieSections[key] + this.sectionLifetime < this.getCurrentTimestamp();
33+
34+ if (isSectionExpired || isCookieSectionExpired) {
2735 delete this.data[key];
2836 changed = true;
2937 }
3038 })
3139
32-
3340 if (changed) {
3441 localStorage.setItem(this.key, JSON.stringify(this.data));
3542 }
3643 },
44+ getNewSectionLifetime() {
45+ return this.getCurrentTimestamp() + this.sectionLifetime;
46+ },
47+ getCurrentTimestamp() {
48+ return Math.floor(Date.now() / 1000);
49+ },
3750 save() {
3851 localStorage.setItem(this.key, JSON.stringify(this.data));
3952 },
40- refresh(sections) {
41- let url = LOKI_BASE_URL + '/customer/section/load';
53+ refresh(sections, forceNewSectionTimestamp ) {
54+ let url = new URL( LOKI_BASE_URL + '/customer/section/load') ;
4255 if (sections) {
43- url += '?sections=' + sections;
56+ url.searchParams.append('sections', sections);
57+ }
58+
59+ forceNewSectionTimestamp = !!forceNewSectionTimestamp;
60+ if (forceNewSectionTimestamp) {
61+ url.searchParams.append('force_new_section_timestamp', 'true');
62+ url.searchParams.append('_', Math.floor(Date.now() / 1000));
4463 }
4564
4665 fetch(url, {
@@ -54,7 +73,6 @@ use Magento\Framework\View\Element\Template;
5473 .then(newData => {
5574 if (typeof newData === 'object') {
5675 this.data = Object.assign(this.data, newData);
57- console.log('Fetching new data', newData);
5876 this.save();
5977 }
6078 })
@@ -68,7 +86,8 @@ use Magento\Framework\View\Element\Template;
6886 return this.data;
6987 }
7088
71- if (!this.data.hasOwnProperty(key) || !this.data[key] ) {
89+ if (!this.data[key] ) {
90+ console.log('Refreshing data:', key, this.data[key]);
7291 this.refresh(key);
7392 }
7493
@@ -79,8 +98,10 @@ use Magento\Framework\View\Element\Template;
7998 this.save();
8099 },
81100 remove(key) {
82- delete this.data[key];
83- this.save();
101+ if (key !== undefined) {
102+ delete this.data[key];
103+ this.save();
104+ }
84105 },
85106 reset() {
86107 this.data = {};
0 commit comments