error handling + cleanup

- handle case where localstorage does not contain item
- eventhandler updates when data changes
This commit is contained in:
cv
2021-05-20 11:20:18 +02:00
parent c912a61b57
commit 438bceba57
5 changed files with 110 additions and 100 deletions
+13 -4
View File
@@ -1,12 +1,24 @@
const { atom } = require('jotai');
const PATH = 'option-collapse';
const initialValue = {};
// collapse options object, initialised from local storage
// REFRACT: When do we read from local storage?
// on every render?
export const collapseAtom = atom(JSON.parse(localStorage.getItem(PATH)) || {});
export const collapseAtom = atom(
(get) => {
const storedOptions = localStorage.getItem(PATH);
if (storedOptions == null) return initialValue;
return JSON.parse(storedOptions);
},
(get, set, newValues) => {
set(collapseAtom, newValues);
localStorage.setItem(PATH, JSON.stringify(newValues));
}
);
// get a single option, if it exists
export const SelectCollapse = (id) => {
@@ -52,9 +64,6 @@ export const setAll = async (items, isCollapsed) => {
// clear storage
localStorage.removeItem(PATH);
// clear local object
console.log('debug collapse here', isCollapsed);
// call batch
BatchOperation({ items: items, isCollapsed: isCollapsed });
};