Skip to content

Commit

Permalink
Change conditional prop checking to filtering
Browse files Browse the repository at this point in the history
  • Loading branch information
Cloud11PL committed Oct 7, 2021
1 parent a8d5f59 commit cdebd2d
Showing 1 changed file with 7 additions and 13 deletions.
20 changes: 7 additions & 13 deletions src/components/CardMenu/CardMenu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -98,27 +98,21 @@ const CardMenu: React.FC<CardMenuProps> = props => {
prevOpen.current = open;
}, [open]);

const [loadingMenuItemIndex, setLoadingMenuItemIndex] = useState<number>(
undefined
);

useEffect(() => {
const loadingMenuItem = menuItems[loadingMenuItemIndex];
const hasAnyItemsLoadingOrWithError = menuItems
?.filter(({ withLoading }) => withLoading)
?.some(({ loading, hasError }) => loading || hasError);

if (!!loadingMenuItem) {
if (!loadingMenuItem.loading && !loadingMenuItem.hasError) {
setOpen(false);
}
if (!hasAnyItemsLoadingOrWithError) {
setOpen(false);
}
}, [loadingMenuItemIndex, menuItems]);
}, [menuItems]);

const handleMenuClick = (index: number) => {
const selectedItem = menuItems[index];
selectedItem.onSelect();

if (selectedItem.withLoading) {
setLoadingMenuItemIndex(index);
} else {
if (!selectedItem.withLoading) {
setOpen(false);
}
};
Expand Down

0 comments on commit cdebd2d

Please sign in to comment.