diff --git a/apps/api/src/features/recipes/recipeValidation.ts b/apps/api/src/features/recipes/recipeValidation.ts index 116dc14..326ae0b 100644 --- a/apps/api/src/features/recipes/recipeValidation.ts +++ b/apps/api/src/features/recipes/recipeValidation.ts @@ -1,5 +1,6 @@ import { GraphQLError } from 'graphql'; import { z } from 'zod'; +import { MAX_SERVINGS, MIN_SERVINGS } from '@t31/validation/recipe'; import { uploadedImagePattern } from './imagePaths.js'; export const categorySchema = z.enum([ @@ -31,7 +32,7 @@ export const recipeInputSchema = z.object({ visibility: z.enum(['public', 'private']), prepMinutes: z.number().int().min(0).max(10080), cookMinutes: z.number().int().min(0).max(10080), - servings: z.number().int().min(1).max(1000), + servings: z.number().int().min(MIN_SERVINGS).max(MAX_SERVINGS), ingredients: z .array( z.object({ diff --git a/apps/validation/package.json b/apps/validation/package.json index eadcd73..ddf1726 100644 --- a/apps/validation/package.json +++ b/apps/validation/package.json @@ -6,6 +6,10 @@ ".": { "types": "./dist/auth.d.ts", "import": "./dist/auth.js" + }, + "./recipe": { + "types": "./dist/recipe.d.ts", + "import": "./dist/recipe.js" } }, "scripts": { diff --git a/apps/validation/src/recipe.ts b/apps/validation/src/recipe.ts new file mode 100644 index 0000000..27c8376 --- /dev/null +++ b/apps/validation/src/recipe.ts @@ -0,0 +1,7 @@ +/** + * Servings a recipe can be written for and scaled to. Shared by the API's + * input validation, the recipe form and the detail page's servings control, + * so the three can never disagree. + */ +export const MIN_SERVINGS = 1; +export const MAX_SERVINGS = 1000; diff --git a/apps/web/src/features/recipe-detail/RecipeDetailPage.css b/apps/web/src/features/recipe-detail/RecipeDetailPage.css index 3c8a8c0..f990c2a 100644 --- a/apps/web/src/features/recipe-detail/RecipeDetailPage.css +++ b/apps/web/src/features/recipe-detail/RecipeDetailPage.css @@ -286,11 +286,34 @@ line-height: var(--line-height-tight); } - .recipe-detail-page__servings-value strong { + .recipe-detail-page__servings-input { + width: 4.5ch; + padding: 0; + + border: var(--border-width-thin) solid transparent; + border-radius: var(--radius-sm); + color: var(--color-text-primary); + background: transparent; + font: inherit; font-size: var(--font-size-2xl); + font-weight: var(--font-weight-semibold); line-height: var(--line-height-tight); + text-align: center; + + /* Hide the browser's own spinner; the −/+ buttons do that job. */ + appearance: textfield; + } + + .recipe-detail-page__servings-input::-webkit-inner-spin-button, + .recipe-detail-page__servings-input::-webkit-outer-spin-button { + margin: 0; + appearance: none; + } + + .recipe-detail-page__servings-input:hover { + border-color: var(--color-border-input); } .recipe-detail-page__content-card { diff --git a/apps/web/src/features/recipe-detail/RecipeDetailPage.test.tsx b/apps/web/src/features/recipe-detail/RecipeDetailPage.test.tsx index a0bf9fb..ae74cbf 100644 --- a/apps/web/src/features/recipe-detail/RecipeDetailPage.test.tsx +++ b/apps/web/src/features/recipe-detail/RecipeDetailPage.test.tsx @@ -1,3 +1,4 @@ +import '@testing-library/jest-dom/vitest'; import { render, screen, within } from '@testing-library/react'; import userEvent from '@testing-library/user-event'; import { MemoryRouter, Route, Routes } from 'react-router-dom'; @@ -139,3 +140,30 @@ it('keeps a failed rating editable and refreshes the persisted rating after retr await screen.findByRole('button', { name: 'Rate Recipe' }), ).toBeTruthy(); }); +it('scales beyond 20 servings and accepts a typed number within the API limits', async () => { + mockRecipeFetch(() => ({ data: { recipe: { ...apiRecipe, servings: 40 } } })); + mount(); + await screen.findByRole('heading', { name: 'API soup' }); + const user = userEvent.setup(); + const [servings] = screen.getAllByLabelText('Servings (1–1000)'); + expect(servings).toHaveValue(40); + await user.click( + screen.getAllByRole('button', { name: 'Increase servings' })[0]!, + ); + expect(servings).toHaveValue(41); + await user.clear(servings!); + await user.type(servings!, '250'); + expect(servings).toHaveValue(250); + // Out-of-range input is clamped when the field is left. + await user.clear(servings!); + await user.type(servings!, '5000'); + await user.tab(); + expect(servings).toHaveValue(1000); + expect( + screen.getAllByRole('button', { name: 'Increase servings' })[0], + ).toBeDisabled(); + // Clearing the field and leaving restores the last valid value. + await user.clear(servings!); + await user.tab(); + expect(servings).toHaveValue(1000); +}); diff --git a/apps/web/src/features/recipe-detail/RecipeDetailPage.tsx b/apps/web/src/features/recipe-detail/RecipeDetailPage.tsx index 3857e9c..75ffd2b 100644 --- a/apps/web/src/features/recipe-detail/RecipeDetailPage.tsx +++ b/apps/web/src/features/recipe-detail/RecipeDetailPage.tsx @@ -13,16 +13,10 @@ import Button from '../../components/ui/Button/Button'; import ConfirmationDialog from '../../components/ui/ConfirmationDialog/ConfirmationDialog'; import EmptyState from '../../components/ui/EmptyState/EmptyState'; import { Icon } from '../../components/ui/Icon/Icon'; -import IconButton from '../../components/ui/IconButton/IconButton'; import LinkButton from '../../components/ui/LinkButton/LinkButton'; import Tag from '../../components/ui/Tag/Tag'; import Toast, { type ToastTone } from '../../components/ui/Toast/Toast'; -import { - BookmarkIcon, - EditIcon, - MinusIcon, - PlusIcon, -} from '../../icons/actions'; +import { BookmarkIcon, EditIcon } from '../../icons/actions'; import { BackIcon } from '../../icons/navigation'; import { EmptyStateIcon, PrivateIcon } from '../../icons/recipe'; import { useAppStore } from '../../store'; @@ -38,10 +32,9 @@ import { import { useRecipeQuery } from '../recipes/useRecipeQuery'; import RecipeQueryStatus from '../recipes/RecipeQueryStatus'; import { formatAmount, scaleAmount } from '../../lib/recipeScaling'; +import ServingsStepper from './ServingsStepper'; import './RecipeDetailPage.css'; -const MIN_SERVINGS = 1; -const MAX_SERVINGS = 20; const MAX_VISIBLE_TAGS = 5; interface RecipeDetailLocationState { @@ -444,40 +437,11 @@ function RecipeDetailContent({ recipe }: RecipeDetailContentProps) {
Ingredient amounts update automatically.
-Ingredient amounts update automatically.
-