From 749898d2c85c000dce4398b0bb73e1b9bdfeb5d5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Oliver=20=C3=98rjavik?= Date: Wed, 7 Oct 2026 10:50:12 +0200 Subject: [PATCH 1/7] feat: add initial RecipeCreatePage Refs: #35 --- .../src/features/recipes/RecipeCreatePage.css | 162 ++++++ .../recipes/RecipeCreatePage.test.tsx | 103 ++++ .../src/features/recipes/RecipeCreatePage.tsx | 512 ++++++++++++++++++ 3 files changed, 777 insertions(+) create mode 100644 apps/web/src/features/recipes/RecipeCreatePage.css create mode 100644 apps/web/src/features/recipes/RecipeCreatePage.test.tsx create mode 100644 apps/web/src/features/recipes/RecipeCreatePage.tsx diff --git a/apps/web/src/features/recipes/RecipeCreatePage.css b/apps/web/src/features/recipes/RecipeCreatePage.css new file mode 100644 index 0000000..ad4a55a --- /dev/null +++ b/apps/web/src/features/recipes/RecipeCreatePage.css @@ -0,0 +1,162 @@ +@layer components { + .recipe-create-page { + padding-block: var(--space-6) var(--space-8); + } + + .recipe-create-page__header { + display: flex; + flex-direction: column; + gap: var(--space-1); + max-width: 42rem; + margin-inline: auto; + text-align: center; + } + + .recipe-create-page__eyebrow { + margin: 0; + color: var(--color-text-accent); + font-size: var(--font-size-sm); + font-weight: var(--font-weight-bold); + letter-spacing: var(--letter-spacing-wide); + text-transform: uppercase; + } + + .recipe-create-page__intro { + color: var(--color-text-secondary); + } + + .recipe-create-page__header h1 { + font-size: var(--font-size-2xl); + } + + .recipe-create-page__content { + display: flex; + flex-direction: column; + gap: var(--space-4); + max-width: 64rem; + margin: var(--space-6) auto 0; + } + + .recipe-create-page__form { + width: 100%; + } + + .recipe-create-page__section { + display: grid; + grid-template-columns: minmax(10rem, 0.6fr) minmax(0, 1.4fr); + gap: var(--space-4); + padding: var(--space-4); + background: var(--color-surface); + border: var(--border-width-thin) solid var(--color-border-default); + border-radius: var(--radius-lg); + box-shadow: var(--shadow-sm); + } + + .recipe-create-page__section h2 { + margin-bottom: var(--space-1); + font-size: var(--font-size-lg); + } + + .recipe-create-page__section-heading { + display: flex; + flex-direction: column; + gap: var(--space-2); + } + + .recipe-create-page__basic-fields, + .recipe-create-page__repeatable-list { + display: flex; + flex-direction: column; + gap: var(--space-3); + } + + .recipe-create-page__image-field { + display: flex; + flex-direction: column; + gap: var(--space-1); + padding: var(--space-3); + border: var(--border-width-thin) dashed var(--color-border-default); + border-radius: var(--radius-md); + } + + .recipe-create-page__image-field label { + font-weight: var(--font-weight-semibold); + } + + .recipe-create-page__image-field p { + font-size: var(--font-size-sm); + } + + .recipe-create-page__details-grid { + display: grid; + grid-template-columns: repeat(3, minmax(0, 1fr)); + gap: var(--space-3); + grid-column: 2; + } + + .recipe-create-page__details-grid + .field, + .recipe-create-page__details-grid + .field + .radio-group { + grid-column: 2; + } + + .recipe-create-page__repeatable-row { + display: flex; + align-items: flex-end; + gap: var(--space-2); + } + + .recipe-create-page__repeatable-row > :first-child { + flex: 1; + min-width: 0; + } + + .recipe-create-page__actions { + display: flex; + justify-content: flex-end; + gap: var(--space-3); + } + + .recipe-create-page__section p { + color: var(--color-text-secondary); + } + + @media (max-width: 40rem) { + .recipe-create-page { + padding-block: var(--space-6) var(--space-8); + } + + .recipe-create-page__content { + margin-top: var(--space-6); + } + + .recipe-create-page__section { + display: flex; + flex-direction: column; + padding: var(--space-3); + } + + .recipe-create-page__details-grid, + .recipe-create-page__details-grid + .field, + .recipe-create-page__details-grid + .field + .radio-group { + grid-column: auto; + } + + .recipe-create-page__details-grid { + display: flex; + flex-direction: column; + } + + .recipe-create-page__repeatable-row { + align-items: stretch; + flex-direction: column; + } + + .recipe-create-page__actions { + flex-direction: column-reverse; + } + + .recipe-create-page__actions .button { + width: 100%; + } + } +} diff --git a/apps/web/src/features/recipes/RecipeCreatePage.test.tsx b/apps/web/src/features/recipes/RecipeCreatePage.test.tsx new file mode 100644 index 0000000..20744b6 --- /dev/null +++ b/apps/web/src/features/recipes/RecipeCreatePage.test.tsx @@ -0,0 +1,103 @@ +import '@testing-library/jest-dom/vitest'; +import { fireEvent, render, screen } from '@testing-library/react'; +import userEvent from '@testing-library/user-event'; +import { describe, expect, it } from 'vitest'; + +import RecipeCreatePage from './RecipeCreatePage'; + +describe('RecipeCreatePage', () => { + it('exposes the recipe fields through accessible labels', () => { + render(); + + expect( + screen.getByRole('heading', { name: 'Add Recipe' }), + ).toBeInTheDocument(); + expect(screen.getByLabelText(/Recipe title/)).toBeInTheDocument(); + expect(screen.getByLabelText(/Short description/)).toBeInTheDocument(); + expect(screen.getByLabelText(/Cooking time/)).toBeInTheDocument(); + expect(screen.getByLabelText(/Servings/)).toBeInTheDocument(); + expect(screen.getByLabelText(/Category/)).toBeInTheDocument(); + expect(screen.getByLabelText(/Tags/)).toBeInTheDocument(); + expect( + screen.getByRole('group', { name: /Visibility/ }), + ).toBeInTheDocument(); + expect(screen.getByLabelText(/Recipe image/)).toBeInTheDocument(); + }); + + it('shows inline errors and focuses the first invalid field', async () => { + const user = userEvent.setup(); + + render(); + await user.click(screen.getByRole('button', { name: 'Publish recipe' })); + + expect(screen.getByText('Enter a recipe title.')).toBeInTheDocument(); + expect( + screen.getByText('Enter a cooking time of at least 1 minute.'), + ).toBeInTheDocument(); + expect(screen.getByText('Choose a recipe category.')).toBeInTheDocument(); + expect(screen.getByText('Add an ingredient.')).toBeInTheDocument(); + expect(screen.getByText('Add an instruction.')).toBeInTheDocument(); + expect(screen.getByLabelText(/Recipe title/)).toHaveAttribute( + 'aria-invalid', + 'true', + ); + }); + + it('adds and removes ingredient and instruction rows', async () => { + const user = userEvent.setup(); + + render(); + + await user.click(screen.getByRole('button', { name: 'Add ingredient' })); + expect(screen.getByLabelText('Ingredient 2')).toBeInTheDocument(); + + await user.click( + screen.getByRole('button', { name: 'Remove ingredient 2' }), + ); + expect(screen.queryByLabelText('Ingredient 2')).not.toBeInTheDocument(); + + await user.click(screen.getByRole('button', { name: 'Add step' })); + expect(screen.getByLabelText('Step 2')).toBeInTheDocument(); + + await user.click(screen.getByRole('button', { name: 'Remove step 2' })); + expect(screen.queryByLabelText('Step 2')).not.toBeInTheDocument(); + }); + + it('reports an invalid image without sending it anywhere', () => { + render(); + fireEvent.change(screen.getByLabelText(/Recipe image/), { + target: { + files: [ + new File(['not an image'], 'recipe.gif', { type: 'image/gif' }), + ], + }, + }); + + expect( + screen.getByText('Choose a JPG, PNG, or WebP image.'), + ).toBeInTheDocument(); + expect(screen.getByLabelText(/Recipe image/)).toHaveAttribute( + 'aria-invalid', + 'true', + ); + }); + + it('shows local success feedback for a valid recipe', async () => { + const user = userEvent.setup(); + + render(); + + await user.type(screen.getByLabelText(/Recipe title/), 'Pasta'); + await user.type(screen.getByLabelText(/Cooking time/), '20'); + await user.type(screen.getByLabelText(/Servings/), '2'); + await user.selectOptions(screen.getByLabelText(/Category/), 'dinner'); + await user.type(screen.getByLabelText('Ingredient 1'), '200g pasta'); + await user.type(screen.getByLabelText('Step 1'), 'Boil the pasta.'); + await user.click(screen.getByRole('button', { name: 'Publish recipe' })); + + expect(screen.getByText('Recipe ready to publish')).toBeInTheDocument(); + expect( + screen.getByText(/No data was sent to a server/), + ).toBeInTheDocument(); + }); +}); diff --git a/apps/web/src/features/recipes/RecipeCreatePage.tsx b/apps/web/src/features/recipes/RecipeCreatePage.tsx new file mode 100644 index 0000000..e415c72 --- /dev/null +++ b/apps/web/src/features/recipes/RecipeCreatePage.tsx @@ -0,0 +1,512 @@ +import { useState, type FormEvent } from 'react'; + +import { DeleteIcon } from '../../icons/actions'; +import Button from '../../components/ui/Button/Button'; +import Field from '../../components/ui/Field/Field'; +import FeedbackMessage from '../../components/ui/FeedbackMessage/FeedbackMessage'; +import IconButton from '../../components/ui/IconButton/IconButton'; +import Radio from '../../components/ui/Radio/Radio'; +import RadioGroup from '../../components/ui/Radio/RadioGroup'; +import Select from '../../components/ui/Select/Select'; +import Textarea from '../../components/ui/Textarea/Textarea'; + +import './RecipeCreatePage.css'; + +type RecipeErrors = { + title?: string; + description?: string; + cookingTime?: string; + servings?: string; + category?: string; + visibility?: string; + image?: string; + ingredients?: string[]; + instructions?: string[]; +}; + +type FormStatus = 'idle' | 'draft' | 'published'; + +const MAX_IMAGE_SIZE = 5 * 1024 * 1024; +const ACCEPTED_IMAGE_TYPES = ['image/jpeg', 'image/png', 'image/webp']; + +function RecipeCreatePage() { + const [title, setTitle] = useState(''); + const [description, setDescription] = useState(''); + const [cookingTime, setCookingTime] = useState(''); + const [servings, setServings] = useState(''); + const [category, setCategory] = useState(''); + const [tags, setTags] = useState(''); + const [visibility, setVisibility] = useState('public'); + const [imageName, setImageName] = useState(''); + const [ingredients, setIngredients] = useState(['']); + const [instructions, setInstructions] = useState(['']); + const [errors, setErrors] = useState({}); + const [status, setStatus] = useState('idle'); + + function clearError(key: keyof RecipeErrors) { + setErrors((currentErrors) => { + const nextErrors = { ...currentErrors }; + delete nextErrors[key]; + return nextErrors; + }); + setStatus('idle'); + } + + function addIngredient() { + setIngredients((currentIngredients) => [...currentIngredients, '']); + } + + function removeIngredient(index: number) { + setIngredients((currentIngredients) => + currentIngredients.filter( + (_, ingredientIndex) => ingredientIndex !== index, + ), + ); + } + + function updateIngredient(index: number, value: string) { + setIngredients((currentIngredients) => + currentIngredients.map((ingredient, ingredientIndex) => + ingredientIndex === index ? value : ingredient, + ), + ); + setErrors((currentErrors) => ({ + ...currentErrors, + ingredients: currentErrors.ingredients?.map((error, errorIndex) => + errorIndex === index ? '' : error, + ), + })); + setStatus('idle'); + } + + function addInstruction() { + setInstructions((currentInstructions) => [...currentInstructions, '']); + } + + function removeInstruction(index: number) { + setInstructions((currentInstructions) => + currentInstructions.filter( + (_, instructionIndex) => instructionIndex !== index, + ), + ); + } + + function updateInstruction(index: number, value: string) { + setInstructions((currentInstructions) => + currentInstructions.map((instruction, instructionIndex) => + instructionIndex === index ? value : instruction, + ), + ); + setErrors((currentErrors) => ({ + ...currentErrors, + instructions: currentErrors.instructions?.map((error, errorIndex) => + errorIndex === index ? '' : error, + ), + })); + setStatus('idle'); + } + + function handleImageChange(file: File | undefined) { + clearError('image'); + + if (!file) { + setImageName(''); + return; + } + + if (!ACCEPTED_IMAGE_TYPES.includes(file.type)) { + setErrors((currentErrors) => ({ + ...currentErrors, + image: 'Choose a JPG, PNG, or WebP image.', + })); + setImageName(''); + return; + } + + if (file.size > MAX_IMAGE_SIZE) { + setErrors((currentErrors) => ({ + ...currentErrors, + image: 'Choose an image smaller than 5 MB.', + })); + setImageName(''); + return; + } + + setImageName(file.name); + } + + function validateForm(): RecipeErrors { + const nextErrors: RecipeErrors = {}; + + if (!title.trim()) nextErrors.title = 'Enter a recipe title.'; + if (description.trim().length > 200) { + nextErrors.description = 'Keep the description under 200 characters.'; + } + if (!cookingTime || Number(cookingTime) < 1) { + nextErrors.cookingTime = 'Enter a cooking time of at least 1 minute.'; + } + if (!servings || Number(servings) < 1) { + nextErrors.servings = 'Enter at least 1 serving.'; + } + if (!category) nextErrors.category = 'Choose a recipe category.'; + if (!visibility) { + nextErrors.visibility = 'Choose who can see this recipe.'; + } + + const ingredientErrors = ingredients.map((ingredient) => + ingredient.trim() ? '' : 'Add an ingredient.', + ); + if (ingredientErrors.some(Boolean)) + nextErrors.ingredients = ingredientErrors; + + const instructionErrors = instructions.map((instruction) => + instruction.trim() ? '' : 'Add an instruction.', + ); + if (instructionErrors.some(Boolean)) { + nextErrors.instructions = instructionErrors; + } + + return nextErrors; + } + + function handleSubmit(event: FormEvent) { + event.preventDefault(); + + const nextErrors = validateForm(); + setErrors(nextErrors); + + if (Object.keys(nextErrors).length > 0) { + const firstInvalidId = [ + 'recipe-title', + 'cooking-time', + 'servings', + 'category', + 'ingredient-0', + 'instruction-0', + ].find((id) => { + if (id === 'ingredient-0') return nextErrors.ingredients?.[0]; + if (id === 'instruction-0') return nextErrors.instructions?.[0]; + return Boolean(nextErrors[id as keyof RecipeErrors]); + }); + + window.setTimeout( + () => + document.getElementById(firstInvalidId ?? 'recipe-title')?.focus(), + 0, + ); + return; + } + + setStatus('published'); + } + + return ( +
+
+

Share with the community

+

Add Recipe

+

+ Share your favorite recipe with the community. +

+
+ +
+
+ {status === 'published' && ( + + Your recipe has been prepared locally. No data was sent to a + server. + + )} + + {status === 'draft' && ( + + Your draft is available in this form until you leave the page. + + )} + +
+
+
+

Basic Information

+

Tell us about your recipe.

+
+ +
+ { + setTitle(event.target.value); + clearError('title'); + }} + required + /> + +