Skip to content

Commit

Permalink
add isOpen to directory menu state
Browse files Browse the repository at this point in the history
  • Loading branch information
shadeemerhi committed Mar 24, 2022
1 parent 6ce9cd4 commit a4e55d4
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 18 deletions.
18 changes: 13 additions & 5 deletions src/atoms/directoryMenuAtom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,19 @@ export type DirectoryMenuItem = {
iconColor: string;
};

export const defaultMenuItem: DirectoryMenuItem = {
displayText: "Home",
link: "/",
icon: TiHome,
iconColor: "black",
interface DirectoryMenuState {
isOpen: boolean;
selectedMenuItem: DirectoryMenuItem;
}

export const defaultMenuItem: DirectoryMenuState = {
isOpen: false,
selectedMenuItem: {
displayText: "Home",
link: "/",
icon: TiHome,
iconColor: "black",
},
};

export const directoryMenuState = atom({
Expand Down
13 changes: 8 additions & 5 deletions src/components/Navbar/Directory/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ const Directory: React.FC = () => {
const [open, setOpen] = useState(false);
const handleClose = () => setOpen(false);

const { directoryState } = useDirectory();
const { directoryState, toggleMenuOpen } = useDirectory();

return (
<Menu>
<Menu isOpen={directoryState.isOpen}>
{({ isOpen }) => (
<>
<MenuButton
Expand All @@ -29,6 +29,7 @@ const Directory: React.FC = () => {
_hover={{ outline: "1px solid", outlineColor: "gray.200" }}
mr={2}
ml={{ base: 0, md: 2 }}
onClick={toggleMenuOpen}
>
<Flex
alignItems="center"
Expand All @@ -40,15 +41,17 @@ const Directory: React.FC = () => {
<Icon
fontSize={24}
mr={{ base: 1, md: 2 }}
color={directoryState.iconColor}
as={directoryState.icon}
color={directoryState.selectedMenuItem.iconColor}
as={directoryState.selectedMenuItem.icon}
/>
<Box
display={{ base: "none", lg: "flex" }}
flexDirection="column"
fontSize="10pt"
>
<Text fontWeight={600}>{directoryState.displayText}</Text>
<Text fontWeight={600}>
{directoryState.selectedMenuItem.displayText}
</Text>
</Box>
</>
</Flex>
Expand Down
29 changes: 21 additions & 8 deletions src/hooks/useDirectory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,29 +18,42 @@ const useDirectory = () => {

const onSelectMenuItem = (menuItem: DirectoryMenuItem) => {
// setState here
setDirectoryState(menuItem);
setDirectoryState((prev) => ({
...prev,
selectedMenuItem: menuItem,
}));
router.push(menuItem.link);
};

const toggleMenuOpen = () => {
setDirectoryState((prev) => ({
...prev,
isOpen: !directoryState.isOpen,
}));
};

useEffect(() => {
const { community } = router.query;

const existingCommunity =
communityStateValue.visitedCommunities[community as string];

if (existingCommunity) {
setDirectoryState({
displayText: `r/${existingCommunity.id}`,
link: `r/${existingCommunity.id}`,
icon: FaReddit,
iconColor: "blue.500",
});
setDirectoryState((prev) => ({
...prev,
selectedMenuItem: {
displayText: `r/${existingCommunity.id}`,
link: `r/${existingCommunity.id}`,
icon: FaReddit,
iconColor: "blue.500",
},
}));
return;
}
setDirectoryState(defaultMenuItem);
}, [router.query?.community, communityStateValue.visitedCommunities]);

return { directoryState, onSelectMenuItem };
return { directoryState, onSelectMenuItem, toggleMenuOpen };
};

export default useDirectory;

0 comments on commit a4e55d4

Please sign in to comment.