Skip to content

Commit

Permalink
feat: extend local storage items getter
Browse files Browse the repository at this point in the history
  • Loading branch information
frederic-maury committed Mar 29, 2024
1 parent e7f7c33 commit aa6a6f9
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions frontend/src/services/localStorage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,16 @@ export function getKey(key: string) {

export function getItem<T>(key: string, defaultValue: T): T {
const item = localStorage.getItem(getKey(key));
let value = defaultValue;
if (item) {
return JSON.parse(item);
const fromLS = JSON.parse(item);
value = {
...value,
...fromLS,
};
}
setItem(key, defaultValue);
return defaultValue;
setItem(key, value);
return value;
}

export function setItem<T>(key: string, value: T) {
Expand Down

0 comments on commit aa6a6f9

Please sign in to comment.