All files / src/components/features/admin/HeroCarouselManager index.tsx

0% Statements 0/264
100% Branches 0/0
0% Functions 0/1
0% Lines 0/264

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 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 
"use client";

import { useState } from "react";
import Image from "next/image";
import { Button, Badge } from "@/components/ui";
import { Icon } from "@/components/ui/icons";
import type { CarouselItem } from "@/app/admin/hero/page";

interface HeroCarouselManagerProps {
  items: CarouselItem[];
  onUpdate: (id: number, data: Partial<CarouselItem>) => Promise<void>;
  onDelete: (id: number) => Promise<void>;
  onReorder: (items: { id: number; order: number }[]) => Promise<void>;
  onEdit: (item: CarouselItem) => void;
}

export default function HeroCarouselManager({
  items,
  onUpdate,
  onDelete,
  onReorder,
  // onEdit is available for future use with a more detailed edit modal
}: HeroCarouselManagerProps) {
  const [draggedIndex, setDraggedIndex] = useState<number | null>(null);
  const [editingId, setEditingId] = useState<number | null>(null);
  const [editValues, setEditValues] = useState<Partial<CarouselItem>>({});

  const handleDragStart = (index: number) => {
    setDraggedIndex(index);
  };

  const handleDragOver = (e: React.DragEvent, index: number) => {
    e.preventDefault();
    if (draggedIndex === null || draggedIndex === index) return;

    // Reorder items locally for visual feedback
    const newItems = [...items];
    const draggedItem = newItems[draggedIndex];
    newItems.splice(draggedIndex, 1);
    newItems.splice(index, 0, draggedItem);

    // Update order values and call API
    const reorderedItems = newItems.map((item, idx) => ({
      id: item.id,
      order: idx}));

    onReorder(reorderedItems);
    setDraggedIndex(index);
  };

  const handleDragEnd = () => {
    setDraggedIndex(null);
  };

  const handleToggleActive = async (item: CarouselItem) => {
    await onUpdate(item.id, { isActive: !item.isActive });
  };

  const handleStartEdit = (item: CarouselItem) => {
    setEditingId(item.id);
    setEditValues({
      headline: item.headline,
      subheadline: item.subheadline,
      badgeText: item.badgeText,
      badgeSubtext: item.badgeSubtext,
      ctaText: item.ctaText});
  };

  const handleSaveEdit = async (id: number) => {
    await onUpdate(id, editValues);
    setEditingId(null);
    setEditValues({});
  };

  const handleCancelEdit = () => {
    setEditingId(null);
    setEditValues({});
  };

  const formatPrice = (price: number) => {
    return new Intl.NumberFormat("en-US", {
      style: "currency",
      currency: "USD"}).format(price);
  };

  const calculateDiscount = (price: number, discountedPrice: number) => {
    if (!discountedPrice || discountedPrice >= price) return null;
    return Math.round(((price - discountedPrice) / price) * 100);
  };

  if (items.length === 0) {
    return (
      <div className="border-2 border-dashed border-gray-300 dark:border-gray-600 rounded-lg p-8 text-center">
        <Icon name="image" className="w-12 h-12 mx-auto text-gray-400 mb-4" />
        <p className="text-gray-500 dark:text-gray-400">
          No carousel items yet. Add products to display in the hero carousel.
        </p>
      </div>
    );
  }

  return (
    <div className="space-y-3">
      {items.map((item, index) => (
        <div
          key={item.id}
          draggable
          onDragStart={() => handleDragStart(index)}
          onDragOver={(e) => handleDragOver(e, index)}
          onDragEnd={handleDragEnd}
          className={`
            flex items-center gap-4 p-4 bg-gray-50 dark:bg-gray-800 rounded-lg
            border border-gray-200 dark:border-gray-700
            ${draggedIndex === index ? "opacity-50" : ""}
            ${!item.isActive ? "opacity-60" : ""}
            cursor-grab active:cursor-grabbing
            transition-all duration-200
          `}
        >
          {/* Drag Handle */}
          <div className="flex-shrink-0 text-gray-400 dark:text-gray-500">
            <Icon name="grip-vertical" className="w-5 h-5" />
          </div>

          {/* Product Image */}
          <div className="flex-shrink-0 w-16 h-16 relative rounded overflow-hidden bg-gray-200 dark:bg-gray-700">
            {item.product.images[0] ? (
              <Image
                src={item.product.images[0].thumbnailUrl || item.product.images[0].url}
                alt={item.product.title}
                fill
                sizes="64px"
                className="object-cover"
              />
            ) : (
              <div className="w-full h-full flex items-center justify-center">
                <Icon name="image" className="w-6 h-6 text-gray-400" />
              </div>
            )}
          </div>

          {/* Content */}
          <div className="flex-1 min-w-0">
            {editingId === item.id ? (
              // Edit Mode
              <div className="space-y-2">
                <input
                  type="text"
                  placeholder="Headline (optional)"
                  value={editValues.headline || ""}
                  onChange={(e) => setEditValues({ ...editValues, headline: e.target.value })}
                  className="w-full px-3 py-1 text-sm border rounded dark:bg-gray-700 dark:border-gray-600 dark:text-white"
                />
                <div className="flex gap-2">
                  <input
                    type="text"
                    placeholder="Badge text (e.g., 30%)"
                    value={editValues.badgeText || ""}
                    onChange={(e) => setEditValues({ ...editValues, badgeText: e.target.value })}
                    className="flex-1 px-3 py-1 text-sm border rounded dark:bg-gray-700 dark:border-gray-600 dark:text-white"
                  />
                  <input
                    type="text"
                    placeholder="Badge subtext"
                    value={editValues.badgeSubtext || ""}
                    onChange={(e) => setEditValues({ ...editValues, badgeSubtext: e.target.value })}
                    className="flex-1 px-3 py-1 text-sm border rounded dark:bg-gray-700 dark:border-gray-600 dark:text-white"
                  />
                </div>
                <input
                  type="text"
                  placeholder="CTA text"
                  value={editValues.ctaText || "Shop Now"}
                  onChange={(e) => setEditValues({ ...editValues, ctaText: e.target.value })}
                  className="w-full px-3 py-1 text-sm border rounded dark:bg-gray-700 dark:border-gray-600 dark:text-white"
                />
              </div>
            ) : (
              // View Mode
              <>
                <div className="flex items-center gap-2 mb-1">
                  <h4 className="font-medium text-gray-900 dark:text-white truncate">
                    {item.headline || item.product.title}
                  </h4>
                  {!item.isActive && (
                    <Badge variant="secondary" className="text-xs">Inactive</Badge>
                  )}
                </div>
                <div className="flex items-center gap-3 text-sm text-gray-500 dark:text-gray-400">
                  {item.badgeText && (
                    <span className="text-blue-600 dark:text-blue-400 font-medium">
                      {item.badgeText} {item.badgeSubtext}
                    </span>
                  )}
                  {!item.badgeText && calculateDiscount(item.product.price, item.product.discountedPrice) && (
                    <span className="text-blue-600 dark:text-blue-400 font-medium">
                      {calculateDiscount(item.product.price, item.product.discountedPrice)}% Off
                    </span>
                  )}
                  <span>
                    {item.product.discountedPrice < item.product.price ? (
                      <>
                        <span className="text-green-600 dark:text-green-400">{formatPrice(item.product.discountedPrice)}</span>
                        <span className="ml-1 line-through text-gray-400">{formatPrice(item.product.price)}</span>
                      </>
                    ) : (
                      formatPrice(item.product.price)
                    )}
                  </span>
                </div>
              </>
            )}
          </div>

          {/* Actions */}
          <div className="flex-shrink-0 flex items-center gap-2">
            {editingId === item.id ? (
              <>
                <Button size="sm" onClick={() => handleSaveEdit(item.id)}>
                  Save
                </Button>
                <Button size="sm" variant="outline" onClick={handleCancelEdit}>
                  Cancel
                </Button>
              </>
            ) : (
              <>
                <Button
                  size="sm"
                  variant="ghost"
                  onClick={() => handleToggleActive(item)}
                  title={item.isActive ? "Deactivate" : "Activate"}
                >
                  <Icon name={item.isActive ? "eye" : "eye-off"} className="w-4 h-4" />
                </Button>
                <Button
                  size="sm"
                  variant="ghost"
                  onClick={() => handleStartEdit(item)}
                  title="Edit"
                >
                  <Icon name="edit" className="w-4 h-4" />
                </Button>
                <Button
                  size="sm"
                  variant="ghost"
                  onClick={() => onDelete(item.id)}
                  className="text-red-600 hover:text-red-700 dark:text-red-400"
                  title="Delete"
                >
                  <Icon name="trash" className="w-4 h-4" />
                </Button>
              </>
            )}
          </div>
        </div>
      ))}

      <p className="text-xs text-gray-500 dark:text-gray-400 text-center mt-4">
        Drag items to reorder. The first item will be displayed first in the carousel.
      </p>
    </div>
  );
}