Skip to content

Commit

Permalink
Invalidate localstorage after 5 minutes instead of checking params in…
Browse files Browse the repository at this point in the history
… url
  • Loading branch information
bo-lu committed Nov 5, 2024
1 parent ae3826a commit 80ce4f7
Showing 1 changed file with 39 additions and 43 deletions.
82 changes: 39 additions & 43 deletions src/reducers/localStorage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,76 +2,72 @@
import { StoreEnhancer } from 'redux';
import { INITMAINMAPINFO, INITMETADATASRCFILTER, INITSPATIALTEMPORALFILTER } from './reducer';

const SESSION_TIMEOUT = 5 * 60 * 1000; // 5 minutes in milliseconds

function checkNestedProperty(obj, props: string): boolean {
const splitted = props.split('.');
let temp = obj;
for (const index in splitted) {
if (temp[splitted[index]] === 'undefined' || !temp[splitted[index]]) return false;
if (typeof temp[splitted[index]] === 'undefined' || !temp[splitted[index]]) return false;
temp = temp[splitted[index]];
}
return true;
}


export const loadState = (): StoreEnhancer<unknown, unknown> | undefined => {
try {
const urlParams = new URLSearchParams(window.location.search);
if (!urlParams.toString()) {
const serializedState = localStorage.getItem('state');
if (serializedState === null) {
return undefined;
}
const state = JSON.parse(serializedState);
if (!checkNestedProperty(state, 'mappingReducer.spatempfilter')) {
state['mappingReducer'].spatempfilter = INITSPATIALTEMPORALFILTER;
}
if (!checkNestedProperty(state, 'mappingReducer.spatialfilter')) {
state['mappingReducer'].spatialfilter = [];
}
if (!checkNestedProperty(state, 'mappingReducer.metasrcfilter')) {
state['mappingReducer'].metasrcfilter = INITMETADATASRCFILTER;
}
if (!checkNestedProperty(state, 'mappingReducer.stacfilter')) {
state['mappingReducer'].stacfilter = [];
}
if (!checkNestedProperty(state, 'mappingReducer.center')) {
state['mappingReducer'].center = INITMAINMAPINFO.center;
}
if (!checkNestedProperty(state, 'mappingReducer.zoom')) {
state['mappingReducer'].zoom = INITMAINMAPINFO.zoom;
}
if (!checkNestedProperty(state, 'mappingReducer.freezeMapSearch')) {
state['mappingReducer'].freezeMapSearch = { freeze: true };
}
return state;
const serializedState = localStorage.getItem('state');
if (serializedState === null) {
return undefined;
}

// Check if the session has expired (5 minutes timeout)
const currentTime = new Date().getTime();
if (currentTime - parseInt(savedTime) > SESSION_TIMEOUT) {
localStorage.clear(); // Invalidate state if session expired
return undefined;
}
else {
const serializedState = localStorage.getItem('state');
if (serializedState === null) {
return undefined;
}
const state = JSON.parse(serializedState);
state['mappingReducer'].spatempfilter = { extents: [], startDate: "", endDate: "" };

const state = JSON.parse(serializedState);
if (!checkNestedProperty(state, 'mappingReducer.spatempfilter')) {
state['mappingReducer'].spatempfilter = INITSPATIALTEMPORALFILTER;
}
if (!checkNestedProperty(state, 'mappingReducer.spatialfilter')) {
state['mappingReducer'].spatialfilter = [];
state['mappingReducer'].metasrcfilter = { sources: [], dataCollection: '', polarization: '', orbitDirection: '' };
}
if (!checkNestedProperty(state, 'mappingReducer.metasrcfilter')) {
state['mappingReducer'].metasrcfilter = INITMETADATASRCFILTER;
}
if (!checkNestedProperty(state, 'mappingReducer.stacfilter')) {
state['mappingReducer'].stacfilter = [];
state['mappingReducer'].center = { lat: 54.5, lng: -115 };
state['mappingReducer'].zoom = [];
}
if (!checkNestedProperty(state, 'mappingReducer.center')) {
state['mappingReducer'].center = INITMAINMAPINFO.center;
}
if (!checkNestedProperty(state, 'mappingReducer.zoom')) {
state['mappingReducer'].zoom = INITMAINMAPINFO.zoom;
}
if (!checkNestedProperty(state, 'mappingReducer.freezeMapSearch')) {
state['mappingReducer'].freezeMapSearch = { freeze: true };
return state;
}
return state;
} catch (err) {
return undefined;
}
};

export const saveState = (state: unknown): void => {
try {
// console.log(state);
const serializedState = JSON.stringify(state);
// console.log(serializedState)
const currentTime = new Date().getTime().toString();

// Clear existing state in localStorage
localStorage.clear();

// Save the state and the timestamp
localStorage.setItem('state', serializedState);
localStorage.setItem('stateTimestamp', currentTime);
} catch (err) {
// ignore write errors
console.log('error set local:', err);
Expand Down

0 comments on commit 80ce4f7

Please sign in to comment.