2021-02-17 14:29:28 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import { __ } from '@wordpress/i18n';
|
|
|
|
import { Button } from '@wordpress/components';
|
|
|
|
import { NAVIGATION_STORE_NAME } from '@woocommerce/data';
|
|
|
|
import { recordEvent } from '@woocommerce/tracks';
|
|
|
|
import { useDispatch, useSelect } from '@wordpress/data';
|
2021-05-25 15:14:14 +00:00
|
|
|
import { Icon, starEmpty, starFilled } from '@wordpress/icons';
|
2021-02-17 14:29:28 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
|
|
|
import './style.scss';
|
|
|
|
|
|
|
|
export const FavoriteButton = ( { id } ) => {
|
|
|
|
const { favorites, isResolving } = useSelect( ( select ) => {
|
|
|
|
return {
|
|
|
|
favorites: select( NAVIGATION_STORE_NAME ).getFavorites(),
|
|
|
|
isResolving: select( NAVIGATION_STORE_NAME ).isResolving(
|
|
|
|
'getFavorites'
|
|
|
|
),
|
|
|
|
};
|
|
|
|
} );
|
|
|
|
|
|
|
|
const { addFavorite, removeFavorite } = useDispatch(
|
|
|
|
NAVIGATION_STORE_NAME
|
|
|
|
);
|
|
|
|
|
|
|
|
const isFavorited = favorites.includes( id );
|
|
|
|
|
|
|
|
const toggleFavorite = () => {
|
|
|
|
const toggle = isFavorited ? removeFavorite : addFavorite;
|
|
|
|
toggle( id );
|
|
|
|
recordEvent( 'navigation_favorite', {
|
|
|
|
id,
|
|
|
|
action: isFavorited ? 'unfavorite' : 'favorite',
|
|
|
|
} );
|
|
|
|
};
|
|
|
|
|
|
|
|
if ( isResolving ) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<Button
|
|
|
|
id="woocommerce-navigation-favorite-button"
|
|
|
|
className="woocommerce-navigation-favorite-button"
|
|
|
|
isTertiary
|
|
|
|
onClick={ toggleFavorite }
|
|
|
|
aria-label={
|
|
|
|
isFavorited
|
2022-03-30 09:00:04 +00:00
|
|
|
? __( 'Add this item to your favorites.', 'woocommerce' )
|
2021-02-17 14:29:28 +00:00
|
|
|
: __(
|
|
|
|
'Remove this item from your favorites.',
|
2022-03-30 09:00:04 +00:00
|
|
|
'woocommerce'
|
2021-02-17 14:29:28 +00:00
|
|
|
)
|
|
|
|
}
|
2021-05-25 15:14:14 +00:00
|
|
|
>
|
|
|
|
<Icon
|
|
|
|
icon={ isFavorited ? starFilled : starEmpty }
|
|
|
|
className={ `star-${ isFavorited ? 'filled' : 'empty' }-icon` }
|
|
|
|
/>
|
|
|
|
</Button>
|
2021-02-17 14:29:28 +00:00
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default FavoriteButton;
|