All files / src/components/features/loyalty/PointsEarningPreview index.tsx

20.77% Statements 16/77
100% Branches 0/0
0% Functions 0/1
20.77% Lines 16/77

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 781x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x                                                                                                                           1x 1x  
"use client";
 
import { Icon } from "@/components/ui/icons";
import { cn } from "@/lib/core";
 
interface PointsEarningPreviewProps {
  cartTotal: number;
  pointsPerDollar?: number;
  tierMultiplier?: number;
  bonusPoints?: number;
  bonusReason?: string;
  className?: string;
}
 
export function PointsEarningPreview({
  cartTotal,
  pointsPerDollar = 1,
  tierMultiplier = 1,
  bonusPoints = 0,
  bonusReason,
  className}: PointsEarningPreviewProps) {
  const basePoints = Math.floor(cartTotal * pointsPerDollar);
  const multipliedPoints = Math.floor(basePoints * tierMultiplier);
  const totalPoints = multipliedPoints + bonusPoints;

  const formatPoints = (points: number) => {
    return new Intl.NumberFormat("en-US").format(points);
  };

  if (totalPoints <= 0) {
    return null;
  }

  return (
    <div className={cn("p-3 bg-gradient-to-r from-yellow-50 to-orange-50 border border-yellow-200 rounded-lg", className)}>
      <div className="flex items-center justify-between">
        <div className="flex items-center gap-2">
          <div className="p-1.5 bg-yellow-100 rounded-full">
            <Icon name="star" size={16} className="text-yellow-600" />
          </div>
          <div>
            <p className="text-sm font-medium text-gray-800">
              Earn <span className="text-yellow-700 font-bold">{formatPoints(totalPoints)}</span> points
            </p>
            <p className="text-xs text-gray-500">on this purchase</p>
          </div>
        </div>
      </div>

      {/* Breakdown */}
      {(tierMultiplier > 1 || bonusPoints > 0) && (
        <div className="mt-2 pt-2 border-t border-yellow-200 space-y-1">
          <div className="flex justify-between text-xs text-gray-600">
            <span>Base points</span>
            <span>{formatPoints(basePoints)}</span>
          </div>

          {tierMultiplier > 1 && (
            <div className="flex justify-between text-xs text-yellow-700">
              <span>Tier bonus ({tierMultiplier}x)</span>
              <span>+{formatPoints(multipliedPoints - basePoints)}</span>
            </div>
          )}

          {bonusPoints > 0 && (
            <div className="flex justify-between text-xs text-orange-600">
              <span>{bonusReason || "Bonus points"}</span>
              <span>+{formatPoints(bonusPoints)}</span>
            </div>
          )}
        </div>
      )}
    </div>
  );
}
 
export default PointsEarningPreview;