User-supplied asset ids; brand on product; strain is the name
Build and push image / build (push) Successful in 48s
Build and push image / build (push) Successful in 48s
Four UX changes after using the rework for a bit: 1. Asset ids are 6-digit numbers from a roll of physical labels — server no longer generates them. POST /api/inventory requires assetId; the add-inventory form has a digits-only input that auto-focuses on entry. 2. Strain and product name are the same thing. Drop products.name; the strain's name supplies the display. Product creation just asks for "Name (strain)" and matches/creates a strain by that name. 3. Brand moves from inventory_items to products. SKUs are brand-specific, so all instances of a product share the brand. Brand selector lives on the product create/edit form, not the per-instance form. 4. Scanning an unknown SKU on the add-inventory step now opens the create-product subform with the SKU prefilled — one less click. Migration: detect prior shape (products.name column present) and rename products/inventory_items/audits to *_v1 archives, recreate empty. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
+4
-4
@@ -18,11 +18,11 @@ export const api = {
|
||||
// Catalog: products
|
||||
createProduct: (body: {
|
||||
sku: string;
|
||||
name: string;
|
||||
type: string;
|
||||
kind: "bulk" | "discrete";
|
||||
strainId?: string | null;
|
||||
strainName?: string;
|
||||
brandId?: string | null;
|
||||
defaultThc?: number;
|
||||
defaultCbd?: number;
|
||||
defaultTotalCannabinoids?: number;
|
||||
@@ -31,10 +31,11 @@ export const api = {
|
||||
updateProduct: (
|
||||
id: string,
|
||||
body: Partial<{
|
||||
name: string;
|
||||
type: string;
|
||||
kind: "bulk" | "discrete";
|
||||
strainId: string | null;
|
||||
strainName: string;
|
||||
brandId: string | null;
|
||||
}>,
|
||||
) =>
|
||||
request<{ ok: true }>(`/products/${id}`, {
|
||||
@@ -62,8 +63,8 @@ export const api = {
|
||||
|
||||
// Inventory items (instances)
|
||||
createInventoryItem: (body: {
|
||||
assetId: string;
|
||||
productId: string;
|
||||
brandId?: string | null;
|
||||
shopId?: string | null;
|
||||
binId?: string | null;
|
||||
price: number;
|
||||
@@ -83,7 +84,6 @@ export const api = {
|
||||
updateInventoryItem: (
|
||||
id: string,
|
||||
body: Partial<{
|
||||
brandId: string | null;
|
||||
shopId: string | null;
|
||||
binId: string | null;
|
||||
price: number;
|
||||
|
||||
@@ -46,7 +46,7 @@ export function ProductDetail({
|
||||
["Asset id", <span className="mono">{item.assetId}</span>],
|
||||
["SKU", <span className="mono">{item.sku}</span>],
|
||||
["Type", `${item.type} · ${item.kind}`],
|
||||
["Strain", item.strainName ?? <span style={{ color: "var(--ink-3)" }}>Unlinked</span>],
|
||||
["Strain", item.name],
|
||||
["Brand", helpers.brandName(data, item.brandId)],
|
||||
["Shop", helpers.shopName(data, item.shopId)],
|
||||
["Total cannabinoids", `${item.totalCannabinoids.toFixed(1)}%`],
|
||||
|
||||
@@ -14,11 +14,16 @@ export function ScanField({
|
||||
items,
|
||||
products,
|
||||
onMatch,
|
||||
onScanNoMatch,
|
||||
matchedLabel,
|
||||
}: {
|
||||
items: Item[];
|
||||
products?: Product[];
|
||||
onMatch: (result: ScanResult) => void;
|
||||
// Fired once after a debounce when the scanned text doesn't resolve to
|
||||
// any known asset id or SKU. The parent can use the raw value (e.g. to
|
||||
// open a "create new product" form prefilled with the scanned SKU).
|
||||
onScanNoMatch?: (raw: string) => void;
|
||||
matchedLabel: string | null;
|
||||
}) {
|
||||
const [scan, setScan] = useState("");
|
||||
@@ -32,11 +37,10 @@ export function ScanField({
|
||||
}
|
||||
const hit = lookup(trimmed, items, products);
|
||||
if (hit) {
|
||||
const label = hit.kind === "item" ? hit.item.name : hit.product.sku;
|
||||
onMatch(hit);
|
||||
setScan("");
|
||||
const name =
|
||||
hit.kind === "item" ? hit.item.name : hit.product.name;
|
||||
setFeedback({ type: "matched", text: `Matched ${name}` });
|
||||
setFeedback({ type: "matched", text: `Matched ${label}` });
|
||||
}
|
||||
}, [scan]); // eslint-disable-line react-hooks/exhaustive-deps
|
||||
|
||||
@@ -45,9 +49,15 @@ export function ScanField({
|
||||
useEffect(() => {
|
||||
if (!scan.trim() || feedback?.type === "matched") return;
|
||||
const timer = setTimeout(() => {
|
||||
const trimmed = scan.trim().toLowerCase();
|
||||
if (!lookup(trimmed, items, products)) {
|
||||
setFeedback({ type: "miss", text: "No asset id or SKU matches that." });
|
||||
const raw = scan.trim();
|
||||
if (!lookup(raw.toLowerCase(), items, products)) {
|
||||
if (onScanNoMatch) {
|
||||
onScanNoMatch(raw);
|
||||
setScan("");
|
||||
setFeedback(null);
|
||||
} else {
|
||||
setFeedback({ type: "miss", text: "No asset id or SKU matches that." });
|
||||
}
|
||||
}
|
||||
}, 400);
|
||||
return () => clearTimeout(timer);
|
||||
|
||||
@@ -1,7 +1,13 @@
|
||||
import { useEffect, useMemo, useState } from "react";
|
||||
import { useMutation, useQueryClient } from "@tanstack/react-query";
|
||||
import type { Bootstrap, InventoryItem, Item, Product, Strain } from "../../types.js";
|
||||
import { TYPES, TODAY_STR, enrichItems, getLastInstance } from "../../types.js";
|
||||
import {
|
||||
ASSET_ID_RE,
|
||||
TYPES,
|
||||
TODAY_STR,
|
||||
enrichItems,
|
||||
getLastInstance,
|
||||
} from "../../types.js";
|
||||
import { fmt } from "../../format.js";
|
||||
import { api } from "../../api.js";
|
||||
import { Btn, Field, Input, Select } from "../primitives/index.js";
|
||||
@@ -25,6 +31,10 @@ export function AddInventoryFlow({ data, onClose }: { data: Bootstrap; onClose:
|
||||
const product = productId
|
||||
? data.products.find((p) => p.id === productId) ?? null
|
||||
: null;
|
||||
const productName =
|
||||
product?.strainId
|
||||
? data.strains.find((s) => s.id === product.strainId)?.name ?? ""
|
||||
: "";
|
||||
|
||||
const goToDetails = (id: string) => {
|
||||
setProductId(id);
|
||||
@@ -48,7 +58,7 @@ export function AddInventoryFlow({ data, onClose }: { data: Bootstrap; onClose:
|
||||
step === "select"
|
||||
? "Add inventory"
|
||||
: step === "details"
|
||||
? `Add ${product?.name ?? ""}`
|
||||
? `Add ${productName}`
|
||||
: "Saved"
|
||||
}
|
||||
eyebrow={
|
||||
@@ -75,6 +85,7 @@ export function AddInventoryFlow({ data, onClose }: { data: Bootstrap; onClose:
|
||||
data={data}
|
||||
items={items}
|
||||
product={product}
|
||||
productName={productName}
|
||||
onBack={() => setStep("select")}
|
||||
onSaved={(assetId) => {
|
||||
setSavedAssetId(assetId);
|
||||
@@ -87,7 +98,7 @@ export function AddInventoryFlow({ data, onClose }: { data: Bootstrap; onClose:
|
||||
{step === "done" && savedAssetId && product && (
|
||||
<DonePane
|
||||
assetId={savedAssetId}
|
||||
productName={product.name}
|
||||
productName={productName}
|
||||
onAddAnother={() => {
|
||||
setSavedAssetId(null);
|
||||
setProductId(null);
|
||||
@@ -123,10 +134,12 @@ function SelectProductStep({
|
||||
|
||||
// New-product subform
|
||||
const [newSku, setNewSku] = useState("");
|
||||
const [newName, setNewName] = useState("");
|
||||
const [newName, setNewName] = useState(""); // strain name; doubles as display name
|
||||
const [newType, setNewType] = useState("Flower");
|
||||
const [newStrain, setNewStrain] = useState(""); // typed strain name
|
||||
const [newStrainId, setNewStrainId] = useState<string>(""); // empty = match-by-name / create
|
||||
const [newBrandId, setNewBrandId] = useState<string>(
|
||||
data.brands[0]?.id ?? NEW_BRAND,
|
||||
);
|
||||
const [newBrandName, setNewBrandName] = useState("");
|
||||
const [error, setError] = useState<string | null>(null);
|
||||
|
||||
const handleScan = (result: ScanResult) => {
|
||||
@@ -140,29 +153,43 @@ function SelectProductStep({
|
||||
}
|
||||
};
|
||||
|
||||
const handleNoMatch = (raw: string) => {
|
||||
// Scanned a SKU we've never seen — open the create form prefilled.
|
||||
setNewSku(raw.toUpperCase());
|
||||
setCreating(true);
|
||||
};
|
||||
|
||||
const matchedStrain: Strain | null = useMemo(() => {
|
||||
const q = newStrain.trim().toLowerCase();
|
||||
const q = newName.trim().toLowerCase();
|
||||
if (!q) return null;
|
||||
return data.strains.find((s) => s.name.trim().toLowerCase() === q) ?? null;
|
||||
}, [newStrain, data.strains]);
|
||||
}, [newName, data.strains]);
|
||||
|
||||
const create = useMutation({
|
||||
mutationFn: async () => {
|
||||
const sku = newSku.trim();
|
||||
const name = newName.trim();
|
||||
const strainName = newStrain.trim() || name;
|
||||
if (!sku) throw new Error("SKU required");
|
||||
if (!name) throw new Error("Product name required");
|
||||
if (!name) throw new Error("Name (strain) required");
|
||||
const cfg = TYPES.find((t) => t.id === newType);
|
||||
if (!cfg) throw new Error("Type required");
|
||||
|
||||
let brandId: string | null = null;
|
||||
if (newBrandId === NEW_BRAND) {
|
||||
if (!newBrandName.trim()) throw new Error("New brand name required");
|
||||
const b = await api.createBrand(newBrandName.trim());
|
||||
brandId = b.id;
|
||||
} else {
|
||||
brandId = newBrandId || null;
|
||||
}
|
||||
|
||||
const result = await api.createProduct({
|
||||
sku,
|
||||
name,
|
||||
type: newType,
|
||||
kind: cfg.kind,
|
||||
strainId: newStrainId || matchedStrain?.id || undefined,
|
||||
strainName: newStrainId || matchedStrain ? undefined : strainName,
|
||||
strainId: matchedStrain?.id,
|
||||
strainName: matchedStrain ? undefined : name,
|
||||
brandId,
|
||||
});
|
||||
return result.id;
|
||||
},
|
||||
@@ -173,6 +200,8 @@ function SelectProductStep({
|
||||
onError: (e: Error) => setError(e.message),
|
||||
});
|
||||
|
||||
const isNewBrand = newBrandId === NEW_BRAND;
|
||||
|
||||
return (
|
||||
<>
|
||||
<div style={{ padding: 32 }}>
|
||||
@@ -180,6 +209,7 @@ function SelectProductStep({
|
||||
items={items}
|
||||
products={data.products}
|
||||
onMatch={handleScan}
|
||||
onScanNoMatch={creating ? undefined : handleNoMatch}
|
||||
matchedLabel={null}
|
||||
/>
|
||||
|
||||
@@ -190,11 +220,15 @@ function SelectProductStep({
|
||||
value={pickedProductId}
|
||||
onChange={(e) => setPickedProductId(e.target.value)}
|
||||
>
|
||||
{data.products.map((p) => (
|
||||
<option key={p.id} value={p.id}>
|
||||
{p.name} · {p.sku} ({p.type})
|
||||
</option>
|
||||
))}
|
||||
{data.products.map((p) => {
|
||||
const strainName =
|
||||
data.strains.find((s) => s.id === p.strainId)?.name ?? "?";
|
||||
return (
|
||||
<option key={p.id} value={p.id}>
|
||||
{strainName} · {p.sku} ({p.type})
|
||||
</option>
|
||||
);
|
||||
})}
|
||||
</Select>
|
||||
</Field>
|
||||
</div>
|
||||
@@ -227,6 +261,7 @@ function SelectProductStep({
|
||||
value={newSku}
|
||||
placeholder="SKU-XXXXXX"
|
||||
onChange={(e) => setNewSku(e.target.value)}
|
||||
autoFocus={!newSku}
|
||||
/>
|
||||
</Field>
|
||||
<Field label="Type">
|
||||
@@ -238,41 +273,43 @@ function SelectProductStep({
|
||||
))}
|
||||
</Select>
|
||||
</Field>
|
||||
<Field label="Product name" span={2}>
|
||||
<Input
|
||||
value={newName}
|
||||
placeholder="e.g. Garden Ghost 3.5g"
|
||||
onChange={(e) => setNewName(e.target.value)}
|
||||
/>
|
||||
</Field>
|
||||
<Field
|
||||
label="Strain"
|
||||
label="Name (strain)"
|
||||
span={2}
|
||||
hint={
|
||||
matchedStrain
|
||||
? `Will link to existing strain "${matchedStrain.name}".`
|
||||
: "Will create a new strain entry from this name (defaults blank — link from product later)."
|
||||
: "Will create a new strain entry from this name."
|
||||
}
|
||||
>
|
||||
<Select
|
||||
value={newStrainId}
|
||||
onChange={(e) => setNewStrainId(e.target.value)}
|
||||
style={{ marginBottom: 8 }}
|
||||
>
|
||||
<option value="">— Match by name typed below —</option>
|
||||
{data.strains.map((s) => (
|
||||
<option key={s.id} value={s.id}>
|
||||
{s.name}
|
||||
</option>
|
||||
))}
|
||||
</Select>
|
||||
<Input
|
||||
value={newStrain}
|
||||
placeholder={`Strain name (defaults to product name if blank)`}
|
||||
onChange={(e) => setNewStrain(e.target.value)}
|
||||
disabled={!!newStrainId}
|
||||
value={newName}
|
||||
placeholder="e.g. Garden Ghost"
|
||||
onChange={(e) => setNewName(e.target.value)}
|
||||
/>
|
||||
</Field>
|
||||
<Field label="Brand">
|
||||
<Select
|
||||
value={newBrandId}
|
||||
onChange={(e) => setNewBrandId(e.target.value)}
|
||||
>
|
||||
{data.brands.map((b) => (
|
||||
<option key={b.id} value={b.id}>
|
||||
{b.name}
|
||||
</option>
|
||||
))}
|
||||
<option value={NEW_BRAND}>+ Add new brand…</option>
|
||||
</Select>
|
||||
</Field>
|
||||
{isNewBrand && (
|
||||
<Field label="New brand name">
|
||||
<Input
|
||||
value={newBrandName}
|
||||
onChange={(e) => setNewBrandName(e.target.value)}
|
||||
placeholder="e.g. Foxglove Farms"
|
||||
/>
|
||||
</Field>
|
||||
)}
|
||||
</div>
|
||||
{error && (
|
||||
<div style={{ marginTop: 12, fontSize: 12, color: "var(--terracotta)" }}>{error}</div>
|
||||
@@ -329,12 +366,14 @@ function InstanceDetailsStep({
|
||||
data,
|
||||
items,
|
||||
product,
|
||||
productName,
|
||||
onBack,
|
||||
onSaved,
|
||||
}: {
|
||||
data: Bootstrap;
|
||||
items: Item[];
|
||||
product: Product;
|
||||
productName: string;
|
||||
onBack: () => void;
|
||||
onSaved: (assetId: string) => void;
|
||||
}) {
|
||||
@@ -353,8 +392,8 @@ function InstanceDetailsStep({
|
||||
return last.price;
|
||||
})();
|
||||
|
||||
const [assetId, setAssetId] = useState("");
|
||||
const [form, setForm] = useState({
|
||||
brandId: last?.brandId ?? data.brands[0]?.id ?? NEW_BRAND,
|
||||
shopId: last?.shopId ?? data.shops[0]?.id ?? NEW_SHOP,
|
||||
binId: data.bins[0]?.id ?? NEW_BIN,
|
||||
weight: last?.weight ?? (isDiscrete ? 0 : 3.5),
|
||||
@@ -366,7 +405,6 @@ function InstanceDetailsStep({
|
||||
totalCannabinoids: last?.totalCannabinoids ?? 26,
|
||||
purchaseDate: TODAY_STR,
|
||||
});
|
||||
const [newBrand, setNewBrand] = useState("");
|
||||
const [newShopName, setNewShopName] = useState("");
|
||||
const [newShopLocation, setNewShopLocation] = useState("");
|
||||
const [newBinName, setNewBinName] = useState("");
|
||||
@@ -378,15 +416,15 @@ function InstanceDetailsStep({
|
||||
|
||||
const totalPrice = isDiscrete ? form.price * form.countOriginal : form.price;
|
||||
const cpg = !isDiscrete && form.weight > 0 ? form.price / form.weight : 0;
|
||||
const assetIdValid = ASSET_ID_RE.test(assetId);
|
||||
const assetIdConflict =
|
||||
assetIdValid && data.inventoryItems.some((i) => i.assetId === assetId);
|
||||
|
||||
const save = useMutation({
|
||||
mutationFn: async () => {
|
||||
let { brandId, shopId, binId } = form;
|
||||
if (brandId === NEW_BRAND) {
|
||||
if (!newBrand.trim()) throw new Error("New brand name required");
|
||||
const b = await api.createBrand(newBrand.trim());
|
||||
brandId = b.id;
|
||||
}
|
||||
if (!assetIdValid) throw new Error("Asset id must be exactly 6 digits");
|
||||
if (assetIdConflict) throw new Error("Asset id already used");
|
||||
let { shopId, binId } = form;
|
||||
if (shopId === NEW_SHOP) {
|
||||
if (!newShopName.trim()) throw new Error("New shop name required");
|
||||
const s = await api.createShop({
|
||||
@@ -404,8 +442,8 @@ function InstanceDetailsStep({
|
||||
binId = b.id;
|
||||
}
|
||||
return api.createInventoryItem({
|
||||
assetId,
|
||||
productId: product.id,
|
||||
brandId,
|
||||
shopId,
|
||||
binId,
|
||||
weight: isDiscrete ? undefined : form.weight,
|
||||
@@ -422,13 +460,11 @@ function InstanceDetailsStep({
|
||||
onError: (e: Error) => setError(e.message),
|
||||
});
|
||||
|
||||
const isNewBrand = form.brandId === NEW_BRAND;
|
||||
const isNewShop = form.shopId === NEW_SHOP;
|
||||
const isNewBin = form.binId === NEW_BIN;
|
||||
|
||||
// Find prior instances of this product (excluding any from before bootstrap;
|
||||
// safe — we just count to surface "we've had this N times before").
|
||||
const priorCount = items.filter((i) => i.productId === product.id).length;
|
||||
const brandName = data.brands.find((b) => b.id === product.brandId)?.name ?? "—";
|
||||
|
||||
return (
|
||||
<>
|
||||
@@ -448,17 +484,46 @@ function InstanceDetailsStep({
|
||||
}}
|
||||
>
|
||||
<span>
|
||||
<strong style={{ color: "var(--ink-2)" }}>{product.name}</strong> · {product.type} ·{" "}
|
||||
<strong style={{ color: "var(--ink-2)" }}>{productName}</strong> · {brandName} · {product.type} ·{" "}
|
||||
<span className="mono">{product.sku}</span>
|
||||
</span>
|
||||
<span>
|
||||
{priorCount > 0
|
||||
? `${priorCount} prior instance${priorCount === 1 ? "" : "s"} — fields autofilled from most recent.`
|
||||
? `${priorCount} prior instance${priorCount === 1 ? "" : "s"} — fields autofilled.`
|
||||
: "First instance of this product."}
|
||||
</span>
|
||||
</div>
|
||||
|
||||
<div className="smallcaps" style={{ color: "var(--ink-3)", marginBottom: 16 }}>
|
||||
Asset tag
|
||||
</div>
|
||||
<Field
|
||||
label="Asset id (6 digits)"
|
||||
hint={
|
||||
assetIdConflict
|
||||
? "That asset id is already in use."
|
||||
: "From the next sticker on your roll. Required."
|
||||
}
|
||||
>
|
||||
<Input
|
||||
autoFocus
|
||||
value={assetId}
|
||||
inputMode="numeric"
|
||||
maxLength={6}
|
||||
placeholder="000000"
|
||||
onChange={(e) => setAssetId(e.target.value.replace(/\D/g, "").slice(0, 6))}
|
||||
style={{
|
||||
fontFamily: "var(--mono)",
|
||||
letterSpacing: "0.1em",
|
||||
borderColor: assetIdConflict ? "var(--terracotta)" : undefined,
|
||||
}}
|
||||
/>
|
||||
</Field>
|
||||
|
||||
<div
|
||||
className="smallcaps"
|
||||
style={{ color: "var(--ink-3)", margin: "28px 0 16px" }}
|
||||
>
|
||||
Source
|
||||
</div>
|
||||
<div
|
||||
@@ -469,16 +534,6 @@ function InstanceDetailsStep({
|
||||
marginBottom: 28,
|
||||
}}
|
||||
>
|
||||
<Field label="Brand">
|
||||
<Select value={form.brandId} onChange={(e) => update("brandId", e.target.value)}>
|
||||
{data.brands.map((b) => (
|
||||
<option key={b.id} value={b.id}>
|
||||
{b.name}
|
||||
</option>
|
||||
))}
|
||||
<option value={NEW_BRAND}>+ Add new brand…</option>
|
||||
</Select>
|
||||
</Field>
|
||||
<Field label="Shop">
|
||||
<Select value={form.shopId} onChange={(e) => update("shopId", e.target.value)}>
|
||||
{data.shops.map((s) => (
|
||||
@@ -489,15 +544,16 @@ function InstanceDetailsStep({
|
||||
<option value={NEW_SHOP}>+ Add new shop…</option>
|
||||
</Select>
|
||||
</Field>
|
||||
{isNewBrand && (
|
||||
<Field label="New brand name" span={2}>
|
||||
<Input
|
||||
value={newBrand}
|
||||
onChange={(e) => setNewBrand(e.target.value)}
|
||||
placeholder="e.g. Foxglove Farms"
|
||||
/>
|
||||
</Field>
|
||||
)}
|
||||
<Field label="Bin">
|
||||
<Select value={form.binId} onChange={(e) => update("binId", e.target.value)}>
|
||||
{data.bins.map((b) => (
|
||||
<option key={b.id} value={b.id}>
|
||||
{b.name}
|
||||
</option>
|
||||
))}
|
||||
<option value={NEW_BIN}>+ Add new bin…</option>
|
||||
</Select>
|
||||
</Field>
|
||||
{isNewShop && (
|
||||
<>
|
||||
<Field label="New shop name">
|
||||
@@ -516,16 +572,6 @@ function InstanceDetailsStep({
|
||||
</Field>
|
||||
</>
|
||||
)}
|
||||
<Field label="Bin">
|
||||
<Select value={form.binId} onChange={(e) => update("binId", e.target.value)}>
|
||||
{data.bins.map((b) => (
|
||||
<option key={b.id} value={b.id}>
|
||||
{b.name}
|
||||
</option>
|
||||
))}
|
||||
<option value={NEW_BIN}>+ Add new bin…</option>
|
||||
</Select>
|
||||
</Field>
|
||||
{isNewBin && (
|
||||
<>
|
||||
<Field label="New bin name">
|
||||
@@ -672,7 +718,7 @@ function InstanceDetailsStep({
|
||||
<Btn
|
||||
variant="primary"
|
||||
icon="check"
|
||||
disabled={save.isPending}
|
||||
disabled={save.isPending || !assetIdValid || assetIdConflict}
|
||||
onClick={() => save.mutate()}
|
||||
>
|
||||
{save.isPending ? "Saving…" : "Save inventory item"}
|
||||
@@ -712,9 +758,9 @@ function DonePane({
|
||||
{assetId}
|
||||
</div>
|
||||
<div style={{ fontSize: 13, color: "var(--ink-2)", marginBottom: 8 }}>
|
||||
Label this {productName} with{" "}
|
||||
Stick label{" "}
|
||||
<span className="mono" style={{ color: "var(--ink)" }}>{assetId}</span>{" "}
|
||||
so you can scan it later.
|
||||
on the {productName}.
|
||||
</div>
|
||||
</div>
|
||||
<ModalFooter>
|
||||
|
||||
@@ -7,7 +7,6 @@ import { api } from "../../api.js";
|
||||
import { Btn, Field, Input, Select } from "../primitives/index.js";
|
||||
import { ModalBackdrop, ModalHeader, ModalFooter } from "./ModalChrome.js";
|
||||
|
||||
const NEW_BRAND = "__new_brand__";
|
||||
const NEW_SHOP = "__new_shop__";
|
||||
const NEW_BIN = "__new_bin__";
|
||||
|
||||
@@ -30,7 +29,6 @@ export function EditInventoryFlow({
|
||||
: item.price;
|
||||
|
||||
const [form, setForm] = useState({
|
||||
brandId: item.brandId ?? NEW_BRAND,
|
||||
shopId: item.shopId ?? NEW_SHOP,
|
||||
binId: item.binId ?? NEW_BIN,
|
||||
weight: item.weight,
|
||||
@@ -42,7 +40,6 @@ export function EditInventoryFlow({
|
||||
totalCannabinoids: item.totalCannabinoids,
|
||||
purchaseDate: item.purchaseDate,
|
||||
});
|
||||
const [newBrand, setNewBrand] = useState("");
|
||||
const [newShopName, setNewShopName] = useState("");
|
||||
const [newShopLocation, setNewShopLocation] = useState("");
|
||||
const [newBinName, setNewBinName] = useState("");
|
||||
@@ -58,12 +55,7 @@ export function EditInventoryFlow({
|
||||
|
||||
const save = useMutation({
|
||||
mutationFn: async () => {
|
||||
let { brandId, shopId, binId } = form;
|
||||
if (brandId === NEW_BRAND) {
|
||||
if (!newBrand.trim()) throw new Error("New brand name required");
|
||||
const b = await api.createBrand(newBrand.trim());
|
||||
brandId = b.id;
|
||||
}
|
||||
let { shopId, binId } = form;
|
||||
if (shopId === NEW_SHOP) {
|
||||
if (!newShopName.trim()) throw new Error("New shop name required");
|
||||
const s = await api.createShop({
|
||||
@@ -81,7 +73,6 @@ export function EditInventoryFlow({
|
||||
binId = b.id;
|
||||
}
|
||||
return api.updateInventoryItem(item.id, {
|
||||
brandId,
|
||||
shopId,
|
||||
binId,
|
||||
weight: isDiscrete ? undefined : form.weight,
|
||||
@@ -101,7 +92,6 @@ export function EditInventoryFlow({
|
||||
onError: (e: Error) => setError(e.message),
|
||||
});
|
||||
|
||||
const isNewBrand = form.brandId === NEW_BRAND;
|
||||
const isNewShop = form.shopId === NEW_SHOP;
|
||||
const isNewBin = form.binId === NEW_BIN;
|
||||
|
||||
@@ -159,16 +149,6 @@ export function EditInventoryFlow({
|
||||
marginBottom: 28,
|
||||
}}
|
||||
>
|
||||
<Field label="Brand">
|
||||
<Select value={form.brandId} onChange={(e) => update("brandId", e.target.value)}>
|
||||
{data.brands.map((b) => (
|
||||
<option key={b.id} value={b.id}>
|
||||
{b.name}
|
||||
</option>
|
||||
))}
|
||||
<option value={NEW_BRAND}>+ Add new brand…</option>
|
||||
</Select>
|
||||
</Field>
|
||||
<Field label="Shop">
|
||||
<Select value={form.shopId} onChange={(e) => update("shopId", e.target.value)}>
|
||||
{data.shops.map((s) => (
|
||||
@@ -179,15 +159,16 @@ export function EditInventoryFlow({
|
||||
<option value={NEW_SHOP}>+ Add new shop…</option>
|
||||
</Select>
|
||||
</Field>
|
||||
{isNewBrand && (
|
||||
<Field label="New brand name" span={2}>
|
||||
<Input
|
||||
value={newBrand}
|
||||
onChange={(e) => setNewBrand(e.target.value)}
|
||||
placeholder="e.g. Foxglove Farms"
|
||||
/>
|
||||
</Field>
|
||||
)}
|
||||
<Field label="Bin">
|
||||
<Select value={form.binId} onChange={(e) => update("binId", e.target.value)}>
|
||||
{data.bins.map((b) => (
|
||||
<option key={b.id} value={b.id}>
|
||||
{b.name}
|
||||
</option>
|
||||
))}
|
||||
<option value={NEW_BIN}>+ Add new bin…</option>
|
||||
</Select>
|
||||
</Field>
|
||||
{isNewShop && (
|
||||
<>
|
||||
<Field label="New shop name">
|
||||
@@ -206,16 +187,6 @@ export function EditInventoryFlow({
|
||||
</Field>
|
||||
</>
|
||||
)}
|
||||
<Field label="Bin">
|
||||
<Select value={form.binId} onChange={(e) => update("binId", e.target.value)}>
|
||||
{data.bins.map((b) => (
|
||||
<option key={b.id} value={b.id}>
|
||||
{b.name}
|
||||
</option>
|
||||
))}
|
||||
<option value={NEW_BIN}>+ Add new bin…</option>
|
||||
</Select>
|
||||
</Field>
|
||||
{isNewBin && (
|
||||
<>
|
||||
<Field label="New bin name">
|
||||
|
||||
@@ -1,15 +1,17 @@
|
||||
import { useState } from "react";
|
||||
import { useMemo, useState } from "react";
|
||||
import { useMutation, useQueryClient } from "@tanstack/react-query";
|
||||
import type { Bootstrap, Product } from "../../types.js";
|
||||
import type { Bootstrap, Product, Strain } from "../../types.js";
|
||||
import { TYPES } from "../../types.js";
|
||||
import { TYPE_GLYPHS } from "../../format.js";
|
||||
import { api } from "../../api.js";
|
||||
import { Btn, Field, Input, Select } from "../primitives/index.js";
|
||||
import { ModalBackdrop, ModalHeader, ModalFooter } from "./ModalChrome.js";
|
||||
|
||||
// Catalog-level edit. SKU is immutable (it's a barcode). Type and kind are
|
||||
// editable but should rarely change after first instances exist — we don't
|
||||
// block since data integrity isn't really at risk (kind only flips behavior).
|
||||
const NEW_BRAND = "__new_brand__";
|
||||
|
||||
// Catalog-level edit. SKU is immutable (it's a barcode). Type can change but
|
||||
// rarely should. Name is the strain name — typing it either matches an
|
||||
// existing strain or creates a new one. Brand can be reassigned at any time.
|
||||
export function EditProductFlow({
|
||||
data,
|
||||
product,
|
||||
@@ -21,21 +23,45 @@ export function EditProductFlow({
|
||||
}) {
|
||||
const qc = useQueryClient();
|
||||
|
||||
const [name, setName] = useState(product.name);
|
||||
const initialStrain = data.strains.find((s) => s.id === product.strainId);
|
||||
|
||||
const [name, setName] = useState(initialStrain?.name ?? "");
|
||||
const [type, setType] = useState(product.type);
|
||||
const [strainId, setStrainId] = useState<string>(product.strainId ?? "");
|
||||
const [brandId, setBrandId] = useState<string>(product.brandId ?? NEW_BRAND);
|
||||
const [newBrandName, setNewBrandName] = useState("");
|
||||
const [error, setError] = useState<string | null>(null);
|
||||
|
||||
const cfg = TYPES.find((t) => t.id === type);
|
||||
|
||||
const matchedStrain: Strain | null = useMemo(() => {
|
||||
const q = name.trim().toLowerCase();
|
||||
if (!q) return null;
|
||||
return data.strains.find((s) => s.name.trim().toLowerCase() === q) ?? null;
|
||||
}, [name, data.strains]);
|
||||
|
||||
const save = useMutation({
|
||||
mutationFn: () =>
|
||||
api.updateProduct(product.id, {
|
||||
name: name.trim(),
|
||||
mutationFn: async () => {
|
||||
if (!name.trim()) throw new Error("Name (strain) required");
|
||||
let nextBrandId: string | null = null;
|
||||
if (brandId === NEW_BRAND) {
|
||||
if (newBrandName.trim()) {
|
||||
const b = await api.createBrand(newBrandName.trim());
|
||||
nextBrandId = b.id;
|
||||
} else {
|
||||
nextBrandId = null;
|
||||
}
|
||||
} else {
|
||||
nextBrandId = brandId || null;
|
||||
}
|
||||
return api.updateProduct(product.id, {
|
||||
type,
|
||||
kind: cfg?.kind ?? product.kind,
|
||||
strainId: strainId || null,
|
||||
}),
|
||||
// Server resolves matched-by-name to existing strain, else creates one
|
||||
strainId: matchedStrain?.id,
|
||||
strainName: matchedStrain ? undefined : name.trim(),
|
||||
brandId: nextBrandId,
|
||||
});
|
||||
},
|
||||
onSuccess: () => {
|
||||
qc.invalidateQueries({ queryKey: ["bootstrap"] });
|
||||
onClose();
|
||||
@@ -43,6 +69,8 @@ export function EditProductFlow({
|
||||
onError: (e: Error) => setError(e.message),
|
||||
});
|
||||
|
||||
const isNewBrand = brandId === NEW_BRAND;
|
||||
|
||||
return (
|
||||
<ModalBackdrop onClose={onClose}>
|
||||
<div
|
||||
@@ -81,17 +109,25 @@ export function EditProductFlow({
|
||||
</span>
|
||||
<span>
|
||||
SKU <span className="mono" style={{ color: "var(--ink-2)" }}>{product.sku}</span>{" "}
|
||||
is locked. Edit individual purchases (price, brand, batch THC) from the
|
||||
is locked. Edit individual purchases (price, batch THC) from the
|
||||
inventory drawer.
|
||||
</span>
|
||||
</div>
|
||||
|
||||
<div style={{ display: "grid", gridTemplateColumns: "repeat(2, 1fr)", gap: 16 }}>
|
||||
<Field label="Product name" span={2}>
|
||||
<Field
|
||||
label="Name (strain)"
|
||||
span={2}
|
||||
hint={
|
||||
matchedStrain
|
||||
? `Will link to existing strain "${matchedStrain.name}".`
|
||||
: "Will create a new strain entry from this name."
|
||||
}
|
||||
>
|
||||
<Input
|
||||
value={name}
|
||||
onChange={(e) => setName(e.target.value)}
|
||||
placeholder="e.g. Garden Ghost 3.5g"
|
||||
placeholder="e.g. Garden Ghost"
|
||||
/>
|
||||
</Field>
|
||||
<Field label="Type">
|
||||
@@ -103,16 +139,25 @@ export function EditProductFlow({
|
||||
))}
|
||||
</Select>
|
||||
</Field>
|
||||
<Field label="Strain">
|
||||
<Select value={strainId} onChange={(e) => setStrainId(e.target.value)}>
|
||||
<option value="">— Unlinked —</option>
|
||||
{data.strains.map((s) => (
|
||||
<option key={s.id} value={s.id}>
|
||||
{s.name}
|
||||
<Field label="Brand">
|
||||
<Select value={brandId} onChange={(e) => setBrandId(e.target.value)}>
|
||||
{data.brands.map((b) => (
|
||||
<option key={b.id} value={b.id}>
|
||||
{b.name}
|
||||
</option>
|
||||
))}
|
||||
<option value={NEW_BRAND}>+ Add new brand…</option>
|
||||
</Select>
|
||||
</Field>
|
||||
{isNewBrand && (
|
||||
<Field label="New brand name (or leave blank for unbranded)" span={2}>
|
||||
<Input
|
||||
value={newBrandName}
|
||||
onChange={(e) => setNewBrandName(e.target.value)}
|
||||
placeholder="e.g. Foxglove Farms"
|
||||
/>
|
||||
</Field>
|
||||
)}
|
||||
</div>
|
||||
|
||||
{error && (
|
||||
|
||||
+18
-14
@@ -11,25 +11,26 @@ export interface Audit {
|
||||
}
|
||||
|
||||
// Product = catalog entry. Identified by SKU. One row per "kind of thing
|
||||
// you can scan" — strain + form factor (Flower/bulk, Pre-roll/discrete, …).
|
||||
// you can scan" — strain + brand + form factor (Flower/bulk, Pre-roll/discrete).
|
||||
// Display name comes from the linked strain (strainId is required).
|
||||
export interface Product {
|
||||
id: string;
|
||||
sku: string;
|
||||
strainId: string | null;
|
||||
name: string;
|
||||
strainId: string;
|
||||
brandId: string | null;
|
||||
type: string;
|
||||
kind: ProductKind;
|
||||
createdAt: string;
|
||||
}
|
||||
|
||||
// InventoryItem = a physical jar/pack you bought. Has its own asset id
|
||||
// (label-printed). Carries everything that varies per purchase: brand, shop,
|
||||
// bin, price, cannabinoids, weight/count, lifecycle, audits.
|
||||
// InventoryItem = a physical jar/pack you bought. Has a 6-digit asset id
|
||||
// (printed on a roll of labels the user owns). Carries per-batch values:
|
||||
// shop, bin, price, cannabinoids, weight/count, lifecycle, audits. Brand
|
||||
// lives on the product, not here.
|
||||
export interface InventoryItem {
|
||||
id: string;
|
||||
assetId: string;
|
||||
productId: string;
|
||||
brandId: string | null;
|
||||
shopId: string | null;
|
||||
binId: string | null;
|
||||
price: number;
|
||||
@@ -52,15 +53,15 @@ export interface InventoryItem {
|
||||
|
||||
// Item = InventoryItem with its product's catalog fields denormalized in.
|
||||
// Built once from bootstrap (`enrichItems`) so views can access `name`,
|
||||
// `sku`, `type`, `kind` without a per-row lookup. This is the shape the
|
||||
// UI and helpers operate on.
|
||||
// `sku`, `type`, `kind`, `brandId` without a per-row lookup. The display
|
||||
// `name` is the strain's name. This is the shape the UI and helpers operate on.
|
||||
export interface Item extends InventoryItem {
|
||||
name: string;
|
||||
sku: string;
|
||||
type: string;
|
||||
kind: ProductKind;
|
||||
strainId: string | null;
|
||||
strainName: string | null;
|
||||
brandId: string | null;
|
||||
strainId: string;
|
||||
}
|
||||
|
||||
export interface Strain {
|
||||
@@ -118,6 +119,9 @@ export const TYPES: TypeConfig[] = [
|
||||
{ id: "Vaporizer", kind: "discrete", auditMode: "presence", cadenceDays: 30, unit: "ct", weighable: false },
|
||||
];
|
||||
|
||||
// User-supplied 6-digit asset ids are printed on a roll of physical tags.
|
||||
export const ASSET_ID_RE = /^\d{6}$/;
|
||||
|
||||
// Local-time YYYY-MM-DD captured once at module load. Used as the default
|
||||
// value for date inputs and as the "today" anchor for days-since math.
|
||||
export const TODAY_STR = (() => {
|
||||
@@ -138,15 +142,15 @@ export function enrichItems(data: Bootstrap): Item[] {
|
||||
for (const inv of data.inventoryItems) {
|
||||
const product = productMap.get(inv.productId);
|
||||
if (!product) continue;
|
||||
const strain = product.strainId ? strainMap.get(product.strainId) ?? null : null;
|
||||
const strain = strainMap.get(product.strainId);
|
||||
out.push({
|
||||
...inv,
|
||||
name: product.name,
|
||||
name: strain?.name ?? "(unknown strain)",
|
||||
sku: product.sku,
|
||||
type: product.type,
|
||||
kind: product.kind,
|
||||
brandId: product.brandId,
|
||||
strainId: product.strainId,
|
||||
strainName: strain?.name ?? null,
|
||||
});
|
||||
}
|
||||
return out;
|
||||
|
||||
@@ -68,7 +68,13 @@ export function BrandsView({
|
||||
}}
|
||||
>
|
||||
{data.brands.map((b) => {
|
||||
const itemCount = data.inventoryItems.filter((i) => i.brandId === b.id).length;
|
||||
// Count instances whose product points at this brand.
|
||||
const productIds = new Set(
|
||||
data.products.filter((p) => p.brandId === b.id).map((p) => p.id),
|
||||
);
|
||||
const itemCount = data.inventoryItems.filter((i) =>
|
||||
productIds.has(i.productId),
|
||||
).length;
|
||||
return (
|
||||
<Card key={b.id} style={{ display: "flex", alignItems: "center", gap: 12 }}>
|
||||
<div style={{ flex: 1, minWidth: 0 }}>
|
||||
|
||||
@@ -63,8 +63,7 @@ export function Inventory({
|
||||
brand.includes(q) ||
|
||||
shop.includes(q) ||
|
||||
i.sku.toLowerCase().includes(q) ||
|
||||
i.assetId.toLowerCase().includes(q) ||
|
||||
(i.strainName?.toLowerCase().includes(q) ?? false)
|
||||
i.assetId.toLowerCase().includes(q)
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user