Refactor inventory forms to include unit and location options; update quantity input handling

This commit is contained in:
Nils-Johan Gynther
2026-04-09 23:25:52 +02:00
parent 50d79a348b
commit 03361f7b7d
9 changed files with 191 additions and 92 deletions
+11
View File
@@ -30,4 +30,15 @@ export default async function RecipesPage() {
<RecipePreview recipes={recipes} />
</main>
);
}
function parseQuantityInput(input: string, defaultUnit: string) {
const match = input.trim().match(/^([\d.,]+)\s*([a-zA-Z]*)$/);
if (!match) return { quantity: NaN, unit: defaultUnit };
let [, num, unit] = match;
num = num.replace(',', '.');
unit = unit || defaultUnit;
if (defaultUnit === 'kg' && (unit === 'g' || unit === 'gram')) return { quantity: parseFloat(num) / 1000, unit: 'kg' };
if (defaultUnit === 'g' && (unit === 'kg' || unit === 'kilogram')) return { quantity: parseFloat(num) * 1000, unit: 'g' };
return { quantity: parseFloat(num), unit };
}