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 | "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 { FeatureCard } from "@/app/admin/hero/page"; interface HeroFeatureCardManagerProps { card: FeatureCard | undefined; position: "top" | "bottom"; onAdd: () => void; onUpdate: (id: number, data: Partial<FeatureCard>) => Promise<void>; onDelete: (id: number) => Promise<void>; onEdit: (card: FeatureCard) => void; } export default function HeroFeatureCardManager({ card, position, onAdd, onUpdate, onDelete}: HeroFeatureCardManagerProps) { const [isEditing, setIsEditing] = useState(false); const [editValues, setEditValues] = useState<Partial<FeatureCard>>({}); const formatPrice = (price: number) => { return new Intl.NumberFormat("en-US", { style: "currency", currency: "USD"}).format(price); }; const handleStartEdit = () => { if (!card) return; setIsEditing(true); setEditValues({ title: card.title, subtitle: card.subtitle}); }; const handleSaveEdit = async () => { if (!card) return; await onUpdate(card.id, editValues); setIsEditing(false); setEditValues({}); }; const handleCancelEdit = () => { setIsEditing(false); setEditValues({}); }; const handleToggleActive = async () => { if (!card) return; await onUpdate(card.id, { isActive: !card.isActive }); }; // Empty state - no card assigned if (!card) { return ( <div className="border-2 border-dashed border-gray-300 dark:border-gray-600 rounded-lg p-6"> <div className="text-center"> <div className="mb-3"> <Badge variant="secondary" className="mb-2"> {position === "top" ? "Top Card" : "Bottom Card"} </Badge> </div> <Icon name="image" className="w-10 h-10 mx-auto text-gray-400 mb-3" /> <p className="text-sm text-gray-500 dark:text-gray-400 mb-4"> No product assigned to this slot </p> <Button size="sm" onClick={onAdd}> <Icon name="plus" className="w-4 h-4 mr-2" /> Select Product </Button> </div> </div> ); } // Card assigned return ( <div className={` border rounded-lg overflow-hidden ${card.isActive ? "border-gray-200 dark:border-gray-700" : "border-gray-300 dark:border-gray-600 opacity-60"} bg-white dark:bg-gray-800 `} > {/* Header */} <div className="flex items-center justify-between px-4 py-2 bg-gray-50 dark:bg-gray-900 border-b border-gray-200 dark:border-gray-700"> <Badge variant={card.isActive ? "default" : "secondary"}> {position === "top" ? "Top Card" : "Bottom Card"} </Badge> <div className="flex items-center gap-1"> <Button size="sm" variant="ghost" onClick={handleToggleActive} title={card.isActive ? "Deactivate" : "Activate"} > <Icon name={card.isActive ? "eye" : "eye-off"} className="w-4 h-4" /> </Button> <Button size="sm" variant="ghost" onClick={handleStartEdit} title="Edit" > <Icon name="edit" className="w-4 h-4" /> </Button> <Button size="sm" variant="ghost" onClick={() => onDelete(card.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> {/* Content */} <div className="p-4"> <div className="flex gap-4"> {/* Product Image */} <div className="flex-shrink-0 w-20 h-20 relative rounded overflow-hidden bg-gray-100 dark:bg-gray-700"> {card.product.images[0] ? ( <Image src={card.product.images[0].thumbnailUrl || card.product.images[0].url} alt={card.product.title} fill sizes="80px" 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> {/* Info */} <div className="flex-1 min-w-0"> {isEditing ? ( // Edit Mode <div className="space-y-2"> <input type="text" placeholder="Title (optional)" value={editValues.title || ""} onChange={(e) => setEditValues({ ...editValues, title: 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" /> <input type="text" placeholder="Subtitle" value={editValues.subtitle || ""} onChange={(e) => setEditValues({ ...editValues, subtitle: 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 mt-2"> <Button size="sm" onClick={handleSaveEdit}> Save </Button> <Button size="sm" variant="outline" onClick={handleCancelEdit}> Cancel </Button> </div> </div> ) : ( // View Mode <> <h4 className="font-medium text-gray-900 dark:text-white truncate"> {card.title || card.product.title} </h4> <p className="text-sm text-gray-500 dark:text-gray-400 mt-1"> {card.subtitle || "limited time offer"} </p> <div className="flex items-center gap-2 mt-2"> {card.product.discountedPrice < card.product.price ? ( <> <span className="text-blue-600 dark:text-blue-400 font-semibold"> {formatPrice(card.product.discountedPrice)} </span> <span className="text-sm text-gray-400 line-through"> {formatPrice(card.product.price)} </span> </> ) : ( <span className="text-blue-600 dark:text-blue-400 font-semibold"> {formatPrice(card.product.price)} </span> )} </div> </> )} </div> </div> </div> {/* Change Product */} {!isEditing && ( <div className="px-4 pb-4"> <Button size="sm" variant="outline" onClick={onAdd} className="w-full"> <Icon name="reload" className="w-4 h-4 mr-2" /> Change Product </Button> </div> )} </div> ); } |