Skip to content

Commit

Permalink
localStorage functions
Browse files Browse the repository at this point in the history
  • Loading branch information
chriscareycode committed Jan 14, 2025
1 parent 3e5a8d0 commit 1124f45
Showing 1 changed file with 35 additions and 3 deletions.
38 changes: 35 additions & 3 deletions src/helpers/nagiostv.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import { Host, HostList, Service, ServiceList } from "types/hostAndServiceTypes";
import { ClientSettings } from "types/settings";
import Cookie from 'js-cookie';

export function cleanDemoDataHostlist(hostlist: HostList) {
//console.log(hostlist);
Expand Down Expand Up @@ -91,7 +92,38 @@ export function convertServiceObjectToArray(servicelist: Record<string, Record<s
return serviceProblemsArray;
}


// Utility functions to handle localStorage and cookies
export const isLocalStorageEnabled = (): boolean => {
try {
const testKey = 'test';
localStorage.setItem(testKey, 'testValue');
localStorage.removeItem(testKey);
return true;
} catch (e) {
return false;
}
};

export const doesLocalStorageSettingsExist = (): boolean => {
if (isLocalStorageEnabled()) {
return localStorage.getItem('settings') !== null;
} else {
return false;
}
}

export const saveCookie = (changeString: string, obj: ClientSettings) => {
Cookie.set('settings', JSON.stringify(obj));
console.log('Saved cookie', changeString, obj);
}

export const saveLocalStorage = (changeString: string, obj: ClientSettings) => {
localStorage.setItem('settings', JSON.stringify(obj));
console.log('Saved localStorage', changeString, obj);
};
if (isLocalStorageEnabled()) {
localStorage.setItem('settings', JSON.stringify(obj));
console.log('Saved localStorage', changeString, obj);
} else {
console.error('LocalStorage is not enabled. Trying to save to cookie instead.');
saveCookie(changeString, obj);
}
};

0 comments on commit 1124f45

Please sign in to comment.