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 [cartOpen, setCartOpen] = useState(false);
const [previewKey, setPreviewKey] = useState<string | null>(null); 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 [deleteConfirmOpen, setDeleteConfirmOpen] = useState(false);
const [deleteMaterialIndex, setDeleteMaterialIndex] = useState<number | null>(null); 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 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"> <div className="flex min-w-0 items-center gap-3">
{price.photo_conversion_url ?? price.photo_url ? ( {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> <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 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"> <div className="flex min-w-0 items-center gap-3">
{price.photo_conversion_url ?? price.photo_url ? ( {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> <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!)} 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 && ( {comboDialogOpen && (
<div className="fixed inset-0 z-50 bg-black/50" onClick={() => setComboDialogOpen(false)} /> <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 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"> <div className="flex min-w-0 items-center gap-3">
{photoUrl ? ( {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> <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 ( return (
<div key={price.id} className={`flex items-center justify-between gap-3 rounded-lg border p-3 ${isSelected ? 'border-primary' : ''}`}> <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"> <div className="flex min-w-0 items-center gap-3">
{price.photo_conversion_url ?? price.photo_url ? ( {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: 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> <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 [cartOpen, setCartOpen] = useState(false);
const [previewKey, setPreviewKey] = useState<string | null>(null); 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 [deleteConfirmOpen, setDeleteConfirmOpen] = useState(false);
const [deleteMaterialIndex, setDeleteMaterialIndex] = useState<number | null>(null); const [deleteMaterialIndex, setDeleteMaterialIndex] = useState<number | null>(null);
@ -455,8 +456,8 @@ export default function CuttingEdit({ cutting, rawMaterials }: Props) {
return ( return (
<div key={price.id} className={`flex flex-wrap items-center justify-between gap-2 rounded-lg border p-3 ${isAdded ? 'border-primary' : ''}`}> <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"> <div className="flex min-w-0 items-center gap-3">
{price.photo_conversion_url ?? price.photo_url ? ( {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> <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. Tidak ada varian ditemukan.
</div> </div>
) : ( ) : (
<div className="space-y-2"> <div className="space-y-2">
{selectedMaterialVariants.map((price) => { {selectedMaterialVariants.map((price) => {
const isAdded = materials.some((m) => m.raw_material_price_id === price.id); 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; 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 ( return (
<div key={price.id} className={`flex flex-wrap items-center justify-between gap-2 rounded-lg border p-3 ${isAdded ? 'border-primary' : ''}`}> <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"> <div className="flex min-w-0 items-center gap-3">
{price.photo_conversion_url ?? price.photo_url ? ( {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> <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" /> <Plus className="h-4 w-4" />
Tambah Tambah
</Button> </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" /> <Layers className="h-4 w-4" />
Kombinasi Kombinasi
</Button> </Button>
@ -767,6 +768,18 @@ export default function CuttingEdit({ cutting, rawMaterials }: Props) {
sources={materials.filter((m) => m.photo_url).map((m) => m.photo_url!)} 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 && ( {comboDialogOpen && (
<div className="fixed inset-0 z-50 bg-black/50" onClick={() => setComboDialogOpen(false)} /> <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 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"> <div className="flex min-w-0 items-center gap-3">
{photoUrl ? ( {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> <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 ( return (
<div key={price.id} className={`flex items-center justify-between gap-3 rounded-lg border p-3 ${isSelected ? 'border-primary' : ''}`}> <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"> <div className="flex min-w-0 items-center gap-3">
{price.photo_conversion_url ?? price.photo_url ? ( {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: 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> <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 [cartOpen, setCartOpen] = useState(false);
const [previewKey, setPreviewKey] = useState<string | null>(null); 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 [cartRemoveKey, setCartRemoveKey] = useState<string | null>(null);
const draftData = useMemo( 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' : 'flex flex-wrap items-center justify-between gap-2 rounded-lg border p-3'
} }
> >
<div className="flex min-w-0 items-center gap-3"> <div className="flex min-w-0 items-center gap-3">
{price.photo_conversion_url ?? price.photo_url ? ( {price.photo_conversion_url ?? price.photo_url ? (
<img <img
src={ src={
price.photo_conversion_url ?? price.photo_url price.photo_conversion_url ?? price.photo_url
} }
alt={ alt={
price.variant 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, 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"> <div className="flex h-10 w-10 shrink-0 items-center justify-center rounded-md bg-muted text-xs text-muted-foreground">
N/A N/A
</div> </div>
@ -1334,6 +1336,18 @@ export default function PurchaseCreate({ suppliers, rawMaterials }: Props) {
title={cartItems.find((i) => i.key === previewKey)?.title} 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 <ConfirmDialog
open={deleteConfirmOpen} open={deleteConfirmOpen}
onOpenChange={(open) => { onOpenChange={(open) => {

View File

@ -95,6 +95,7 @@ export default function PurchaseEdit({
); );
const [cartOpen, setCartOpen] = useState(false); const [cartOpen, setCartOpen] = useState(false);
const [previewKey, setPreviewKey] = useState<string | null>(null); 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 [cartRemoveKey, setCartRemoveKey] = useState<string | null>(null);
const [fetchedVariants, setFetchedVariants] = useState<Map<number, RawMaterialVariant[]>>(new Map()); const [fetchedVariants, setFetchedVariants] = useState<Map<number, RawMaterialVariant[]>>(new Map());
@ -314,7 +315,8 @@ export default function PurchaseEdit({
<img <img
src={price.photo_conversion_url ?? price.photo_url!} src={price.photo_conversion_url ?? price.photo_url!}
alt={price.variant} 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"> <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} 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 <ConfirmDialog
open={cartRemoveKey !== null} open={cartRemoveKey !== null}
onOpenChange={(open) => { onOpenChange={(open) => {