feat: add variant preview functionality with modal for image display in cutting and purchase management

This commit is contained in:
Yoga Pangestu 2026-08-20 21:51:38 +07:00
parent fbca04bce6
commit 6539d7dd2d
4 changed files with 81 additions and 27 deletions

View File

@ -101,6 +101,7 @@ export default function CuttingCreate({ rawMaterials }: Props) {
const [cartOpen, setCartOpen] = useState(false);
const [previewKey, setPreviewKey] = useState<string | null>(null);
const [variantPreview, setVariantPreview] = useState<{ src: string; title?: string; description?: string } | null>(null);
const [deleteConfirmOpen, setDeleteConfirmOpen] = useState(false);
const [deleteMaterialIndex, setDeleteMaterialIndex] = useState<number | null>(null);
@ -456,7 +457,7 @@ export default function CuttingCreate({ rawMaterials }: Props) {
<div key={price.id} className={`flex flex-wrap items-center justify-between gap-2 rounded-lg border p-3 ${isAdded ? 'border-primary' : ''}`}>
<div className="flex min-w-0 items-center gap-3">
{price.photo_conversion_url ?? price.photo_url ? (
<img src={price.photo_conversion_url ?? price.photo_url} alt={price.variant} className="h-10 w-10 shrink-0 rounded-md object-cover" />
<img src={price.photo_conversion_url ?? price.photo_url} alt={price.variant} className="h-10 w-10 shrink-0 cursor-pointer rounded-md object-cover" onClick={() => setVariantPreview({ src: price.photo_url, title: price.variant, description: rm.name })} />
) : (
<div className="flex h-10 w-10 shrink-0 items-center justify-center rounded-md bg-muted text-xs text-muted-foreground">N/A</div>
)}
@ -511,7 +512,7 @@ export default function CuttingCreate({ rawMaterials }: Props) {
<div key={price.id} className={`flex flex-wrap items-center justify-between gap-2 rounded-lg border p-3 ${isAdded ? 'border-primary' : ''}`}>
<div className="flex min-w-0 items-center gap-3">
{price.photo_conversion_url ?? price.photo_url ? (
<img src={price.photo_conversion_url ?? price.photo_url} alt={price.variant} className="h-10 w-10 shrink-0 rounded-md object-cover" />
<img src={price.photo_conversion_url ?? price.photo_url} alt={price.variant} className="h-10 w-10 shrink-0 cursor-pointer rounded-md object-cover" onClick={() => setVariantPreview({ src: price.photo_url, title: price.variant, description: selectedMaterial?.name })} />
) : (
<div className="flex h-10 w-10 shrink-0 items-center justify-center rounded-md bg-muted text-xs text-muted-foreground">N/A</div>
)}
@ -767,6 +768,18 @@ export default function CuttingCreate({ rawMaterials }: Props) {
sources={materials.filter((m) => m.photo_url).map((m) => m.photo_url!)}
/>
<ImagePreviewModal
open={variantPreview !== null}
onOpenChange={(open) => {
if (!open) {
setVariantPreview(null);
}
}}
src={variantPreview?.src ?? null}
title={variantPreview?.title}
description={variantPreview?.description}
/>
{comboDialogOpen && (
<div className="fixed inset-0 z-50 bg-black/50" onClick={() => setComboDialogOpen(false)} />
)}
@ -815,7 +828,7 @@ export default function CuttingCreate({ rawMaterials }: Props) {
<div key={item.priceId} className="flex items-center justify-between gap-2 rounded-md border border-primary bg-primary/5 px-3 py-2">
<div className="flex min-w-0 items-center gap-3">
{photoUrl ? (
<img src={photoUrl} alt={variantName} className="h-8 w-8 shrink-0 rounded-md object-cover" />
<img src={photoUrl} alt={variantName} className="h-8 w-8 shrink-0 cursor-pointer rounded-md object-cover" onClick={() => setVariantPreview({ src: price?.photo_url ?? null, title: variantName, description: item.materialName })} />
) : (
<div className="flex h-8 w-8 shrink-0 items-center justify-center rounded-md bg-muted text-xs text-muted-foreground">N/A</div>
)}
@ -853,8 +866,8 @@ export default function CuttingCreate({ rawMaterials }: Props) {
return (
<div key={price.id} className={`flex items-center justify-between gap-3 rounded-lg border p-3 ${isSelected ? 'border-primary' : ''}`}>
<div className="flex min-w-0 items-center gap-3">
{price.photo_conversion_url ?? price.photo_url ? (
<img src={price.photo_conversion_url ?? price.photo_url} alt={price.variant} className="h-10 w-10 shrink-0 rounded-md object-cover" />
{price.photo_conversion_url ?? price.photo_url ? (
<img src={price.photo_conversion_url ?? price.photo_url} alt={price.variant} className="h-10 w-10 shrink-0 cursor-pointer rounded-md object-cover" onClick={() => setVariantPreview({ src: price.photo_url, title: price.variant, description: comboMaterial.name })} />
) : (
<div className="flex h-10 w-10 shrink-0 items-center justify-center rounded-md bg-muted text-xs text-muted-foreground">N/A</div>
)}

View File

@ -104,6 +104,7 @@ export default function CuttingEdit({ cutting, rawMaterials }: Props) {
const [cartOpen, setCartOpen] = useState(false);
const [previewKey, setPreviewKey] = useState<string | null>(null);
const [variantPreview, setVariantPreview] = useState<{ src: string; title?: string; description?: string } | null>(null);
const [deleteConfirmOpen, setDeleteConfirmOpen] = useState(false);
const [deleteMaterialIndex, setDeleteMaterialIndex] = useState<number | null>(null);
@ -455,8 +456,8 @@ export default function CuttingEdit({ cutting, rawMaterials }: Props) {
return (
<div key={price.id} className={`flex flex-wrap items-center justify-between gap-2 rounded-lg border p-3 ${isAdded ? 'border-primary' : ''}`}>
<div className="flex min-w-0 items-center gap-3">
{price.photo_conversion_url ?? price.photo_url ? (
<img src={price.photo_conversion_url ?? price.photo_url} alt={price.variant} className="h-10 w-10 shrink-0 rounded-md object-cover" />
{price.photo_conversion_url ?? price.photo_url ? (
<img src={price.photo_conversion_url ?? price.photo_url} alt={price.variant} className="h-10 w-10 shrink-0 cursor-pointer rounded-md object-cover" onClick={() => setVariantPreview({ src: price.photo_url, title: price.variant, description: rm.name })} />
) : (
<div className="flex h-10 w-10 shrink-0 items-center justify-center rounded-md bg-muted text-xs text-muted-foreground">N/A</div>
)}
@ -502,7 +503,7 @@ export default function CuttingEdit({ cutting, rawMaterials }: Props) {
Tidak ada varian ditemukan.
</div>
) : (
<div className="space-y-2">
<div className="space-y-2">
{selectedMaterialVariants.map((price) => {
const isAdded = materials.some((m) => m.raw_material_price_id === price.id);
const addedCount = materials.filter((m) => m.raw_material_price_id === price.id).length;
@ -510,8 +511,8 @@ export default function CuttingEdit({ cutting, rawMaterials }: Props) {
return (
<div key={price.id} className={`flex flex-wrap items-center justify-between gap-2 rounded-lg border p-3 ${isAdded ? 'border-primary' : ''}`}>
<div className="flex min-w-0 items-center gap-3">
{price.photo_conversion_url ?? price.photo_url ? (
<img src={price.photo_conversion_url ?? price.photo_url} alt={price.variant} className="h-10 w-10 shrink-0 rounded-md object-cover" />
{price.photo_conversion_url ?? price.photo_url ? (
<img src={price.photo_conversion_url ?? price.photo_url} alt={price.variant} className="h-10 w-10 shrink-0 cursor-pointer rounded-md object-cover" onClick={() => setVariantPreview({ src: price.photo_url, title: price.variant, description: selectedMaterial?.name })} />
) : (
<div className="flex h-10 w-10 shrink-0 items-center justify-center rounded-md bg-muted text-xs text-muted-foreground">N/A</div>
)}
@ -528,7 +529,7 @@ export default function CuttingEdit({ cutting, rawMaterials }: Props) {
<Plus className="h-4 w-4" />
Tambah
</Button>
<Button type="button" variant="outline" size="sm" onClick={() => openComboDialog(selectedMaterial, price.id)}>
<Button type="button" variant="outline" size="sm" onClick={() => openComboDialog(selectedMaterial, price.id)}>
<Layers className="h-4 w-4" />
Kombinasi
</Button>
@ -767,6 +768,18 @@ export default function CuttingEdit({ cutting, rawMaterials }: Props) {
sources={materials.filter((m) => m.photo_url).map((m) => m.photo_url!)}
/>
<ImagePreviewModal
open={variantPreview !== null}
onOpenChange={(open) => {
if (!open) {
setVariantPreview(null);
}
}}
src={variantPreview?.src ?? null}
title={variantPreview?.title}
description={variantPreview?.description}
/>
{comboDialogOpen && (
<div className="fixed inset-0 z-50 bg-black/50" onClick={() => setComboDialogOpen(false)} />
)}
@ -815,7 +828,7 @@ export default function CuttingEdit({ cutting, rawMaterials }: Props) {
<div key={item.priceId} className="flex items-center justify-between gap-2 rounded-md border border-primary bg-primary/5 px-3 py-2">
<div className="flex min-w-0 items-center gap-3">
{photoUrl ? (
<img src={photoUrl} alt={variantName} className="h-8 w-8 shrink-0 rounded-md object-cover" />
<img src={photoUrl} alt={variantName} className="h-8 w-8 shrink-0 cursor-pointer rounded-md object-cover" onClick={() => setVariantPreview({ src: price?.photo_url ?? null, title: variantName, description: item.materialName })} />
) : (
<div className="flex h-8 w-8 shrink-0 items-center justify-center rounded-md bg-muted text-xs text-muted-foreground">N/A</div>
)}
@ -844,8 +857,8 @@ export default function CuttingEdit({ cutting, rawMaterials }: Props) {
return (
<div key={price.id} className={`flex items-center justify-between gap-3 rounded-lg border p-3 ${isSelected ? 'border-primary' : ''}`}>
<div className="flex min-w-0 items-center gap-3">
{price.photo_conversion_url ?? price.photo_url ? (
<img src={price.photo_conversion_url ?? price.photo_url} alt={price.variant} className="h-10 w-10 shrink-0 rounded-md object-cover" />
{price.photo_conversion_url ?? price.photo_url ? (
<img src={price.photo_conversion_url ?? price.photo_url} alt={price.variant} className="h-10 w-10 shrink-0 cursor-pointer rounded-md object-cover" onClick={() => setVariantPreview({ src: price.photo_url, title: price.variant, description: comboMaterial.name })} />
) : (
<div className="flex h-10 w-10 shrink-0 items-center justify-center rounded-md bg-muted text-xs text-muted-foreground">N/A</div>
)}

View File

@ -141,6 +141,7 @@ export default function PurchaseCreate({ suppliers, rawMaterials }: Props) {
);
const [cartOpen, setCartOpen] = useState(false);
const [previewKey, setPreviewKey] = useState<string | null>(null);
const [variantPreview, setVariantPreview] = useState<{ src: string; title?: string; description?: string } | null>(null);
const [cartRemoveKey, setCartRemoveKey] = useState<string | null>(null);
const draftData = useMemo(
@ -915,18 +916,19 @@ export default function PurchaseCreate({ suppliers, rawMaterials }: Props) {
: 'flex flex-wrap items-center justify-between gap-2 rounded-lg border p-3'
}
>
<div className="flex min-w-0 items-center gap-3">
{price.photo_conversion_url ?? price.photo_url ? (
<img
src={
price.photo_conversion_url ?? price.photo_url
}
alt={
price.variant
}
className="h-10 w-10 shrink-0 rounded-md object-cover"
/>
) : (
<div className="flex min-w-0 items-center gap-3">
{price.photo_conversion_url ?? price.photo_url ? (
<img
src={
price.photo_conversion_url ?? price.photo_url
}
alt={
price.variant
}
className="h-10 w-10 shrink-0 cursor-pointer rounded-md object-cover"
onClick={() => setVariantPreview({ src: price.photo_url, title: price.variant, description: selectedMaterial?.name })}
/>
) : (
<div className="flex h-10 w-10 shrink-0 items-center justify-center rounded-md bg-muted text-xs text-muted-foreground">
N/A
</div>
@ -1334,6 +1336,18 @@ export default function PurchaseCreate({ suppliers, rawMaterials }: Props) {
title={cartItems.find((i) => i.key === previewKey)?.title}
/>
<ImagePreviewModal
open={variantPreview !== null}
onOpenChange={(open) => {
if (!open) {
setVariantPreview(null);
}
}}
src={variantPreview?.src ?? null}
title={variantPreview?.title}
description={variantPreview?.description}
/>
<ConfirmDialog
open={deleteConfirmOpen}
onOpenChange={(open) => {

View File

@ -95,6 +95,7 @@ export default function PurchaseEdit({
);
const [cartOpen, setCartOpen] = useState(false);
const [previewKey, setPreviewKey] = useState<string | null>(null);
const [variantPreview, setVariantPreview] = useState<{ src: string; title?: string; description?: string } | null>(null);
const [cartRemoveKey, setCartRemoveKey] = useState<string | null>(null);
const [fetchedVariants, setFetchedVariants] = useState<Map<number, RawMaterialVariant[]>>(new Map());
@ -314,7 +315,8 @@ export default function PurchaseEdit({
<img
src={price.photo_conversion_url ?? price.photo_url!}
alt={price.variant}
className="h-10 w-10 shrink-0 rounded-md object-cover"
className="h-10 w-10 shrink-0 cursor-pointer rounded-md object-cover"
onClick={() => setVariantPreview({ src: price.photo_url ?? null, title: price.variant, description: group.name })}
/>
) : (
<div className="flex h-10 w-10 shrink-0 items-center justify-center rounded-md bg-muted text-xs text-muted-foreground">
@ -679,6 +681,18 @@ export default function PurchaseEdit({
title={cartItems.find((i) => i.key === previewKey)?.title}
/>
<ImagePreviewModal
open={variantPreview !== null}
onOpenChange={(open) => {
if (!open) {
setVariantPreview(null);
}
}}
src={variantPreview?.src ?? null}
title={variantPreview?.title}
description={variantPreview?.description}
/>
<ConfirmDialog
open={cartRemoveKey !== null}
onOpenChange={(open) => {