import { useState } from "react"; import { useMutation, useQueryClient } from "@tanstack/react-query"; import type { Bootstrap, Item } from "../../types.js"; import { helpers, TODAY_STR, enrichItems } from "../../types.js"; import { fmt } from "../../format.js"; import { api } from "../../api.js"; import { Btn, Field, Icon, Input, Textarea } from "../primitives/index.js"; import { ScanField, type ScanResult } from "../ScanField.js"; import { ModalBackdrop, ModalHeader, ModalFooter } from "./ModalChrome.js"; import { useToast } from "../Toast.js"; export function ConsumeFlow({ data, onClose, item: initialItem, }: { data: Bootstrap; onClose: () => void; item: Item | null; }) { const qc = useQueryClient(); const { toast } = useToast(); const allItems = enrichItems(data); const active = allItems.filter((i) => i.status === "active"); const [itemId, setItemId] = useState(initialItem?.id ?? active[0]?.id ?? ""); const [rating, setRating] = useState(4); const [notes, setNotes] = useState(""); const [date, setDate] = useState(TODAY_STR); const [error, setError] = useState(null); const item = allItems.find((i) => i.id === itemId); const finish = useMutation({ mutationFn: () => api.finishInventoryItem(itemId, { date, rating, notes }), onSuccess: () => { qc.invalidateQueries({ queryKey: ["bootstrap"] }); toast(`Marked ${item?.name ?? "item"} as consumed — ${rating}/5 stars`); onClose(); }, onError: (e: Error) => setError(e.message), }); const handleScan = (result: ScanResult) => { if (result.kind === "item") { setItemId(result.item.id); } }; if (!item) return null; const bin = data.bins.find((b) => b.id === item.binId); const lifespan = Math.round((+new Date(date) - +new Date(item.purchaseDate)) / 86_400_000); return (
{item.name}
{item.assetId} · {helpers.brandName(data, item.brandId)} · {bin?.name} · purchased{" "} {fmt.dateShort(item.purchaseDate)}
LASTED
{lifespan} days
setDate(e.target.value)} />
{[1, 2, 3, 4, 5].map((n) => ( ))} {rating}/5