import { useEffect, useState } from "react";
import { Link } from "react-router-dom";
import { api } from "../api";
import Icon from "../components/Icon";
import { amountText, fmt, unitShort } from "../units";
export default function Dashboard() {
const [expiring, setExpiring] = useState([]);
const [shopping, setShopping] = useState([]);
const [groupShopping, setGroupShopping] = useState([]);
const [error, setError] = useState(null);
useEffect(() => {
async function load() {
try {
const [e, s, g] = await Promise.all([
api.expiring(),
api.shoppingList(),
api.groupShoppingList(),
]);
setExpiring(e);
setShopping(s);
setGroupShopping(g);
} catch (err) {
setError(err.message);
}
}
load();
}, []);
const overdue = expiring.filter((e) => e.days_left < 0).length;
return (
Übersicht
Bestand, Ablauf und Bedarf auf einen Blick
Einlagern
Auslagern
{error &&
{error}
}
{overdue > 0 && (
{overdue} Charge(n) im Lager sind bereits abgelaufen – bitte aussortieren.
)}
Bald ablaufend
{expiring.length}
Nachzukaufen
{shopping.length + groupShopping.length}
Bald ablaufend
{expiring.length === 0 ? (
Nichts läuft demnächst ab.
) : (
| Produkt | Menge | MHD | Tage |
{expiring.map((it) => (
| {it.product_name} |
{fmt(it.quantity)} {unitShort(it.base_unit)} |
{it.best_before} |
{it.days_left < 0
? {-it.days_left}d überf.
: it.days_left}
|
))}
)}
Einkaufsliste
{shopping.length === 0 && groupShopping.length === 0 ? (
Alle Mindestbestände erreicht.
) : (
| Bedarf | Bestand | Fehlt |
{groupShopping.map((it) => (
| Gruppe {it.name} |
{fmt(it.stock)} {it.unit_name} |
{fmt(it.deficit)} {it.unit_name} |
))}
{shopping.map((it) => (
| {it.name} |
{amountText(it.stock, it.package_size, it.base_unit)} |
{amountText(it.deficit, it.package_size, it.base_unit)} |
))}
)}
);
}