Refactor inventory forms to include unit and location options; update quantity input handling
This commit is contained in:
@@ -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 };
|
||||
}
|
||||
Reference in New Issue
Block a user