Merge branch 'main' into codex/refactor-additemscreen-for-quantity-input

This commit is contained in:
beatz174-bit authored and GitHub committed 2025-11-23 12:09:30 +10:00
commit 6337f69b63
6 files changed
+120 -76

No files matched your search

+2 -2
View File
@@ -23,7 +23,7 @@ export const ActivePickListScreen = () => {
const existing = await db.pickItems.get(itemId);
if (!existing) return;
await db.pickItems.update(itemId, {
quantity_units: existing.quantity_units + 1,
quantity: existing.quantity + 1,
updated_at: Date.now(),
});
};
@@ -41,7 +41,7 @@ export const ActivePickListScreen = () => {
await db.pickItems.update(itemId, { status: 'picked', updated_at: Date.now() });
};
const handleSwipeRight = async (itemId: string) => {
const handleDeleteItem = async (itemId: string) => {
await db.pickItems.delete(itemId);
};
+36 -40
View File
@@ -26,26 +26,18 @@ export const AddItemScreen = () => {
const [quantity, setQuantity] = useState(1);
const [isCarton, setIsCarton] = useState(false);
const navigate = useNavigate();
const existingProductIds = useMemo(
() => new Set(items.map((item) => item.product_id)),
[items],
);
const availableProducts = useMemo(
() => products.filter((product) => !existingProductIds.has(product.id)),
[existingProductIds, products],
);
const unitLabel = selectedProduct?.unit_type ?? 'Units';
const cartonLabel = selectedProduct?.bulk_name ?? 'Cartons';
const filteredProducts = useMemo(() => {
const normalizedQuery = query.trim().toLowerCase();
if (!normalizedQuery) return availableProducts;
if (!normalizedQuery) return products;
return availableProducts.filter((product) => {
return products.filter((product) => {
const searchableText = `${product.name} ${product.category} ${product.barcode ?? ''}`.toLowerCase();
return searchableText.includes(normalizedQuery);
});
}, [availableProducts, query]);
}, [products, query]);
useEffect(() => {
if (!selectedProduct) return;
@@ -67,17 +59,35 @@ export const AddItemScreen = () => {
const addItem = async () => {
const productId = selectedProduct?.id;
if (!id || !productId || existingProductIds.has(productId)) return;
await db.pickItems.add({
id: uuidv4(),
pick_list_id: id,
product_id: productId,
quantity_units: quantity,
quantity_bulk: isCarton ? 1 : 0,
status: 'pending',
created_at: Date.now(),
updated_at: Date.now(),
});
if (!id || !productId) return;
const addOrUpdateItem = async (is_carton: boolean, quantity: number) => {
if (quantity <= 0) return;
const existing = items.find(
(item) => item.product_id === productId && item.is_carton === is_carton,
);
if (existing) {
await db.pickItems.update(existing.id, {
quantity: existing.quantity + quantity,
updated_at: Date.now(),
});
return;
}
await db.pickItems.add({
id: uuidv4(),
pick_list_id: id,
product_id: productId,
quantity,
is_carton,
status: 'pending',
created_at: Date.now(),
updated_at: Date.now(),
});
};
await Promise.all([addOrUpdateItem(false, units), addOrUpdateItem(true, bulk)]);
navigate(`/pick-lists/${id}`);
};
@@ -124,22 +134,8 @@ export const AddItemScreen = () => {
/>
)}
/>
<TextField
label="Quantity"
type="number"
inputProps={{ min: 1 }}
value={quantity}
onChange={(event) => {
const value = Number(event.target.value);
setQuantity(Number.isNaN(value) || value < 1 ? 1 : value);
}}
helperText={quantityHelperText}
fullWidth
/>
<FormControlLabel
control={<Checkbox checked={isCarton} onChange={(event) => setIsCarton(event.target.checked)} />}
label={cartonLabel}
/>
<NumericStepper label={unitLabel} value={units} onChange={setUnits} />
<NumericStepper label={cartonLabel} value={bulk} onChange={setBulk} />
<Button variant="contained" disabled={!selectedProduct} onClick={addItem}>
Add to List
</Button>