import { useState } from "react"; import { useMutation, useQueryClient } from "@tanstack/react-query"; import type { Bootstrap, Item } from "../../types.js"; import { TYPES } from "../../types.js"; import { api } from "../../api.js"; import type { BatchOp } from "../../api.js"; import { Btn, Field, Input, Select } from "../primitives/index.js"; import { ModalBackdrop, ModalHeader, ModalFooter } from "./ModalChrome.js"; import { useToast } from "../Toast.js"; export function BulkEditModal({ data, items, onClose, }: { data: Bootstrap; items: Item[]; onClose: () => void; }) { const qc = useQueryClient(); const { toast } = useToast(); const [shopId, setShopId] = useState(""); const [binId, setBinId] = useState(""); const [price, setPrice] = useState(""); const [thc, setThc] = useState(""); const [cbd, setCbd] = useState(""); const [totalCannabinoids, setTotalCannabinoids] = useState(""); const [purchaseDate, setPurchaseDate] = useState(""); const [containerWeight, setContainerWeight] = useState(""); const [error, setError] = useState(null); const hasBulkWeighable = items.some((i) => { const cfg = TYPES.find((t) => t.id === i.type); return i.kind === "bulk" && cfg?.weighable; }); const save = useMutation({ mutationFn: () => { const fields: Record = {}; if (shopId) fields.shopId = shopId; if (binId) fields.binId = binId; if (price !== "") fields.price = parseFloat(price); if (thc !== "") fields.thc = parseFloat(thc); if (cbd !== "") fields.cbd = parseFloat(cbd); if (totalCannabinoids !== "") fields.totalCannabinoids = parseFloat(totalCannabinoids); if (purchaseDate) fields.purchaseDate = purchaseDate; if (containerWeight !== "") fields.containerWeight = parseFloat(containerWeight); if (Object.keys(fields).length === 0) { return Promise.reject(new Error("No fields to update — fill in at least one field.")); } const ops: BatchOp[] = items.map((i) => ({ action: "update" as const, id: i.id, fields, })); return api.batchInventory(ops); }, onSuccess: () => { qc.invalidateQueries({ queryKey: ["bootstrap"] }); toast(`Updated ${items.length} item${items.length === 1 ? "" : "s"}`); onClose(); }, onError: (e: Error) => setError(e.message), }); return (
Only fields you fill in will be updated. Leave blank to keep current values.
Source
Values
setPrice(e.target.value)} /> setPurchaseDate(e.target.value)} /> setThc(e.target.value)} /> setCbd(e.target.value)} /> setTotalCannabinoids(e.target.value)} />
{hasBulkWeighable && (
setContainerWeight(e.target.value)} />
)} {error && (
{error}
)}
Cancel save.mutate()} > {save.isPending ? "Saving…" : `Update ${items.length} item${items.length === 1 ? "" : "s"}`}
); }