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
+1
View File
@@ -34,3 +34,4 @@ yarn.lock
package.json package.json
package-lock.json package-lock.json
db.json db.json
server/db backup.json
+2
View File
@@ -3,6 +3,8 @@
-moz-osx-font-smoothing: grayscale; -moz-osx-font-smoothing: grayscale;
} }
body,
html,
.App { .App {
margin: 0px auto; margin: 0px auto;
overflow: hidden; overflow: hidden;
+13 -4
View File
@@ -1,12 +1,24 @@
const { atom } = require('jotai'); const { atom } = require('jotai');
const PATH = 'option-collapse'; const PATH = 'option-collapse';
const initialValue = {};
// collapse options object, initialised from local storage // collapse options object, initialised from local storage
// REFRACT: When do we read from local storage? // REFRACT: When do we read from local storage?
// on every render? // 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 // get a single option, if it exists
export const SelectCollapse = (id) => { export const SelectCollapse = (id) => {
@@ -52,9 +64,6 @@ export const setAll = async (items, isCollapsed) => {
// clear storage // clear storage
localStorage.removeItem(PATH); localStorage.removeItem(PATH);
// clear local object
console.log('debug collapse here', isCollapsed);
// call batch // call batch
BatchOperation({ items: items, isCollapsed: isCollapsed }); BatchOperation({ items: items, isCollapsed: isCollapsed });
}; };
@@ -206,7 +206,8 @@ export default function EventListWrapper() {
}, [isError]); }, [isError]);
// Events API // Events API
const eventsHandler = useCallback(async (action, payload) => { const eventsHandler = useCallback(
async (action, payload) => {
switch (action) { switch (action) {
case 'add': case 'add':
try { try {
@@ -301,7 +302,9 @@ export default function EventListWrapper() {
showErrorToast('Unrecognised request', action); showErrorToast('Unrecognised request', action);
break; break;
} }
}, []); },
[data]
);
return ( return (
<> <>
@@ -1,7 +1,6 @@
import { Menu, MenuButton, MenuItem, MenuList } from '@chakra-ui/menu'; import { Menu, MenuButton, MenuItem, MenuList } from '@chakra-ui/menu';
import { IconButton } from '@chakra-ui/button'; import { IconButton } from '@chakra-ui/button';
import { FiZap, FiPlus, FiClock, FiMinusCircle } from 'react-icons/fi'; import { FiZap, FiPlus, FiClock, FiMinusCircle } from 'react-icons/fi';
import { useEffect } from 'react';
export default function MenuActionButtons(props) { export default function MenuActionButtons(props) {
const { actionHandler } = props; const { actionHandler } = props;
@@ -10,10 +9,6 @@ export default function MenuActionButtons(props) {
backgroundColor: 'rgba(255,255,255,1)', backgroundColor: 'rgba(255,255,255,1)',
}; };
useEffect(() =>{
console.log('debug action button render')
})
return ( return (
<Menu isLazy lazyBehavior='unmount'> <Menu isLazy lazyBehavior='unmount'>
<MenuButton <MenuButton