All files / src/components/features/product/ProductActionButtons index.tsx

100% Statements 216/216
100% Branches 19/19
100% Functions 4/4
100% Lines 216/216

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 2171x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 42x 42x 42x 42x 42x 42x 42x 42x 42x 42x 42x 42x 42x 42x 42x 42x 42x 42x 42x 42x 42x 42x 42x 34x 34x 34x 96x 96x 96x 96x 96x 96x 96x 96x 96x 34x 34x 34x 34x 8x 8x 42x 4x 4x 4x 12x 12x 12x 12x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 4x 4x 4x 4x 4x 4x 42x 3x 3x 3x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 3x 3x 3x 3x 1x 1x 1x 1x 1x 1x 1x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 1x 1x 1x 1x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x  
'use client';
 
import { Product } from '@/types/product';
import { IconButton, Button } from '@/components/ui';
import { useProductActions } from '@/hooks/useProductActions';
import { cn } from '@/lib/core';
 
/**
 * ProductActionButtons Component Props
 */
export interface ProductActionButtonsProps {
  /** Product data */
  product: Product;
  /** Actions to display */
  actions?: ('quickView' | 'addToCart' | 'wishlist' | 'compare')[];
  /** Layout direction */
  layout?: 'horizontal' | 'vertical';
  /** Icon-only mode (no text labels) */
  iconOnly?: boolean;
  /** Button variant style */
  variant?: 'icon' | 'button' | 'text';
  /** Additional CSS classes */
  className?: string;
}
 
/**
 * Action configuration
 */
const actionConfig = {
  quickView: {
    icon: 'eye' as const,
    label: 'Quick View',
    ariaLabel: 'View product details in quick view modal'},
  addToCart: {
    icon: 'cart' as const,
    label: 'Add to Cart',
    ariaLabel: 'Add product to shopping cart'},
  wishlist: {
    icon: 'heart' as const,
    label: 'Wishlist',
    ariaLabel: 'Add product to wishlist'},
  compare: {
    icon: 'arrows-left-right' as const,
    label: 'Compare',
    ariaLabel: 'Add product to comparison'}};
 
/**
 * ProductActionButtons - Reusable product action button group
 *
 * Features:
 * - Configurable actions (quick view, add to cart, wishlist, compare)
 * - Multiple layout options (horizontal, vertical)
 * - Icon-only or with labels
 * - Uses useProductActions hook for consistency
 * - Accessible with aria-labels
 *
 * @example
 * ```tsx
 * // Icon buttons (default)
 * <ProductActionButtons
 *   product={product}
 *   actions={['quickView', 'addToCart', 'wishlist']}
 * />
 *
 * // Vertical layout
 * <ProductActionButtons
 *   product={product}
 *   layout="vertical"
 * />
 *
 * // With text labels
 * <ProductActionButtons
 *   product={product}
 *   iconOnly={false}
 * />
 *
 * // Custom actions
 * <ProductActionButtons
 *   product={product}
 *   actions={['addToCart', 'wishlist']}
 * />
 * ```
 */
export function ProductActionButtons({
  product,
  actions = ['quickView', 'addToCart', 'wishlist'],
  layout = 'horizontal',
  iconOnly = true,
  variant = 'icon',
  className}: ProductActionButtonsProps) {
  const { addToCart, addToWishlist, showQuickView, addToCompare } = useProductActions(product);
 
  const actionHandlers = {
    quickView: showQuickView,
    addToCart: () => addToCart(),
    wishlist: addToWishlist,
    compare: addToCompare};
 
  const containerClassName = cn(
    'flex items-center',
    layout === 'horizontal' ? 'flex-row gap-2.5' : 'flex-col gap-2',
    className
  );
 
  // Icon-only variant (default)
  if (variant === 'icon' && iconOnly) {
    return (
      <div className={containerClassName}>
        {actions.map((action) => {
          const config = actionConfig[action];
          return (
            <IconButton
              key={action}
              icon={config.icon}
              onClick={actionHandlers[action]}
              aria-label={config.ariaLabel}
            />
          );
        })}
      </div>
    );
  }
 
  // Button variant with special handling for add to cart
  if (variant === 'button') {
    return (
      <div className={containerClassName}>
        {actions.map((action) => {
          const config = actionConfig[action];
 
          // Add to cart gets a special primary button style
          if (action === 'addToCart') {
            return (
              <Button
                key={action}
                onClick={actionHandlers[action]}
                variant="primary"
                size="sm"
                aria-label={config.ariaLabel}
              >
                {config.label}
              </Button>
            );
          }
 
          // Other actions use icon buttons
          return (
            <IconButton
              key={action}
              icon={config.icon}
              onClick={actionHandlers[action]}
              aria-label={config.ariaLabel}
            />
          );
        })}
      </div>
    );
  }
 
  // Text variant (buttons with labels)
  if (variant === 'text') {
    return (
      <div className={containerClassName}>
        {actions.map((action) => {
          const config = actionConfig[action];
          return (
            <Button
              key={action}
              onClick={actionHandlers[action]}
              variant={action === 'addToCart' ? 'primary' : 'secondary'}
              size="sm"
              aria-label={config.ariaLabel}
              leftIcon={<span className="text-lg">{config.icon}</span>}
            >
              {config.label}
            </Button>
          );
        })}
      </div>
    );
  }
 
  return null;
}
 
/**
 * Quick product actions (icon buttons)
 */
export function QuickProductActions({
  product,
  className}: Pick<ProductActionButtonsProps, 'product' | 'className'>) {
  return (
    <ProductActionButtons
      product={product}
      actions={['quickView', 'addToCart', 'wishlist']}
      variant="icon"
      className={className}
    />
  );
}
 
/**
 * Product actions with add to cart button
 */
export function ProductActionsWithCart({
  product,
  className}: Pick<ProductActionButtonsProps, 'product' | 'className'>) {
  return (
    <ProductActionButtons
      product={product}
      actions={['quickView', 'addToCart', 'wishlist']}
      variant="button"
      className={className}
    />
  );
}