import { useState } from "react"; import { useMutation, useQueryClient } from "@tanstack/react-query"; import type { Bootstrap, Item } from "../../types.js"; import { TODAY_STR } from "../../types.js"; import { api } from "../../api.js"; import type { BatchOp } from "../../api.js"; import { Btn, Field, Input } from "../primitives/index.js"; import { ModalBackdrop, ModalHeader, ModalFooter } from "./ModalChrome.js"; import { useToast } from "../Toast.js"; export function BulkCheckoutModal({ data, items, onClose, }: { data: Bootstrap; items: Item[]; onClose: () => void; }) { const qc = useQueryClient(); const { toast } = useToast(); const eligible = items.filter((i) => i.status === "active"); const excluded = items.length - eligible.length; const [date, setDate] = useState(TODAY_STR); const [error, setError] = useState(null); const checkout = useMutation({ mutationFn: () => { const ops: BatchOp[] = eligible.map((i) => ({ action: "checkout" as const, id: i.id, date, })); return api.batchInventory(ops); }, onSuccess: () => { qc.invalidateQueries({ queryKey: ["bootstrap"] }); toast(`Checked out ${eligible.length} item${eligible.length === 1 ? "" : "s"}`); onClose(); }, onError: (e: Error) => setError(e.message), }); return (
{excluded > 0 && (
{excluded} item{excluded === 1 ? " is" : "s are"} not active and will be skipped.
)} {eligible.length === 0 ? (
No active items to check out.
) : ( <>
setDate(e.target.value)} />
Items: {eligible.map((i) => i.name).join(", ")}
)} {error && (
{error}
)}
Cancel checkout.mutate()} > {checkout.isPending ? "Saving…" : `Check out ${eligible.length}`}
); }