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 [comboMaterialName, setComboMaterialName] = useState('');
const [comboSelectedPriceIds, setComboSelectedPriceIds] = useState<number[]>([]);
const [comboSelectedItems, setComboSelectedItems] = useState<{ materialName: string; materialId: number; priceId: number }[]>([]);
const [comboResult, setComboResult] = useState(0);
const comboMaterial = useMemo(
@ -260,40 +260,48 @@ export default function CuttingCreate({ rawMaterials }: Props) {
[fetchedVariants],
);
const openComboDialog = useCallback((materialName: string, preSelectPriceId?: number) => {
setComboMaterialName(materialName);
setComboSelectedPriceIds(preSelectPriceId ? [preSelectPriceId] : []);
const openComboDialog = useCallback((material: { id: number; name: string }, preSelectPriceId?: number) => {
setComboMaterialName(material.name);
if (preSelectPriceId) {
setComboSelectedItems([{ materialName: material.name, materialId: material.id, priceId: preSelectPriceId }]);
} else {
setComboSelectedItems([]);
}
setComboResult(0);
setComboDialogOpen(true);
}, []);
const toggleComboPrice = useCallback((priceId: number) => {
setComboSelectedPriceIds((prev) =>
prev.includes(priceId) ? prev.filter((id) => id !== priceId) : [...prev, priceId],
);
}, []);
const material = comboMaterial;
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(() => {
if (comboSelectedPriceIds.length < 2) {
if (comboSelectedItems.length < 2) {
return;
}
const comboIndex = combinations.length;
const comboMaterialData = rawMaterials.find((m) => m.name === comboMaterialName);
const variants = comboMaterialData ? (fetchedVariants.get(comboMaterialData.id) ?? []) : [];
const newMaterials: MaterialState[] = comboSelectedPriceIds.map((priceId) => {
const price = variants.find((p) => p.id === priceId);
const newMaterials: MaterialState[] = comboSelectedItems.map((item) => {
const variants = fetchedVariants.get(item.materialId) ?? [];
const price = variants.find((p) => p.id === item.priceId);
return {
raw_material_price_id: priceId,
raw_material_price_id: item.priceId,
material_usage: '0',
material_result: 0,
combination_id: comboIndex,
variant: price?.variant ?? '',
material_name: comboMaterialData?.name ?? '',
unit: comboMaterialData?.unit ?? '',
material_name: item.materialName,
unit: rawMaterials.find((m) => m.id === item.materialId)?.unit ?? '',
photo_url: price?.photo_url ?? null,
photo_conversion_url: price?.photo_conversion_url ?? null,
};
@ -309,9 +317,9 @@ export default function CuttingCreate({ rawMaterials }: Props) {
setComboDialogOpen(false);
setComboMaterialName('');
setComboSelectedPriceIds([]);
setComboSelectedItems([]);
setComboResult(0);
}, [comboSelectedPriceIds, comboResult, rawMaterials, fetchedVariants, comboMaterialName, combinations.length]);
}, [comboSelectedItems, comboResult, rawMaterials, fetchedVariants, combinations.length]);
const removeMaterial = useCallback((index: number) => {
setDeleteMaterialIndex(index);
@ -465,7 +473,7 @@ export default function CuttingCreate({ rawMaterials }: Props) {
<Plus className="h-4 w-4" />
Tambah
</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" />
Kombinasi
</Button>
@ -520,7 +528,7 @@ export default function CuttingCreate({ rawMaterials }: Props) {
<Plus className="h-4 w-4" />
Tambah
</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" />
Kombinasi
</Button>
@ -793,23 +801,18 @@ export default function CuttingCreate({ rawMaterials }: Props) {
</Combobox>
</div>
{comboSelectedPriceIds.length > 0 && (
{comboSelectedItems.length > 0 && (
<div className="space-y-2">
<Label className="text-sm font-medium">Varian Dipilih</Label>
<div className="space-y-1">
{comboSelectedPriceIds.map((priceId) => {
let variantName = '';
let materialName = comboMaterialName;
let photoUrl: string | null = null;
const price = comboMaterialVariants.find((pp) => pp.id === priceId);
if (price) {
variantName = price.variant;
photoUrl = price.photo_conversion_url ?? price.photo_url;
}
{comboSelectedItems.map((item) => {
const variants = fetchedVariants.get(item.materialId) ?? [];
const price = variants.find((pp) => pp.id === item.priceId);
const variantName = price?.variant ?? '';
const photoUrl = price?.photo_conversion_url ?? price?.photo_url ?? null;
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">
{photoUrl ? (
<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">
<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>
<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" />
</Button>
</div>
@ -845,7 +848,7 @@ export default function CuttingCreate({ rawMaterials }: Props) {
) : (
<div className="space-y-2">
{comboMaterialVariants.map((price) => {
const isSelected = comboSelectedPriceIds.includes(price.id);
const isSelected = comboSelectedItems.some((item) => item.priceId === price.id);
return (
<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>
<DialogFooter>
<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
</Button>
</DialogFooter>

View File

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