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

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

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 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   
"use client";

import { useState } from "react";
import Image from "next/image";
import { Button, Badge } from "@/components/ui";
import { Icon } from "@/components/ui/icons";
import PromoBannerEditor, { type PromoBannerData } from "../PromoBannerEditor";

export interface PromoBanner {
  id: number;
  title: string;
  headline: string;
  description: string | null;
  ctaText: string;
  productId: number | null;
  ctaLink: string | null;
  imageUrl: string;
  backgroundColor: string;
  textColor: string;
  darkBgColor: string | null;
  darkTextColor: string | null;
  size: "large" | "small";
  imagePosition: "left" | "right";
  order: number;
  isActive: boolean;
  startDate: string | null;
  endDate: string | null;
  product: {
    id: number;
    title: string;
    price: number;
    discountedPrice: number;
    images: { id: number; url: string; thumbnailUrl: string | null }[];
  } | null;
}

interface Product {
  id: number;
  title: string;
  price: number;
  discountedPrice: number;
  images: { id: number; url: string; thumbnailUrl: string | null }[];
}

interface PromoBannerManagerProps {
  banners: PromoBanner[];
  products: Product[];
  onAdd: (data: Omit<PromoBannerData, "id" | "product">) => Promise<void>;
  onUpdate: (id: number, data: Partial<PromoBanner>) => Promise<void>;
  onDelete: (id: number) => Promise<void>;
  onReorder: (items: { id: number; order: number }[]) => Promise<void>;
}

export default function PromoBannerManager({
  banners,
  products,
  onAdd,
  onUpdate,
  onDelete,
  onReorder,
}: PromoBannerManagerProps) {
  const [draggedIndex, setDraggedIndex] = useState<number | null>(null);
  const [deletingId, setDeletingId] = useState<number | null>(null);
  const [editingBanner, setEditingBanner] = useState<PromoBanner | null>(null);
  const [showEditor, setShowEditor] = useState(false);

  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 = [...banners];
    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 (banner: PromoBanner) => {
    await onUpdate(banner.id, { isActive: !banner.isActive });
  };

  const handleEdit = (banner: PromoBanner) => {
    setEditingBanner(banner);
    setShowEditor(true);
  };

  const handleAdd = () => {
    setEditingBanner(null);
    setShowEditor(true);
  };

  const handleSave = async (data: Omit<PromoBannerData, "id" | "product">) => {
    if (editingBanner) {
      await onUpdate(editingBanner.id, data);
    } else {
      await onAdd(data);
    }
    setShowEditor(false);
    setEditingBanner(null);
  };

  const handleDelete = async (id: number) => {
    if (deletingId === id) {
      await onDelete(id);
      setDeletingId(null);
    } else {
      setDeletingId(id);
      // Auto-reset after 3 seconds
      setTimeout(() => setDeletingId(null), 3000);
    }
  };

  const handleCloseEditor = () => {
    setShowEditor(false);
    setEditingBanner(null);
  };

  return (
    <div className="space-y-4">
      {/* Add Button */}
      <div className="flex justify-end">
        <Button onClick={handleAdd}>
          <Icon name="plus" className="w-4 h-4 mr-2" />
          Add Promo Banner
        </Button>
      </div>

      {/* Banner List */}
      {banners.length === 0 ? (
        <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 promo banners yet. Add banners to display promotional content.
          </p>
        </div>
      ) : (
        <div className="space-y-3">
          {banners.map((banner, index) => (
            <div
              key={banner.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" : ""}
                ${!banner.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>

              {/* Preview Thumbnail */}
              <div
                className="flex-shrink-0 w-20 h-16 relative rounded overflow-hidden flex items-center justify-center"
                style={{ backgroundColor: banner.backgroundColor }}
              >
                {banner.imageUrl ? (
                  <Image
                    src={banner.imageUrl}
                    alt={banner.title}
                    fill
                    sizes="80px"
                    className="object-contain"
                  />
                ) : (
                  <Icon name="image" className="w-6 h-6 text-gray-400" />
                )}
              </div>

              {/* Content */}
              <div className="flex-1 min-w-0">
                <div className="flex items-center gap-2 mb-1">
                  <h4 className="font-medium text-gray-900 dark:text-white truncate">
                    {banner.title}
                  </h4>
                  <Badge
                    variant={banner.size === "large" ? "default" : "secondary"}
                    className="text-xs"
                  >
                    {banner.size === "large" ? "Large" : "Small"}
                  </Badge>
                  {!banner.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">
                  <span className="text-blue-600 dark:text-blue-400 font-medium">
                    {banner.headline}
                  </span>
                  <span className="text-gray-400">|</span>
                  {banner.product ? (
                    <span>Links to: {banner.product.title}</span>
                  ) : banner.ctaLink ? (
                    <span>Links to: {banner.ctaLink}</span>
                  ) : null}
                </div>
              </div>

              {/* Color Indicators */}
              <div className="flex-shrink-0 flex items-center gap-1">
                <div
                  className="w-5 h-5 rounded border border-gray-300 dark:border-gray-600"
                  style={{ backgroundColor: banner.backgroundColor }}
                  title={`Background: ${banner.backgroundColor}`}
                />
                <div
                  className="w-5 h-5 rounded border border-gray-300 dark:border-gray-600"
                  style={{ backgroundColor: banner.textColor }}
                  title={`Text: ${banner.textColor}`}
                />
              </div>

              {/* Actions */}
              <div className="flex-shrink-0 flex items-center gap-2">
                <Button
                  size="sm"
                  variant="ghost"
                  onClick={() => handleToggleActive(banner)}
                  title={banner.isActive ? "Deactivate" : "Activate"}
                >
                  <Icon
                    name={banner.isActive ? "eye" : "eye-off"}
                    className="w-4 h-4"
                  />
                </Button>
                <Button
                  size="sm"
                  variant="ghost"
                  onClick={() => handleEdit(banner)}
                  title="Edit"
                >
                  <Icon name="edit" className="w-4 h-4" />
                </Button>
                <Button
                  size="sm"
                  variant="ghost"
                  onClick={() => handleDelete(banner.id)}
                  className={
                    deletingId === banner.id
                      ? "text-white bg-red-600 hover:bg-red-700"
                      : "text-red-600 hover:text-red-700 dark:text-red-400"
                  }
                  title={deletingId === banner.id ? "Click again to confirm" : "Delete"}
                >
                  <Icon name="trash" className="w-4 h-4" />
                  {deletingId === banner.id && (
                    <span className="ml-1 text-xs">Confirm?</span>
                  )}
                </Button>
              </div>
            </div>
          ))}

          <p className="text-xs text-gray-500 dark:text-gray-400 text-center mt-4">
            Drag items to reorder. Large banners display full width, small banners display side by side.
          </p>
        </div>
      )}

      {/* Editor Modal */}
      {showEditor && (
        <PromoBannerEditor
          banner={editingBanner ? { ...editingBanner } : undefined}
          products={products}
          onSave={handleSave}
          onClose={handleCloseEditor}
        />
      )}
    </div>
  );
}