Bisher war die Auswahl eine fest im Frontend verdrahtete Liste und es gab nur eine Form: ueberall stand "3 Glas". Neu unter Verwaltung > Gebinde: anlegen, umbenennen, loeschen, jeweils mit Einzahl und Mehrzahl. Die eingebauten Gebinde lassen sich in der Schreibweise aendern, aber nicht loeschen; ein Gebinde, das ein Artikel verwendet, ebenfalls nicht. Der Artikel speichert weiterhin nur die Einzahl als Text - so bleiben vorhandene Artikel, Sicherungen und CSV-Dateien gueltig, und eine unbekannte Bezeichnung faellt schlicht auf die Einzahl zurueck. Deshalb zieht ein Umbenennen die Artikel mit; sonst zeigten sie auf eine Bezeichnung, die es nicht mehr gibt. Die Mehrzahl greift jetzt in Artikelliste, Artikelseite (Chargen und Gebinde- Auswahl), Auslagern und in allen Ablauf- und Einkaufslisten samt Startseite. Einheiten wie Gramm oder Liter bleiben unveraendert - die haben im Deutschen keine Mehrzahl. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
56 lines
1.9 KiB
JavaScript
56 lines
1.9 KiB
JavaScript
import { createContext, useCallback, useContext, useEffect, useState } from "react";
|
||
import { api } from "./api";
|
||
import { useAuth } from "./auth";
|
||
import { formatBestBefore, formatDate, setGebindeFormen } from "./units";
|
||
|
||
export const DATE_FORMAT_KEY = "date_format";
|
||
const DEFAULT_FORMAT = "de";
|
||
|
||
const SettingsContext = createContext({
|
||
dateFormat: DEFAULT_FORMAT,
|
||
formatDate: (iso) => formatDate(iso, DEFAULT_FORMAT),
|
||
formatBestBefore: (iso, precision) => formatBestBefore(iso, precision, DEFAULT_FORMAT),
|
||
reload: () => {},
|
||
});
|
||
|
||
/** Lädt die anzeigerelevanten Einstellungen einmal nach dem Login. */
|
||
export function SettingsProvider({ children }) {
|
||
const { user } = useAuth();
|
||
const [dateFormat, setDateFormat] = useState(DEFAULT_FORMAT);
|
||
|
||
const reload = useCallback(async () => {
|
||
try {
|
||
const rows = await api.listSettings();
|
||
const row = rows.find((r) => r.key === DATE_FORMAT_KEY);
|
||
setDateFormat(row?.value || DEFAULT_FORMAT);
|
||
} catch {
|
||
/* Einstellungen sind optional – Standard bleibt bestehen. */
|
||
}
|
||
try {
|
||
// Mehrzahlformen der Gebinde. Sie landen in einer Modulvariablen, weil
|
||
// auch Nicht-Komponenten (die Formatierer in units.js) sie brauchen.
|
||
setGebindeFormen(await api.listPackageTypes());
|
||
} catch {
|
||
/* Ohne Liste bleibt die Einzahl stehen – unschoen, aber nicht falsch. */
|
||
}
|
||
}, []);
|
||
|
||
useEffect(() => {
|
||
if (user) reload();
|
||
}, [user, reload]);
|
||
|
||
const value = {
|
||
dateFormat,
|
||
formatDate: (iso) => formatDate(iso, dateFormat),
|
||
// MHD: Monatsangaben erscheinen als "09/2026", Tagesdaten im gewaehlten Format.
|
||
formatBestBefore: (iso, precision) => formatBestBefore(iso, precision, dateFormat),
|
||
reload,
|
||
};
|
||
|
||
return <SettingsContext.Provider value={value}>{children}</SettingsContext.Provider>;
|
||
}
|
||
|
||
export function useSettings() {
|
||
return useContext(SettingsContext);
|
||
}
|