import { useState } from 'react'; import { useCreateWare, useDeleteWare, usePatchWare } from '../api/queries'; import { NumberInput } from './NumberInput'; import { COIN_LABELS, CURRENCIES, formatMoney } from '../lib/format'; import { scaledDamageDice, MAX_GEAR_LEVEL } from '../lib/previewCalc'; import { stockLabel, wareName } from '../lib/wares'; import type { Currency, Item, SaleOffer, Weapon } from '../api/types'; // One ware in the DM's list. "Edit" swaps the line for its price and stock. function WareRow({ campaignId, ware, onError }: { campaignId: string; ware: SaleOffer; onError: (err: unknown) => void }) { const patchWare = usePatchWare(campaignId); const deleteWare = useDeleteWare(campaignId); const [editing, setEditing] = useState(false); const [price, setPrice] = useState(''); const [currency, setCurrency] = useState(ware.currency); const [stock, setStock] = useState(''); function startEditing() { setPrice(String(ware.price)); setCurrency(ware.currency); setStock(ware.stock === null ? '' : String(ware.stock)); setEditing(true); } function handleSave() { const amount = Math.floor(Number(price)); if (!(amount > 0)) return; patchWare.mutate({ id: ware.id, price: amount, currency, stock: stock === '' ? null : Number(stock) }, { onError, onSuccess: () => setEditing(false) }); } return (
{wareName(ware)} — {formatMoney(ware.price, ware.currency)} · {stockLabel(ware)}
{editing && (
setPrice(e.target.value)} style={{ width: 90 }} /> setStock(e.target.value)} style={{ flex: 1, minWidth: 120 }} />
)}
); } // The DM's list of what one seller has for sale — an NPC, or an encounter // itself — with a form to put something from the campaign pools up for sale. export function WaresEditor({ campaignId, seller, wares, weapons, items, onError, }: { campaignId: string; seller: { npcId: string } | { encounterId: string }; wares: SaleOffer[]; weapons: Weapon[]; items: Item[]; onError: (err: unknown) => void; }) { const createWare = useCreateWare(campaignId); const [kind, setKind] = useState<'item' | 'weapon'>('item'); const [poolId, setPoolId] = useState(''); const [level, setLevel] = useState(''); const [price, setPrice] = useState(''); const [currency, setCurrency] = useState('GOLD'); const [stock, setStock] = useState(''); const pool: (Weapon | Item)[] = kind === 'weapon' ? weapons : items; const picked = pool.find((entry) => entry.id === poolId); function handleAdd() { const amount = Math.floor(Number(price)); if (!picked || !(amount > 0)) return; createWare.mutate( { ...seller, ...(kind === 'weapon' ? { weaponId: picked.id } : { itemId: picked.id }), price: amount, currency, level: level ? Number(level) : undefined, stock: stock === '' ? null : Number(stock), }, { onError, onSuccess: () => { setPoolId(''); setLevel(''); setPrice(''); setStock(''); }, } ); } return (
{wares.length === 0 && (

Nothing for sale yet.

)} {wares.map((ware) => ( ))}
{picked && ( )}
setPrice(e.target.value)} style={{ width: 90 }} /> setStock(e.target.value)} style={{ flex: 1, minWidth: 120 }} />
); }