Spaces:
Running on CPU Upgrade
Running on CPU Upgrade
Download web/src/apps/amazon-profit/FbaCapacityControl.tsx from fsanyoto/runloopable: direct link, hf CLI and curl.
- Browser
- Download file 4.03 kB
-
https://huggingface.co/spaces/fsanyoto/runloopable/resolve/main/web/src/apps/amazon-profit/FbaCapacityControl.tsx
- Command line
-
hf download hf://spaces/fsanyoto/runloopable/web/src/apps/amazon-profit/FbaCapacityControl.tsx
-
curl -L -o FbaCapacityControl.tsx https://huggingface.co/spaces/fsanyoto/runloopable/resolve/main/web/src/apps/amazon-profit/FbaCapacityControl.tsx
4.03 kB
| import { useEffect, useState } from "react"; | |
| import { API_V1, CREDENTIALS } from "../../apiContract"; | |
| import { refusalMessage } from "../../customer-grid/seam"; | |
| type Capacity = { value: number; unit: string } | null; | |
| type CapacityStatus = { fbaCapacity: Capacity; units: string[]; canEdit: boolean }; | |
| /** The one FBA capacity Command Center and Restock planner share (`/amazon-profit/fba-capacity`). */ | |
| export default function FbaCapacityControl() { | |
| const [status, setStatus] = useState<CapacityStatus | null>(null); | |
| const [draft, setDraft] = useState(""); | |
| const [unit, setUnit] = useState("cu ft"); | |
| const [busy, setBusy] = useState(false); | |
| const [note, setNote] = useState<string | null>(null); | |
| useEffect(() => { | |
| const controller = new AbortController(); | |
| void fetch(`${API_V1}/amazon-profit/fba-capacity`, { credentials: CREDENTIALS, cache: "no-store", | |
| signal: controller.signal }) | |
| .then(async (res) => { | |
| if (!res.ok) throw new Error("capacity refused"); | |
| const body = await res.json() as CapacityStatus; | |
| setStatus(body); | |
| setDraft(body.fbaCapacity ? String(body.fbaCapacity.value) : ""); | |
| setUnit(body.fbaCapacity?.unit ?? body.units[0] ?? "cu ft"); | |
| }) | |
| .catch(() => { if (!controller.signal.aborted) setNote("FBA capacity could not be loaded."); }); | |
| return () => controller.abort(); | |
| }, []); | |
| // Save the number and unit together. An empty box clears the stored value. | |
| const save = async (text: string, nextUnit: string) => { | |
| if (!status?.canEdit || busy) return; | |
| const trimmed = text.trim(); | |
| const value = trimmed === "" ? null : Number(trimmed.replace(/,/g, "")); | |
| if (value !== null && (!Number.isFinite(value) || value <= 0)) { | |
| setNote("Enter a number above zero."); | |
| return; | |
| } | |
| const stored = status.fbaCapacity; | |
| if (value === null ? stored === null : stored?.value === value && stored.unit === nextUnit) return; | |
| setBusy(true); | |
| setNote(null); | |
| try { | |
| const res = await fetch(`${API_V1}/amazon-profit/fba-capacity`, { | |
| method: "PUT", credentials: CREDENTIALS, headers: { "Content-Type": "application/json" }, | |
| body: JSON.stringify({ fbaCapacity: value === null ? null : { value, unit: nextUnit } }), | |
| }); | |
| const body: unknown = await res.json().catch(() => null); | |
| if (!res.ok) { | |
| setNote(refusalMessage(body, res.status, "FBA capacity did not save. Try again.")); | |
| return; | |
| } | |
| const saved = (body as { fbaCapacity?: Capacity } | null)?.fbaCapacity ?? null; | |
| setStatus({ ...status, fbaCapacity: saved }); | |
| setDraft(saved ? String(saved.value) : ""); | |
| if (saved) setUnit(saved.unit); | |
| setNote("Saved"); | |
| } catch { | |
| setNote("FBA capacity did not save. Try again."); | |
| } finally { | |
| setBusy(false); | |
| } | |
| }; | |
| const locked = !status?.canEdit || busy; | |
| const title = status && !status.canEdit ? "Only an editor can change FBA capacity." : undefined; | |
| return <form className="cc-capacity" data-testid="fba-capacity-control" | |
| onSubmit={(event) => { event.preventDefault(); void save(draft, unit); }}> | |
| <label className="cc-capacity-field"> | |
| <span>FBA capacity</span> | |
| <input className="cg-input cc-capacity-value" type="text" inputMode="decimal" aria-label="FBA capacity" | |
| value={draft} disabled={!status || locked} title={title} placeholder="Not set" | |
| onChange={(event) => { setDraft(event.target.value); setNote(null); }} /> | |
| </label> | |
| <select className="cg-select cc-capacity-unit" aria-label="FBA capacity unit" value={unit} | |
| disabled={!status || locked} title={title} | |
| onChange={(event) => { setUnit(event.target.value); setNote(null); }}> | |
| {(status?.units ?? ["cu ft", "cu m"]).map((u) => <option key={u} value={u}>{u}</option>)} | |
| </select> | |
| {status?.canEdit && <button type="submit" className="cg-btn" disabled={busy}>Save</button>} | |
| <span className="cc-capacity-note" role="status">{busy ? "Saving" : note}</span> | |
| </form>; | |
| } | |