From 5fd4cbd42f297067a02776a29b07b11793c37c0c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Oliver=20=C3=98rjavik?= Date: Mon, 16 Nov 2026 15:09:04 +0100 Subject: [PATCH 1/2] fix(recipe): use English ingredient units with legacy mapping The editor offered Norwegian units (ss, ts, stk) in an otherwise English UI. A shared ingredientUnits module now lists English units (g, kg, ml, dL, L, tsp, tbsp, cup, pcs), and maps legacy ss/ts/stk to tbsp/tsp/pcs in the editor and the detail view, so stored recipes keep working without a data migration (saving stores English). Custom stored units such as "cloves" stay selectable. Closes #99 --- .../recipe-detail/RecipeDetailPage.test.tsx | 4 +- .../recipe-detail/RecipeDetailPage.tsx | 3 +- .../recipes/RecipeCreatePage.test.tsx | 25 +++++++++++ .../src/features/recipes/RecipeCreatePage.tsx | 22 +++++----- apps/web/src/lib/ingredientUnits.test.ts | 24 +++++++++++ apps/web/src/lib/ingredientUnits.ts | 42 +++++++++++++++++++ 6 files changed, 106 insertions(+), 14 deletions(-) create mode 100644 apps/web/src/lib/ingredientUnits.test.ts create mode 100644 apps/web/src/lib/ingredientUnits.ts diff --git a/apps/web/src/features/recipe-detail/RecipeDetailPage.test.tsx b/apps/web/src/features/recipe-detail/RecipeDetailPage.test.tsx index f90b9fa..d877925 100644 --- a/apps/web/src/features/recipe-detail/RecipeDetailPage.test.tsx +++ b/apps/web/src/features/recipe-detail/RecipeDetailPage.test.tsx @@ -33,11 +33,11 @@ it('loads details and scales ingredient quantities without changing the server', mount(); expect(await screen.findByRole('heading', { name: 'API soup' })).toBeTruthy(); expect(screen.getByText('Boil the water.')).toBeTruthy(); - expect(screen.getByText('1 l')).toBeTruthy(); + expect(screen.getByText('1 L')).toBeTruthy(); await userEvent .setup() .click(screen.getAllByRole('button', { name: 'Increase servings' })[0]!); - expect(screen.getByText('1.5 l')).toBeTruthy(); + expect(screen.getByText('1.5 L')).toBeTruthy(); expect(screen.getByRole('button', { name: 'Rate Recipe' })).toBeTruthy(); }); it('treats inaccessible private recipes as unavailable', async () => { diff --git a/apps/web/src/features/recipe-detail/RecipeDetailPage.tsx b/apps/web/src/features/recipe-detail/RecipeDetailPage.tsx index 384750d..f8996c1 100644 --- a/apps/web/src/features/recipe-detail/RecipeDetailPage.tsx +++ b/apps/web/src/features/recipe-detail/RecipeDetailPage.tsx @@ -37,6 +37,7 @@ import { } from '../recipes/recipeApi'; import { useRecipeQuery } from '../recipes/useRecipeQuery'; import RecipeQueryStatus from '../recipes/RecipeQueryStatus'; +import { unitLabel } from '../../lib/ingredientUnits'; import './RecipeDetailPage.css'; const MIN_SERVINGS = 1; @@ -569,7 +570,7 @@ function RecipeDetailContent({ recipe }: RecipeDetailContentProps) { ); const amountWithUnit = - `${scaledAmount} ${ingredient.unit}`.trim(); + `${scaledAmount} ${unitLabel(ingredient.unit)}`.trim(); return (
  • diff --git a/apps/web/src/features/recipes/RecipeCreatePage.test.tsx b/apps/web/src/features/recipes/RecipeCreatePage.test.tsx index b05c11c..d333db3 100644 --- a/apps/web/src/features/recipes/RecipeCreatePage.test.tsx +++ b/apps/web/src/features/recipes/RecipeCreatePage.test.tsx @@ -346,3 +346,28 @@ it('does not fetch or reset the new recipe form after list invalidation', () => expect(title).toHaveValue('New draft'); expect(fetch).not.toHaveBeenCalled(); }); +it('opens recipes with legacy Norwegian units as English and saves English', async () => { + let input: unknown; + mockRecipeFetch((operation, variables) => { + const recipe = { + ...apiRecipe, + isCreatedByCurrentUser: true, + ingredients: [{ ...apiRecipe.ingredients[0], unit: 'ss' }], + }; + if (operation === 'UpdateRecipe') { + input = variables.input; + return { data: { updateRecipe: recipe } }; + } + return { data: { recipe } }; + }); + mount('/recipes/' + apiRecipe.id + '/edit'); + await screen.findByRole('heading', { name: 'Edit Recipe' }); + expect(screen.getByLabelText('Ingredient 1 unit')).toHaveValue('tbsp'); + expect(screen.queryByRole('option', { name: 'ss' })).toBeNull(); + const user = userEvent.setup(); + await user.click(screen.getByRole('button', { name: 'Save changes' })); + await screen.findByRole('heading', { name: 'Saved recipe destination' }); + expect(input).toMatchObject({ + ingredients: [expect.objectContaining({ unit: 'tbsp' })], + }); +}); diff --git a/apps/web/src/features/recipes/RecipeCreatePage.tsx b/apps/web/src/features/recipes/RecipeCreatePage.tsx index 625399f..3fe5ab9 100644 --- a/apps/web/src/features/recipes/RecipeCreatePage.tsx +++ b/apps/web/src/features/recipes/RecipeCreatePage.tsx @@ -29,6 +29,7 @@ import { useRecipeQuery } from './useRecipeQuery'; import RecipeQueryStatus from './RecipeQueryStatus'; import LinkButton from '../../components/ui/LinkButton/LinkButton'; import RecipeImageField from './RecipeImageField'; +import { INGREDIENT_UNITS, normalizeUnit } from '../../lib/ingredientUnits'; import './RecipeCreatePage.css'; @@ -95,6 +96,7 @@ function RecipeEditor({ recipe }: { recipe?: Recipe }) { recipe?.ingredients.map((item) => ({ ...item, amount: String(item.amount), + unit: normalizeUnit(item.unit), })) ?? [{ name: '', amount: '', unit: 'g' }], ); const [instructions, setInstructions] = useState( @@ -737,21 +739,19 @@ function RecipeEditor({ recipe }: { recipe?: Recipe }) { updateIngredient(index, 'unit', event.target.value) } > - {!['', 'g', 'kg', 'l', 'dl', 'ss', 'ts', 'stk'].includes( - ingredient.unit, + {/* Keep a custom stored unit (e.g. "cloves") selectable. */} + {!INGREDIENT_UNITS.some( + (unit) => unit.value === ingredient.unit, ) && ( )} - - - - - - - - + {INGREDIENT_UNITS.map((unit) => ( + + ))}
    diff --git a/apps/web/src/lib/ingredientUnits.test.ts b/apps/web/src/lib/ingredientUnits.test.ts new file mode 100644 index 0000000..8c51f85 --- /dev/null +++ b/apps/web/src/lib/ingredientUnits.test.ts @@ -0,0 +1,24 @@ +import { describe, expect, it } from 'vitest'; +import { INGREDIENT_UNITS, normalizeUnit, unitLabel } from './ingredientUnits'; + +describe('ingredient units', () => { + it('translates legacy Norwegian units to English', () => { + expect(normalizeUnit('ss')).toBe('tbsp'); + expect(normalizeUnit('ts')).toBe('tsp'); + expect(normalizeUnit('STK')).toBe('pcs'); + expect(unitLabel('ss')).toBe('tbsp'); + }); + + it('keeps English and custom units as stored', () => { + expect(normalizeUnit('g')).toBe('g'); + expect(unitLabel('dl')).toBe('dL'); + expect(unitLabel('cloves')).toBe('cloves'); + expect(unitLabel('')).toBe(''); + }); + + it('offers no Norwegian units in the editor', () => { + const values: string[] = INGREDIENT_UNITS.map((unit) => unit.value); + for (const legacy of ['ss', 'ts', 'stk']) + expect(values).not.toContain(legacy); + }); +}); diff --git a/apps/web/src/lib/ingredientUnits.ts b/apps/web/src/lib/ingredientUnits.ts new file mode 100644 index 0000000..3e1f0f6 --- /dev/null +++ b/apps/web/src/lib/ingredientUnits.ts @@ -0,0 +1,42 @@ +/** + * Ingredient units offered in the recipe editor, stored by `value` and shown + * by `label`. Units are free text in the API, so recipes may also contain + * other units (e.g. "cloves"); those are kept and shown as stored. + */ +export const INGREDIENT_UNITS = [ + { value: '', label: 'No unit' }, + { value: 'g', label: 'g' }, + { value: 'kg', label: 'kg' }, + { value: 'ml', label: 'ml' }, + { value: 'dl', label: 'dL' }, + { value: 'l', label: 'L' }, + { value: 'tsp', label: 'tsp' }, + { value: 'tbsp', label: 'tbsp' }, + { value: 'cup', label: 'cup' }, + { value: 'pcs', label: 'pcs' }, +] as const; + +/* + * Earlier versions of the editor stored Norwegian abbreviations. They are + * translated when a recipe is shown or opened for editing, so existing + * recipes keep working without a data migration; saving stores English. + */ +const LEGACY_UNITS: Record = { + ss: 'tbsp', // spiseskje + ts: 'tsp', // teskje + stk: 'pcs', // stykk +}; + +/** The stored value to use for a unit, translating legacy Norwegian units. */ +export function normalizeUnit(unit: string) { + return LEGACY_UNITS[unit.trim().toLowerCase()] ?? unit; +} + +/** How a stored unit is displayed next to an amount ('' for no unit). */ +export function unitLabel(unit: string) { + const value = normalizeUnit(unit); + if (!value) return ''; + return ( + INGREDIENT_UNITS.find((option) => option.value === value)?.label ?? value + ); +} From 7a652886b440e403a268dcf39b4d6d69795703a3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Oliver=20=C3=98rjavik?= Date: Tue, 17 Nov 2026 00:57:35 +0100 Subject: [PATCH 2/2] fix(recipe): only map defined legacy units, not inherited properties normalizeUnit() looked units up with a plain index, so free-text units such as "__proto__" or "constructor" resolved to inherited object properties and returned a non-string. Lookups now use Object.hasOwn(). --- apps/web/src/lib/ingredientUnits.test.ts | 8 ++++++++ apps/web/src/lib/ingredientUnits.ts | 5 ++++- 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/apps/web/src/lib/ingredientUnits.test.ts b/apps/web/src/lib/ingredientUnits.test.ts index 8c51f85..5225266 100644 --- a/apps/web/src/lib/ingredientUnits.test.ts +++ b/apps/web/src/lib/ingredientUnits.test.ts @@ -21,4 +21,12 @@ describe('ingredient units', () => { for (const legacy of ['ss', 'ts', 'stk']) expect(values).not.toContain(legacy); }); + + it.each(['__proto__', 'constructor', 'toString', 'hasOwnProperty'])( + 'keeps the free-text unit "%s" as typed, not an inherited property', + (unit) => { + expect(normalizeUnit(unit)).toBe(unit); + expect(unitLabel(unit)).toBe(unit); + }, + ); }); diff --git a/apps/web/src/lib/ingredientUnits.ts b/apps/web/src/lib/ingredientUnits.ts index 3e1f0f6..54caa28 100644 --- a/apps/web/src/lib/ingredientUnits.ts +++ b/apps/web/src/lib/ingredientUnits.ts @@ -29,7 +29,10 @@ const LEGACY_UNITS: Record = { /** The stored value to use for a unit, translating legacy Norwegian units. */ export function normalizeUnit(unit: string) { - return LEGACY_UNITS[unit.trim().toLowerCase()] ?? unit; + const key = unit.trim().toLowerCase(); + // Units are free text: only the mappings defined above count, never + // inherited object properties such as "__proto__" or "constructor". + return Object.hasOwn(LEGACY_UNITS, key) ? LEGACY_UNITS[key]! : unit; } /** How a stored unit is displayed next to an amount ('' for no unit). */