Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor header components and add talons #1793

Merged
merged 5 commits into from
Sep 30, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions packages/peregrine/lib/talons/Header/useCartTrigger.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { useCallback, useEffect } from 'react';
import { useCartContext } from '@magento/peregrine/lib/context/cart';

export const useCartTrigger = () => {
const [{ details }, { getCartDetails, toggleCart }] = useCartContext();
const { items_qty: itemCount } = details;

useEffect(() => {
getCartDetails();
}, [getCartDetails]);

const handleClick = useCallback(() => {
toggleCart();
}, [toggleCart]);

return {
handleClick,
itemCount
};
};
20 changes: 20 additions & 0 deletions packages/peregrine/lib/talons/Header/useHeader.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { useCallback } from 'react';
import { useAppContext } from '@magento/peregrine/lib/context/app';

export const useHeader = () => {
const [
{ hasBeenOffline, isOnline, searchOpen },
{ toggleSearch }
] = useAppContext();

const handleSearchTriggerClick = useCallback(() => {
toggleSearch();
}, [toggleSearch]);

return {
handleSearchTriggerClick,
hasBeenOffline,
isOnline,
searchOpen
};
};
14 changes: 14 additions & 0 deletions packages/peregrine/lib/talons/Header/useNavigationTrigger.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { useCallback } from 'react';
import { useAppContext } from '@magento/peregrine/lib/context/app';

export const useNavigationTrigger = () => {
const [, { toggleDrawer }] = useAppContext();

const handleOpenNavigation = useCallback(() => {
toggleDrawer('nav');
}, [toggleDrawer]);

return {
handleOpenNavigation
};
};
10 changes: 10 additions & 0 deletions packages/peregrine/lib/talons/Header/useSearchTrigger.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { useCallback } from 'react';
export const useSearchTrigger = props => {
const { onClick } = props;
const handleClick = useCallback(() => {
onClick();
}, [onClick]);
return {
handleClick
};
};

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ exports[`Cart icon svg has fill and correct value when cart contains items 1`] =
<button
aria-label="Toggle mini cart. You have 10 items in your cart."
className="a"
onClick={[MockFunction]}
onClick={[Function]}
>
<span>
<svg
Expand Down Expand Up @@ -43,7 +43,7 @@ exports[`Cart icon svg has no fill when cart is empty 1`] = `
<button
aria-label="Toggle mini cart. You have 0 items in your cart."
className="a"
onClick={[MockFunction]}
onClick={[Function]}
>
<span>
<svg
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,44 +1,61 @@
import React from 'react';
import { createTestInstance } from '@magento/peregrine';
import { useCartContext } from '@magento/peregrine/lib/context/cart';

import Trigger from '../cartTrigger';
import CartTrigger from '../cartTrigger';

jest.mock('@magento/peregrine/lib/context/cart', () => {
const state = {
details: {}
};
const api = {
getCartDetails: jest.fn(),
toggleCart: jest.fn()
};

const useCartContext = jest.fn(() => [state, api]);

return { useCartContext };
});

const classes = {
root: 'a'
};
const getCartDetails = jest.fn();
const toggleCart = jest.fn();

test('Cart icon svg has no fill when cart is empty', () => {
const props = {
cart: {
const [cartState, cartApi] = useCartContext();
useCartContext.mockReturnValueOnce([
{
...cartState,
details: {
items_qty: 0
}
},
classes,
getCartDetails,
toggleCart
};
{
...cartApi
}
]);

const component = createTestInstance(<Trigger {...props} />);
const component = createTestInstance(<CartTrigger classes={classes} />);

expect(component.toJSON()).toMatchSnapshot();
});

test('Cart icon svg has fill and correct value when cart contains items', () => {
const props = {
cart: {
const [cartState, cartApi] = useCartContext();
useCartContext.mockReturnValueOnce([
{
...cartState,
details: {
items_qty: 10
}
},
classes,
getCartDetails,
toggleCart
};
{
...cartApi
}
]);

const component = createTestInstance(<Trigger {...props} />);
const component = createTestInstance(<CartTrigger classes={classes} />);

expect(component.toJSON()).toMatchSnapshot();
});
4 changes: 0 additions & 4 deletions packages/venia-ui/lib/components/Header/cartCounter.css

This file was deleted.

27 changes: 0 additions & 27 deletions packages/venia-ui/lib/components/Header/cartCounter.js

This file was deleted.

5 changes: 5 additions & 0 deletions packages/venia-ui/lib/components/Header/cartTrigger.css
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,8 @@
height: 3rem;
min-width: 3rem;
}

.counter {
font-weight: 600;
margin-left: 0.3rem;
}
34 changes: 12 additions & 22 deletions packages/venia-ui/lib/components/Header/cartTrigger.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import React, { useEffect } from 'react';
import { func, number, shape, string } from 'prop-types';
import React from 'react';
import { shape, string } from 'prop-types';
import { ShoppingCart as ShoppingCartIcon } from 'react-feather';

import Icon from '../Icon';
import CartCounter from './cartCounter';

import { mergeClasses } from '../../classify';
import defaultClasses from './cartTrigger.css';
import { useCartTrigger } from '@magento/peregrine/lib/talons/Header/useCartTrigger';

const CART_ICON_FILLED = (
<Icon
Expand All @@ -27,42 +27,32 @@ const CART_ICON_EMPTY = (
);

const CartTrigger = props => {
const { cart, getCartDetails, toggleCart } = props;
const { details: cartDetails } = cart;
const { items_qty: numItems } = cartDetails;
const { handleClick, itemCount } = useCartTrigger();

const classes = mergeClasses(defaultClasses, props.classes);
const cartIcon = itemCount > 0 ? CART_ICON_FILLED : CART_ICON_EMPTY;
const buttonAriaLabel = `Toggle mini cart. You have ${itemCount} items in your cart.`;

useEffect(() => {
getCartDetails();
}, [getCartDetails]);

const cartIcon = numItems > 0 ? CART_ICON_FILLED : CART_ICON_EMPTY;
const buttonAriaLabel = `Toggle mini cart. You have ${numItems} items in your cart.`;
const itemCounter = itemCount ? (
<span className={classes.counter}>{itemCount}</span>
) : null;

return (
<button
className={classes.root}
aria-label={buttonAriaLabel}
onClick={toggleCart}
onClick={handleClick}
>
{cartIcon}
<CartCounter numItems={numItems} />
{itemCounter}
</button>
);
};

CartTrigger.propTypes = {
cart: shape({
details: shape({
items_qty: number
}).isRequired
}).isRequired,
classes: shape({
root: string
}),
getCartDetails: func.isRequired,
toggleCart: func
})
};

export default CartTrigger;
7 changes: 0 additions & 7 deletions packages/venia-ui/lib/components/Header/header.css
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,6 @@
padding: 0 1rem;
}

.navTrigger,
.cartTrigger {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unused classes.

composes: root from '../clickable.css';
height: 3rem;
width: 3rem;
}

.logo {
grid-area: title;
width: 3rem;
Expand Down
Loading