diff --git a/src/components/Sidebar/Sidebar.jsx b/src/components/Sidebar/Sidebar.jsx index bc9682167..bae713f48 100644 --- a/src/components/Sidebar/Sidebar.jsx +++ b/src/components/Sidebar/Sidebar.jsx @@ -1,6 +1,6 @@ import clsx from "clsx"; import PropTypes from "prop-types"; -import React, { useState, useEffect } from "react"; +import React from "react"; import { AnnounceIcon } from "../../icons/AnnounceIcon"; import { BellIcon } from "../../icons/BellIcon"; import { XolaLogoSimple } from "../../images/XolaLogoSimple"; @@ -29,52 +29,12 @@ export const Sidebar = ({ onLogoClick, isStickyHeader = true, isStickyFooter = true, + isLeftDrawerOpen, + isRightDrawerOpen, + handleDrawerStateChange, }) => { - const [isLeftDrawerOpen, setIsLeftDrawerOpen] = useState(false); - const [isRightDrawerOpen, setIsRightDrawerOpen] = useState(false); - - useEffect(() => { - setIsLeftDrawerOpen(false); // Close the drawer if notifications changes - setIsRightDrawerOpen(false); - }, [notifications]); - - const toggleLeftDrawer = () => { - if (!isLeftDrawerOpen) { - // Close the right drawer when you open the left - setIsRightDrawerOpen(false); - } - - setIsLeftDrawerOpen(!isLeftDrawerOpen); - }; - - const toggleRightDrawer = () => { - if (!isRightDrawerOpen) { - // Close the left drawer when you open the right - setIsLeftDrawerOpen(false); - } - - setIsRightDrawerOpen(!isRightDrawerOpen); - }; - const { announcements: leftDrawer, notices: rightDrawer } = notifications ?? {}; const hideRightDrawer = rightDrawer?.count <= 0 || !rightDrawer; - - const handleOnClose = (direction, closeDrawer) => { - if (direction === "left") { - if (closeDrawer) { - setIsLeftDrawerOpen(false); - } - - leftDrawer.onClose?.(); - } else { - if (closeDrawer) { - setIsRightDrawerOpen(false); - } - - rightDrawer.onClose?.(); - } - }; - const isStickyHeaderFooter = isStickyHeader && isStickyFooter; return ( @@ -97,7 +57,7 @@ export const Sidebar = ({ > {leftDrawer && (
- + handleDrawerStateChange("left")}> {leftDrawer.count} @@ -106,7 +66,7 @@ export const Sidebar = ({ {rightDrawer && (
- + handleDrawerStateChange("right")}> {rightDrawer.count} @@ -123,7 +83,7 @@ export const Sidebar = ({ title={leftDrawer.title} content={leftDrawer.content} isOpen={isLeftDrawerOpen} - onClose={(e) => handleOnClose("left", !!e)} + onClose={(e) => !!e && handleDrawerStateChange("left")} /> )} @@ -135,7 +95,7 @@ export const Sidebar = ({ title={rightDrawer.title} content={rightDrawer.content} isOpen={isRightDrawerOpen} - onClose={(e) => handleOnClose("right", !!e)} + onClose={(e) => !!e && handleDrawerStateChange("right")} /> )} @@ -169,6 +129,9 @@ Sidebar.propTypes = { isStickyHeader: PropTypes.bool, isStickyFooter: PropTypes.bool, onLogoClick: PropTypes.func.isRequired, + isLeftDrawerOpen: PropTypes.bool, + isRightDrawerOpen: PropTypes.bool, + handleDrawerStateChange: PropTypes.func, notifications: PropTypes.shape({ announcements: PropTypes.shape({ count: PropTypes.number, diff --git a/src/stories/Navigation/Sidebar.stories.js b/src/stories/Navigation/Sidebar.stories.js index 9cd12bae7..7aa176e4f 100644 --- a/src/stories/Navigation/Sidebar.stories.js +++ b/src/stories/Navigation/Sidebar.stories.js @@ -1,4 +1,4 @@ -import React from "react"; +import React, { useState } from "react"; import { AnnounceIcon, CheckIcon, HelpCenterIcon, LogoutIcon, PolicyIcon, Sidebar, StarIcon, UserIcon } from "../.."; const SidebarStories = { @@ -103,6 +103,9 @@ export const CustomLogo = () => { }; export const SidebarWithNotifications = () => { + const [isLeftDrawerOpen, setIsLeftDrawerOpen] = useState(false); + const [isRightDrawerOpen, setIsRightDrawerOpen] = useState(false); + const notifications = { announcements: { count: 1, @@ -114,11 +117,41 @@ export const SidebarWithNotifications = () => { count: 32, content:
Some content
, title: "Notifications & Pending items", + onClose: () => console.log("Notifications closed"), }, }; + + const handleDrawerStateChange = (drawer?: "left" | "right") => { + if (drawer === "left") { + if (isRightDrawerOpen) { + setIsRightDrawerOpen(false); + notifications.notices.onClose(); + } else if (isLeftDrawerOpen) { + notifications.announcements.onClose(); + } + + setIsLeftDrawerOpen(!isLeftDrawerOpen); + } else if (drawer === "right") { + if (isLeftDrawerOpen) { + setIsLeftDrawerOpen(false); + notifications.announcements.onClose(); + } else if (isRightDrawerOpen) { + notifications.notices.onClose(); + } + + setIsRightDrawerOpen(!isRightDrawerOpen); + } + }; return (
- } notifications={notifications} onLogoClick={handleLogoClick}> + } + notifications={notifications} + isLeftDrawerOpen={isLeftDrawerOpen} + isRightDrawerOpen={isRightDrawerOpen} + handleDrawerStateChange={handleDrawerStateChange} + onLogoClick={handleLogoClick} + > Sellers