Skip to content

Commit

Permalink
refactored menu iconic for a better DX (#2687)
Browse files Browse the repository at this point in the history
  • Loading branch information
sebaholesz authored Aug 10, 2023
2 parents c157b74 + 561da94 commit 41d4aa1
Show file tree
Hide file tree
Showing 5 changed files with 133 additions and 102 deletions.
15 changes: 9 additions & 6 deletions storefront/components/Layout/Header/MenuIconic/MenuIconic.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { useTypedTranslationFunction } from 'hooks/typescript/useTypedTranslatio
import { useDomainConfig } from 'hooks/useDomainConfig';
import { useWishlist } from 'hooks/useWishlist';
import { MenuIconicItem, MenuIconicItemLink, MenuIconicItemIcon } from './MenuIconicElements';
import { MenuIconicItemLogin } from './MenuIconicItemLogin';
import { MenuIconicItemUser } from './MenuIconicItemUser';

const TEST_IDENTIFIER = 'layout-header-menuiconic';

Expand All @@ -20,22 +20,25 @@ export const MenuIconic: FC = () => {

return (
<ul className="flex" data-testid={TEST_IDENTIFIER}>
<MenuIconicItem dataTestId={TEST_IDENTIFIER + '-stores'} className="max-vl:hidden">
<MenuIconicItem dataTestId={TEST_IDENTIFIER + '-stores'} className="max-lg:hidden">
<MenuIconicItemLink href={storesUrl}>
<MenuIconicItemIcon icon="Marker" />
{t('Stores')}
</MenuIconicItemLink>
</MenuIconicItem>
<MenuIconicItem dataTestId={TEST_IDENTIFIER + '-login'} className="max-vl:mr-0">
<MenuIconicItemLogin />

<MenuIconicItem dataTestId={TEST_IDENTIFIER + '-login'} className="max-lg:mr-0">
<MenuIconicItemUser />
</MenuIconicItem>
<MenuIconicItem dataTestId={TEST_IDENTIFIER + '-comparison'} className="max-vl:hidden">

<MenuIconicItem dataTestId={TEST_IDENTIFIER + '-comparison'} className="max-lg:hidden">
<MenuIconicItemLink href={productsComparisonUrl} title={t('Comparison')}>
<MenuIconicItemIcon icon="Compare" />
{!!comparison?.products.length && <span>{comparison.products.length}</span>}
</MenuIconicItemLink>
</MenuIconicItem>
<MenuIconicItem dataTestId={TEST_IDENTIFIER + '-wishlist'} className="max-vl:hidden">

<MenuIconicItem dataTestId={TEST_IDENTIFIER + '-wishlist'} className="max-lg:hidden">
<MenuIconicItemLink href={wishlistUrl} title={t('Wishlist')}>
<MenuIconicItemIcon icon={wishlist?.products.length ? 'HeartFull' : 'Heart'} />
{!!wishlist?.products.length && <span>{wishlist.products.length}</span>}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { useCurrentUserData } from 'hooks/user/useCurrentUserData';
import dynamic from 'next/dynamic';

const TEST_IDENTIFIER = 'layout-header-menuiconic-login';

const MenuIconicItemUserAuthenticated = dynamic(() =>
import('components/Layout/Header/MenuIconic/MenuIconicItemUserAuthenticated').then(
(component) => component.MenuIconicItemUserAuthenticated,
),
);

const MenuIconicItemUserUnauthenticated = dynamic(() =>
import('components/Layout/Header/MenuIconic/MenuIconicItemUserUnauthenticated').then(
(component) => component.MenuIconicItemUserUnauthenticated,
),
);

export const MenuIconicItemUser: FC = () => {
const { isUserLoggedIn } = useCurrentUserData();

return isUserLoggedIn ? (
<MenuIconicItemUserAuthenticated dataTestId={TEST_IDENTIFIER} />
) : (
<MenuIconicItemUserUnauthenticated dataTestId={TEST_IDENTIFIER} />
);
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import { useTypedTranslationFunction } from 'hooks/typescript/useTypedTranslationFunction';
import { MenuIconicItemLink, MenuIconicItemIcon, MenuIconicSubItemLink } from './MenuIconicElements';
import { getInternationalizedStaticUrls } from 'helpers/localization/getInternationalizedStaticUrls';
import { useDomainConfig } from 'hooks/useDomainConfig';
import { useAuth } from 'hooks/auth/useAuth';
import { ExtendedNextLink } from 'components/Basic/ExtendedNextLink/ExtendedNextLink';

export const MenuIconicItemUserAuthenticated: FC = ({ dataTestId }) => {
const t = useTypedTranslationFunction();
const { logout } = useAuth();
const { url } = useDomainConfig();
const [customerUrl, customerOrdersUrl, customerEditProfileUrl] = getInternationalizedStaticUrls(
['/customer', '/customer/orders', '/customer/edit-profile'],
url,
);

return (
<>
<div className="group">
<MenuIconicItemLink
href={customerUrl}
className="rounded-t-xl p-3 group-hover:bg-white group-hover:text-dark max-lg:hidden"
dataTestId={dataTestId + '-my-account'}
>
<MenuIconicItemIcon icon="User" className="group-hover:text-dark" />
{t('My account')}
</MenuIconicItemLink>

<ul className="pointer-events-none absolute top-full right-0 z-cart block min-w-[150px] origin-top-right scale-50 rounded-xl rounded-tr-none bg-white opacity-0 shadow-lg transition-all group-hover:pointer-events-auto group-hover:scale-100 group-hover:opacity-100">
<li className="block" data-testid={dataTestId + '-sub-0'}>
<MenuIconicSubItemLink href={customerOrdersUrl}>{t('My orders')}</MenuIconicSubItemLink>
</li>
<li className="block border-t border-border">
<MenuIconicSubItemLink href={customerEditProfileUrl} dataTestId={dataTestId + '-sub-1'}>
{t('Edit profile')}
</MenuIconicSubItemLink>
</li>
<li className="block border-t border-border">
<MenuIconicSubItemLink onClick={logout} dataTestId={dataTestId + '-sub-2'}>
{t('Logout')}
</MenuIconicSubItemLink>
</li>
</ul>
</div>

<div className="order-2 ml-1 flex h-9 w-9 cursor-pointer items-center justify-center text-lg outline-none lg:hidden">
<ExtendedNextLink href={customerUrl} type="static">
<div className="relative flex h-full w-full items-center justify-center text-white transition-colors">
<MenuIconicItemIcon icon="User" className="mr-0" />
</div>
</ExtendedNextLink>
</div>
</>
);
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { useTypedTranslationFunction } from 'hooks/typescript/useTypedTranslationFunction';
import { MenuIconicItemLink, MenuIconicItemIcon } from './MenuIconicElements';
import { Heading } from 'components/Basic/Heading/Heading';
import { Login } from 'components/Blocks/Popup/Login/Login';
import { useState } from 'react';
import dynamic from 'next/dynamic';

const Popup = dynamic(() => import('components/Layout/Popup/Popup').then((component) => component.Popup));

export const MenuIconicItemUserUnauthenticated: FC = ({ dataTestId }) => {
const t = useTypedTranslationFunction();
const [isLoginPopupOpened, setIsLoginPopupOpened] = useState(false);
const handleLogin = () => setIsLoginPopupOpened(true);

return (
<>
<MenuIconicItemLink
onClick={handleLogin}
className="cursor-pointer max-lg:hidden"
dataTestId={dataTestId + '-link-popup'}
>
<MenuIconicItemIcon icon="User" />
{t('Login')}
</MenuIconicItemLink>

<div className="order-2 ml-1 flex h-9 w-9 cursor-pointer items-center justify-center text-lg outline-none lg:hidden">
<div
className="relative flex h-full w-full items-center justify-center text-white transition-colors"
onClick={handleLogin}
>
<MenuIconicItemIcon icon="User" className="mr-0" />
</div>
</div>

{isLoginPopupOpened && (
<Popup onCloseCallback={() => setIsLoginPopupOpened(false)}>
<Heading type="h2">{t('Login')}</Heading>
<Login />
</Popup>
)}
</>
);
};

0 comments on commit 41d4aa1

Please sign in to comment.