feat: refactor combo selection to use structured item objects for improved clarity and functionality

This commit is contained in:
Yoga Pangestu 2026-08-20 20:40:29 +07:00
parent 06b9c91948
commit cc112ef253
2 changed files with 80 additions and 69 deletions

View File

@ -111,7 +111,7 @@ export default function CuttingCreate({ rawMaterials }: Props) {
const [comboDialogOpen, setComboDialogOpen] = useState(false); const [comboDialogOpen, setComboDialogOpen] = useState(false);
const [comboMaterialName, setComboMaterialName] = useState(''); const [comboMaterialName, setComboMaterialName] = useState('');
const [comboSelectedPriceIds, setComboSelectedPriceIds] = useState<number[]>([]); const [comboSelectedItems, setComboSelectedItems] = useState<{ materialName: string; materialId: number; priceId: number }[]>([]);
const [comboResult, setComboResult] = useState(0); const [comboResult, setComboResult] = useState(0);
const comboMaterial = useMemo( const comboMaterial = useMemo(
@ -260,40 +260,48 @@ export default function CuttingCreate({ rawMaterials }: Props) {
[fetchedVariants], [fetchedVariants],
); );
const openComboDialog = useCallback((materialName: string, preSelectPriceId?: number) => { const openComboDialog = useCallback((material: { id: number; name: string }, preSelectPriceId?: number) => {
setComboMaterialName(materialName); setComboMaterialName(material.name);
setComboSelectedPriceIds(preSelectPriceId ? [preSelectPriceId] : []); if (preSelectPriceId) {
setComboSelectedItems([{ materialName: material.name, materialId: material.id, priceId: preSelectPriceId }]);
} else {
setComboSelectedItems([]);
}
setComboResult(0); setComboResult(0);
setComboDialogOpen(true); setComboDialogOpen(true);
}, []); }, []);
const toggleComboPrice = useCallback((priceId: number) => { const toggleComboPrice = useCallback((priceId: number) => {
setComboSelectedPriceIds((prev) => const material = comboMaterial;
prev.includes(priceId) ? prev.filter((id) => id !== priceId) : [...prev, priceId], if (!material) return;
); setComboSelectedItems((prev) => {
}, []); const existing = prev.find((item) => item.priceId === priceId);
if (existing) {
return prev.filter((item) => item.priceId !== priceId);
}
return [...prev, { materialName: material.name, materialId: material.id, priceId }];
});
}, [comboMaterial]);
const confirmCombo = useCallback(() => { const confirmCombo = useCallback(() => {
if (comboSelectedPriceIds.length < 2) { if (comboSelectedItems.length < 2) {
return; return;
} }
const comboIndex = combinations.length; const comboIndex = combinations.length;
const comboMaterialData = rawMaterials.find((m) => m.name === comboMaterialName); const newMaterials: MaterialState[] = comboSelectedItems.map((item) => {
const variants = comboMaterialData ? (fetchedVariants.get(comboMaterialData.id) ?? []) : []; const variants = fetchedVariants.get(item.materialId) ?? [];
const price = variants.find((p) => p.id === item.priceId);
const newMaterials: MaterialState[] = comboSelectedPriceIds.map((priceId) => {
const price = variants.find((p) => p.id === priceId);
return { return {
raw_material_price_id: priceId, raw_material_price_id: item.priceId,
material_usage: '0', material_usage: '0',
material_result: 0, material_result: 0,
combination_id: comboIndex, combination_id: comboIndex,
variant: price?.variant ?? '', variant: price?.variant ?? '',
material_name: comboMaterialData?.name ?? '', material_name: item.materialName,
unit: comboMaterialData?.unit ?? '', unit: rawMaterials.find((m) => m.id === item.materialId)?.unit ?? '',
photo_url: price?.photo_url ?? null, photo_url: price?.photo_url ?? null,
photo_conversion_url: price?.photo_conversion_url ?? null, photo_conversion_url: price?.photo_conversion_url ?? null,
}; };
@ -309,9 +317,9 @@ export default function CuttingCreate({ rawMaterials }: Props) {
setComboDialogOpen(false); setComboDialogOpen(false);
setComboMaterialName(''); setComboMaterialName('');
setComboSelectedPriceIds([]); setComboSelectedItems([]);
setComboResult(0); setComboResult(0);
}, [comboSelectedPriceIds, comboResult, rawMaterials, fetchedVariants, comboMaterialName, combinations.length]); }, [comboSelectedItems, comboResult, rawMaterials, fetchedVariants, combinations.length]);
const removeMaterial = useCallback((index: number) => { const removeMaterial = useCallback((index: number) => {
setDeleteMaterialIndex(index); setDeleteMaterialIndex(index);
@ -465,7 +473,7 @@ export default function CuttingCreate({ 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(rm.name, price.id)}> <Button type="button" variant="outline" size="sm" onClick={() => openComboDialog(rm, price.id)}>
<Layers className="h-4 w-4" /> <Layers className="h-4 w-4" />
Kombinasi Kombinasi
</Button> </Button>
@ -520,7 +528,7 @@ export default function CuttingCreate({ 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.name, 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>
@ -793,23 +801,18 @@ export default function CuttingCreate({ rawMaterials }: Props) {
</Combobox> </Combobox>
</div> </div>
{comboSelectedPriceIds.length > 0 && ( {comboSelectedItems.length > 0 && (
<div className="space-y-2"> <div className="space-y-2">
<Label className="text-sm font-medium">Varian Dipilih</Label> <Label className="text-sm font-medium">Varian Dipilih</Label>
<div className="space-y-1"> <div className="space-y-1">
{comboSelectedPriceIds.map((priceId) => { {comboSelectedItems.map((item) => {
let variantName = ''; const variants = fetchedVariants.get(item.materialId) ?? [];
let materialName = comboMaterialName; const price = variants.find((pp) => pp.id === item.priceId);
let photoUrl: string | null = null; const variantName = price?.variant ?? '';
const photoUrl = price?.photo_conversion_url ?? price?.photo_url ?? null;
const price = comboMaterialVariants.find((pp) => pp.id === priceId);
if (price) {
variantName = price.variant;
photoUrl = price.photo_conversion_url ?? price.photo_url;
}
return ( return (
<div key={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 rounded-md object-cover" />
@ -818,10 +821,10 @@ export default function CuttingCreate({ rawMaterials }: Props) {
)} )}
<div className="min-w-0"> <div className="min-w-0">
<p className="text-sm font-medium truncate">{variantName}</p> <p className="text-sm font-medium truncate">{variantName}</p>
<p className="text-xs text-muted-foreground">{materialName}</p> <p className="text-xs text-muted-foreground">{item.materialName}</p>
</div> </div>
</div> </div>
<Button type="button" variant="ghost" size="icon-sm" onClick={() => toggleComboPrice(priceId)}> <Button type="button" variant="ghost" size="icon-sm" onClick={() => toggleComboPrice(item.priceId)}>
<Trash2 className="h-3 w-3 text-destructive" /> <Trash2 className="h-3 w-3 text-destructive" />
</Button> </Button>
</div> </div>
@ -845,7 +848,7 @@ export default function CuttingCreate({ rawMaterials }: Props) {
) : ( ) : (
<div className="space-y-2"> <div className="space-y-2">
{comboMaterialVariants.map((price) => { {comboMaterialVariants.map((price) => {
const isSelected = comboSelectedPriceIds.includes(price.id); const isSelected = comboSelectedItems.some((item) => item.priceId === price.id);
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' : ''}`}>
@ -873,7 +876,7 @@ export default function CuttingCreate({ rawMaterials }: Props) {
</div> </div>
<DialogFooter> <DialogFooter>
<Button type="button" variant="outline" onClick={() => setComboDialogOpen(false)}>Batal</Button> <Button type="button" variant="outline" onClick={() => setComboDialogOpen(false)}>Batal</Button>
<Button type="button" onClick={confirmCombo} disabled={comboSelectedPriceIds.length < 2}> <Button type="button" onClick={confirmCombo} disabled={comboSelectedItems.length < 2}>
Konfirmasi Konfirmasi
</Button> </Button>
</DialogFooter> </DialogFooter>

View File

@ -114,7 +114,7 @@ export default function CuttingEdit({ cutting, rawMaterials }: Props) {
const [comboDialogOpen, setComboDialogOpen] = useState(false); const [comboDialogOpen, setComboDialogOpen] = useState(false);
const [comboMaterialName, setComboMaterialName] = useState(''); const [comboMaterialName, setComboMaterialName] = useState('');
const [comboSelectedPriceIds, setComboSelectedPriceIds] = useState<number[]>([]); const [comboSelectedItems, setComboSelectedItems] = useState<{ materialName: string; materialId: number; priceId: number }[]>([]);
const [comboResult, setComboResult] = useState(0); const [comboResult, setComboResult] = useState(0);
const comboMaterial = useMemo( const comboMaterial = useMemo(
@ -265,40 +265,48 @@ export default function CuttingEdit({ cutting, rawMaterials }: Props) {
[], [],
); );
const openComboDialog = useCallback((materialName: string, preSelectPriceId?: number) => { const openComboDialog = useCallback((material: { id: number; name: string }, preSelectPriceId?: number) => {
setComboMaterialName(materialName); setComboMaterialName(material.name);
setComboSelectedPriceIds(preSelectPriceId ? [preSelectPriceId] : []); if (preSelectPriceId) {
setComboSelectedItems([{ materialName: material.name, materialId: material.id, priceId: preSelectPriceId }]);
} else {
setComboSelectedItems([]);
}
setComboResult(0); setComboResult(0);
setComboDialogOpen(true); setComboDialogOpen(true);
}, []); }, []);
const toggleComboPrice = useCallback((priceId: number) => { const toggleComboPrice = useCallback((priceId: number) => {
setComboSelectedPriceIds((prev) => const material = comboMaterial;
prev.includes(priceId) ? prev.filter((id) => id !== priceId) : [...prev, priceId], if (!material) return;
); setComboSelectedItems((prev) => {
}, []); const existing = prev.find((item) => item.priceId === priceId);
if (existing) {
return prev.filter((item) => item.priceId !== priceId);
}
return [...prev, { materialName: material.name, materialId: material.id, priceId }];
});
}, [comboMaterial]);
const confirmCombo = useCallback(() => { const confirmCombo = useCallback(() => {
if (comboSelectedPriceIds.length < 2) { if (comboSelectedItems.length < 2) {
return; return;
} }
const comboIndex = combinations.length; const comboIndex = combinations.length;
const comboMaterialData = rawMaterials.find((m) => m.name === comboMaterialName); const newMaterials: MaterialState[] = comboSelectedItems.map((item) => {
const variants = comboMaterialData ? (fetchedVariants.get(comboMaterialData.id) ?? []) : []; const variants = fetchedVariants.get(item.materialId) ?? [];
const price = variants.find((p) => p.id === item.priceId);
const newMaterials: MaterialState[] = comboSelectedPriceIds.map((priceId) => {
const price = variants.find((p) => p.id === priceId);
return { return {
raw_material_price_id: priceId, raw_material_price_id: item.priceId,
material_usage: '0', material_usage: '0',
material_result: 0, material_result: 0,
combination_id: comboIndex, combination_id: comboIndex,
variant: price?.variant ?? '', variant: price?.variant ?? '',
material_name: comboMaterialData?.name ?? '', material_name: item.materialName,
unit: comboMaterialData?.unit ?? '', unit: rawMaterials.find((m) => m.id === item.materialId)?.unit ?? '',
photo_url: price?.photo_url ?? null, photo_url: price?.photo_url ?? null,
photo_conversion_url: price?.photo_conversion_url ?? null, photo_conversion_url: price?.photo_conversion_url ?? null,
}; };
@ -314,9 +322,9 @@ export default function CuttingEdit({ cutting, rawMaterials }: Props) {
setComboDialogOpen(false); setComboDialogOpen(false);
setComboMaterialName(''); setComboMaterialName('');
setComboSelectedPriceIds([]); setComboSelectedItems([]);
setComboResult(0); setComboResult(0);
}, [comboSelectedPriceIds, comboResult, rawMaterials, fetchedVariants, comboMaterialName, combinations.length]); }, [comboSelectedItems, comboResult, rawMaterials, fetchedVariants, combinations.length]);
const updateMaterial = useCallback( const updateMaterial = useCallback(
(index: number, field: keyof MaterialState, value: unknown) => { (index: number, field: keyof MaterialState, value: unknown) => {
@ -465,7 +473,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(rm.name, price.id)}> <Button type="button" variant="outline" size="sm" onClick={() => openComboDialog(rm, price.id)}>
<Layers className="h-4 w-4" /> <Layers className="h-4 w-4" />
Kombinasi Kombinasi
</Button> </Button>
@ -520,7 +528,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.name, 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>
@ -793,18 +801,18 @@ export default function CuttingEdit({ cutting, rawMaterials }: Props) {
</Combobox> </Combobox>
</div> </div>
{comboSelectedPriceIds.length > 0 && ( {comboSelectedItems.length > 0 && (
<div className="space-y-2"> <div className="space-y-2">
<Label className="text-sm font-medium">Varian Dipilih</Label> <Label className="text-sm font-medium">Varian Dipilih</Label>
<div className="space-y-1"> <div className="space-y-1">
{comboSelectedPriceIds.map((priceId) => { {comboSelectedItems.map((item) => {
const p = comboMaterialVariants.find((pp) => pp.id === priceId); const variants = fetchedVariants.get(item.materialId) ?? [];
const variantName = p?.variant ?? ''; const price = variants.find((pp) => pp.id === item.priceId);
const materialName = comboMaterialName; const variantName = price?.variant ?? '';
const photoUrl = p?.photo_conversion_url ?? p?.photo_url ?? null; const photoUrl = price?.photo_conversion_url ?? price?.photo_url ?? null;
return ( return (
<div key={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 rounded-md object-cover" />
@ -813,10 +821,10 @@ export default function CuttingEdit({ cutting, rawMaterials }: Props) {
)} )}
<div className="min-w-0"> <div className="min-w-0">
<p className="text-sm font-medium truncate">{variantName}</p> <p className="text-sm font-medium truncate">{variantName}</p>
<p className="text-xs text-muted-foreground">{materialName}</p> <p className="text-xs text-muted-foreground">{item.materialName}</p>
</div> </div>
</div> </div>
<Button type="button" variant="ghost" size="icon-sm" onClick={() => toggleComboPrice(priceId)}> <Button type="button" variant="ghost" size="icon-sm" onClick={() => toggleComboPrice(item.priceId)}>
<Trash2 className="h-3 w-3 text-destructive" /> <Trash2 className="h-3 w-3 text-destructive" />
</Button> </Button>
</div> </div>
@ -831,7 +839,7 @@ export default function CuttingEdit({ cutting, rawMaterials }: Props) {
<Label className="text-sm font-medium">Pilih Varian</Label> <Label className="text-sm font-medium">Pilih Varian</Label>
<div className="space-y-2"> <div className="space-y-2">
{comboMaterialVariants.map((price) => { {comboMaterialVariants.map((price) => {
const isSelected = comboSelectedPriceIds.includes(price.id); const isSelected = comboSelectedItems.some((item) => item.priceId === price.id);
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' : ''}`}>
@ -858,7 +866,7 @@ export default function CuttingEdit({ cutting, rawMaterials }: Props) {
</div> </div>
<DialogFooter> <DialogFooter>
<Button type="button" variant="outline" onClick={() => setComboDialogOpen(false)}>Batal</Button> <Button type="button" variant="outline" onClick={() => setComboDialogOpen(false)}>Batal</Button>
<Button type="button" onClick={confirmCombo} disabled={comboSelectedPriceIds.length < 2}> <Button type="button" onClick={confirmCombo} disabled={comboSelectedItems.length < 2}>
Konfirmasi Konfirmasi
</Button> </Button>
</DialogFooter> </DialogFooter>