diff --git a/components/common/Layout/Layout.tsx b/components/common/Layout/Layout.tsx
index 75d995c23..ff6d72aaf 100644
--- a/components/common/Layout/Layout.tsx
+++ b/components/common/Layout/Layout.tsx
@@ -49,21 +49,53 @@ interface Props {
}
}
+const ModalView: FC<{ modalView: string; closeModal(): any }> = ({
+ modalView,
+ closeModal,
+}) => {
+ return (
+
+ {modalView === 'LOGIN_VIEW' && }
+ {modalView === 'SIGNUP_VIEW' && }
+ {modalView === 'FORGOT_VIEW' && }
+
+ )
+}
+
+const ModalUI: FC = () => {
+ const { displayModal, closeModal, modalView } = useUI()
+ return displayModal ? (
+
+ ) : null
+}
+
+const SidebarView: FC<{ sidebarView: string; closeSidebar(): any }> = ({
+ sidebarView,
+ closeSidebar,
+}) => {
+ return (
+
+ {sidebarView === 'CART_VIEW' && }
+ {sidebarView === 'CHECKOUT_VIEW' && }
+ {sidebarView === 'PAYMENT_VIEW' && }
+ {sidebarView === 'SHIPPING_VIEW' && }
+
+ )
+}
+
+const SidebarUI: FC = () => {
+ const { displaySidebar, closeSidebar, sidebarView } = useUI()
+ return displaySidebar ? (
+
+ ) : null
+}
+
const Layout: FC = ({
children,
pageProps: { categories = [], ...pageProps },
}) => {
- const {
- displaySidebar,
- displayModal,
- closeSidebar,
- closeModal,
- modalView,
- sidebarView,
- } = useUI()
const { acceptedCookies, onAcceptCookies } = useAcceptCookies()
const { locale = 'en-US' } = useRouter()
-
const navBarlinks = categories.slice(0, 2).map((c) => ({
label: c.name,
href: `/search/${c.slug}`,
@@ -75,24 +107,8 @@ const Layout: FC = ({
{children}
-
- {displayModal && (
-
- {modalView === 'LOGIN_VIEW' && }
- {modalView === 'SIGNUP_VIEW' && }
- {modalView === 'FORGOT_VIEW' && }
-
- )}
-
- {displaySidebar && (
-
- {sidebarView === 'CART_VIEW' && }
- {sidebarView === 'CHECKOUT_VIEW' && }
- {sidebarView === 'PAYMENT_VIEW' && }
- {sidebarView === 'SHIPPING_VIEW' && }
-
- )}
-
+
+
{
const [state, dispatch] = React.useReducer(uiReducer, initialState)
- const openSidebar = () => dispatch({ type: 'OPEN_SIDEBAR' })
- const closeSidebar = () => dispatch({ type: 'CLOSE_SIDEBAR' })
- const toggleSidebar = () =>
- state.displaySidebar
- ? dispatch({ type: 'CLOSE_SIDEBAR' })
- : dispatch({ type: 'OPEN_SIDEBAR' })
- const closeSidebarIfPresent = () =>
- state.displaySidebar && dispatch({ type: 'CLOSE_SIDEBAR' })
+ const openSidebar = useCallback(() => dispatch({ type: 'OPEN_SIDEBAR' }), [
+ dispatch,
+ ])
+ const closeSidebar = useCallback(() => dispatch({ type: 'CLOSE_SIDEBAR' }), [
+ dispatch,
+ ])
+ const toggleSidebar = useCallback(
+ () =>
+ state.displaySidebar
+ ? dispatch({ type: 'CLOSE_SIDEBAR' })
+ : dispatch({ type: 'OPEN_SIDEBAR' }),
+ [dispatch, state.displaySidebar]
+ )
+ const closeSidebarIfPresent = useCallback(
+ () => state.displaySidebar && dispatch({ type: 'CLOSE_SIDEBAR' }),
+ [dispatch, state.displaySidebar]
+ )
- const openDropdown = () => dispatch({ type: 'OPEN_DROPDOWN' })
- const closeDropdown = () => dispatch({ type: 'CLOSE_DROPDOWN' })
+ const openDropdown = useCallback(() => dispatch({ type: 'OPEN_DROPDOWN' }), [
+ dispatch,
+ ])
+ const closeDropdown = useCallback(
+ () => dispatch({ type: 'CLOSE_DROPDOWN' }),
+ [dispatch]
+ )
- const openModal = () => dispatch({ type: 'OPEN_MODAL' })
- const closeModal = () => dispatch({ type: 'CLOSE_MODAL' })
+ const openModal = useCallback(() => dispatch({ type: 'OPEN_MODAL' }), [
+ dispatch,
+ ])
+ const closeModal = useCallback(() => dispatch({ type: 'CLOSE_MODAL' }), [
+ dispatch,
+ ])
- const openToast = () => dispatch({ type: 'OPEN_TOAST' })
- const closeToast = () => dispatch({ type: 'CLOSE_TOAST' })
+ const openToast = useCallback(() => dispatch({ type: 'OPEN_TOAST' }), [
+ dispatch,
+ ])
+ const closeToast = useCallback(() => dispatch({ type: 'CLOSE_TOAST' }), [
+ dispatch,
+ ])
- const setUserAvatar = (value: string) =>
- dispatch({ type: 'SET_USER_AVATAR', value })
+ const setUserAvatar = useCallback(
+ (value: string) => dispatch({ type: 'SET_USER_AVATAR', value }),
+ [dispatch]
+ )
- const setModalView = (view: MODAL_VIEWS) =>
- dispatch({ type: 'SET_MODAL_VIEW', view })
+ const setModalView = useCallback(
+ (view: MODAL_VIEWS) => dispatch({ type: 'SET_MODAL_VIEW', view }),
+ [dispatch]
+ )
- const setSidebarView = (view: SIDEBAR_VIEWS) =>
- dispatch({ type: 'SET_SIDEBAR_VIEW', view })
+ const setSidebarView = useCallback(
+ (view: SIDEBAR_VIEWS) => dispatch({ type: 'SET_SIDEBAR_VIEW', view }),
+ [dispatch]
+ )
const value = useMemo(
() => ({