mirror of
https://github.com/lrnselfreliance/wrolpi.git
synced 2026-12-03 05:09:58 -05:00
Rebuild Inventory as a config-only system with food catalog and calculators
Replace the DB-backed inventory module with a config-only design and build out food-storage tooling around it. Inventory (config-only, no database) - Each inventory is a YAML file at config/inventory/<slug>.yaml, loaded once into memory and the single source of truth. New reusable MultiFileConfig (wrolpi/common.py) manages a directory of per-entity files. - Inventory types (food/fuel/tool) seed a customizable field schema (text/number/quantity/date/select/location/calories). Unit math/aggregation moved to the frontend (mathjs); Pint dropped from the backend. - Minimal API: GET / (all inventories full), POST /, PUT /<slug> (whole- inventory save with version-based optimistic concurrency; 404 for missing), DELETE. Legacy inventories.yaml / old tables migrate to per-inventory files (pound->lb), and an alembic migration drops the tables. Frontend - New app/src/components/inventory/ package: spreadsheet-style table with Tab/Enter keyboard entry, sortable columns, expired-row highlighting, customizable field editor, and pooled cross-inventory location autocomplete. - A single useInventories store loads everything once and persists whole inventories optimistically; list/items/summary/locations derive client-side. - Summary groups items (default by Category), totals size x count, shows a calories column, and embeds a ration estimate (how long the stock lasts). Food catalog - Shared, editable catalog (config/inventory/catalog.yaml) seeded from catalog_defaults.yaml (~60 generic items, total calories per package) with merge-keep-edits + tombstones. GET/PUT /api/inventory/catalog, a management modal, and entry-row pre-fill of category/size/calories from a name match. Calculators - Generic Food Storage planner (like the Water calculator): household + editable per-person/year amounts -> total to store over a chosen duration, with women/children weighted by DGA calorie shares and an imperial/metric toggle. Also fixes ConfigFile.save() to accept nested file_names. Backend pytest and frontend jest suites updated/added throughout; alembic check clean. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
parent
ae5f64424d
commit
3d6aead072
46 changed files with 3612 additions and 2697 deletions
|
|
@ -16,7 +16,12 @@ import {
|
|||
getDomain,
|
||||
getDownloads,
|
||||
getFiles,
|
||||
getInventory,
|
||||
getInventories,
|
||||
createInventory,
|
||||
saveInventory,
|
||||
deleteInventory,
|
||||
getCatalog,
|
||||
saveCatalog,
|
||||
getOutdatedZims,
|
||||
getSettings,
|
||||
getStatistics,
|
||||
|
|
@ -1471,33 +1476,107 @@ export const useArchiveStatistics = () => {
|
|||
return {statistics, fetchStatistics}
|
||||
}
|
||||
|
||||
export const useInventory = (inventoryId) => {
|
||||
const [byCategory, setByCategory] = useState();
|
||||
const [bySubcategory, setBySubcategory] = useState();
|
||||
const [byName, setByName] = useState();
|
||||
export const useInventories = () => {
|
||||
// Single in-memory store: every inventory (full) is loaded once. The list, the selected inventory's items,
|
||||
// the summary, location suggestions, and the ration calculator all derive from this — no per-action GETs.
|
||||
// Mutations persist the whole changed inventory (optimistically) instead of calling granular endpoints.
|
||||
const [inventories, setInventories] = useState(null);
|
||||
const ref = useRef([]);
|
||||
useEffect(() => {
|
||||
ref.current = inventories || [];
|
||||
}, [inventories]);
|
||||
|
||||
const fetchInventory = async () => {
|
||||
if (!inventoryId) {
|
||||
return
|
||||
const fetchInventories = async () => {
|
||||
const result = await getInventories();
|
||||
if (Array.isArray(result)) {
|
||||
setInventories(result);
|
||||
}
|
||||
|
||||
try {
|
||||
const inventory = await getInventory(inventoryId);
|
||||
setByCategory(enumerate(inventory['by_category']));
|
||||
setBySubcategory(enumerate(inventory['by_subcategory']));
|
||||
setByName(enumerate(inventory['by_name']));
|
||||
} catch (e) {
|
||||
console.error(e);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
useEffect(() => {
|
||||
fetchInventory();
|
||||
}, [inventoryId]);
|
||||
fetchInventories();
|
||||
}, []);
|
||||
|
||||
return {byCategory, bySubcategory, byName, fetchInventory}
|
||||
// Persist a patch ({name?, fields?, items?}) to one inventory: update locally first for snappy UI, then save.
|
||||
// On conflict/error the saved value is null, so we re-fetch to reconcile with the server.
|
||||
const persistInventory = async (slug, patch) => {
|
||||
const current = ref.current.find(i => i.slug === slug);
|
||||
const version = current ? current.version : undefined;
|
||||
setInventories(prev => (prev || []).map(i => i.slug === slug ? {...i, ...patch} : i));
|
||||
const saved = await saveInventory(slug, {...patch, version});
|
||||
if (saved) {
|
||||
setInventories(prev => (prev || []).map(i => i.slug === slug ? saved : i));
|
||||
} else {
|
||||
await fetchInventories();
|
||||
}
|
||||
return saved;
|
||||
};
|
||||
|
||||
const addInventory = async (name, type) => {
|
||||
const inventory = await createInventory({name, type});
|
||||
if (inventory) {
|
||||
setInventories(prev => [...(prev || []), inventory]);
|
||||
}
|
||||
return inventory;
|
||||
};
|
||||
|
||||
const removeInventory = async (slug) => {
|
||||
const response = await deleteInventory(slug);
|
||||
if (response.ok) {
|
||||
setInventories(prev => (prev || []).filter(i => i.slug !== slug));
|
||||
}
|
||||
return response;
|
||||
};
|
||||
|
||||
return {inventories, fetchInventories, persistInventory, addInventory, removeInventory}
|
||||
}
|
||||
|
||||
// The shared food catalog: load once, persist the whole list on change (optimistic).
|
||||
export const useCatalog = () => {
|
||||
const [catalog, setCatalog] = useState(null);
|
||||
|
||||
const fetchCatalog = async () => {
|
||||
const result = await getCatalog();
|
||||
if (Array.isArray(result)) {
|
||||
setCatalog(result);
|
||||
}
|
||||
};
|
||||
|
||||
useEffect(() => {
|
||||
fetchCatalog();
|
||||
}, []);
|
||||
|
||||
const persistCatalog = async (items) => {
|
||||
setCatalog(items);
|
||||
const saved = await saveCatalog(items);
|
||||
if (Array.isArray(saved)) {
|
||||
setCatalog(saved);
|
||||
} else {
|
||||
await fetchCatalog();
|
||||
}
|
||||
return saved;
|
||||
};
|
||||
|
||||
return {catalog, fetchCatalog, persistCatalog};
|
||||
};
|
||||
|
||||
// Pool location suggestions from every inventory's `location`-type fields (shared across inventories).
|
||||
export const collectLocations = (inventories) => {
|
||||
const locations = new Set();
|
||||
(inventories || []).forEach(inv => {
|
||||
const keys = (inv.fields || []).filter(f => f.type === 'location').map(f => f.key);
|
||||
if (keys.length === 0) {
|
||||
return;
|
||||
}
|
||||
(inv.items || []).forEach(item => keys.forEach(k => {
|
||||
if (item[k]) {
|
||||
locations.add(item[k]);
|
||||
}
|
||||
}));
|
||||
});
|
||||
return Array.from(locations).sort();
|
||||
};
|
||||
|
||||
export const useStatistics = () => {
|
||||
const [statistics, setStatistics] = useState();
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue