From 63d8c06b481762c7f7c30076d9b6088aa70c63a2 Mon Sep 17 00:00:00 2001 From: neodk2004 Date: Sat, 6 Jun 2026 10:11:54 +0200 Subject: [PATCH] feat(01-03): add PetForm and PetCard components - PetForm: create/edit mode, species/breed from DB (never hardcoded) - Species selector wired to listSpecies tRPC query - Breed selector wired to listBreeds, resets on species change - Bio (maxLength=150) + adoption story (maxLength=500) with char counters - Pet name maxLength=30 with Zod validation via react-hook-form - AvatarUploadZone stub (Plan 04 wires upload; disabled in create mode) - PetCard: avatar with active orange ring, species badge, hover shadow --- src/components/pet/PetCard.tsx | 88 +++++++++ src/components/pet/PetForm.tsx | 338 +++++++++++++++++++++++++++++++++ 2 files changed, 426 insertions(+) create mode 100644 src/components/pet/PetCard.tsx create mode 100644 src/components/pet/PetForm.tsx diff --git a/src/components/pet/PetCard.tsx b/src/components/pet/PetCard.tsx new file mode 100644 index 0000000..5185f3a --- /dev/null +++ b/src/components/pet/PetCard.tsx @@ -0,0 +1,88 @@ +"use client"; + +import { Card, CardContent } from "@/components/ui/card"; +import { Avatar, AvatarFallback, AvatarImage } from "@/components/ui/avatar"; +import { Badge } from "@/components/ui/badge"; + +/** + * PetCard — renders a pet's identity card for the My Pets grid. + * Used in the /pets page for selecting and managing multiple pets. + * When isActive, displays an orange ring to indicate the active pet. + */ + +interface PetCardProps { + pet: { + id: string; + name: string; + avatarKey?: string | null; + species: { id: string; name: string }; + breed: { id: string; name: string } | null; + }; + isActive: boolean; + onSelect: (petId: string) => void; +} + +function getInitials(name: string): string { + return name.slice(0, 2).toUpperCase(); +} + +export function PetCard({ pet, isActive, onSelect }: PetCardProps) { + return ( + onSelect(pet.id)} + role="button" + tabIndex={0} + onKeyDown={(e) => { + if (e.key === "Enter" || e.key === " ") { + e.preventDefault(); + onSelect(pet.id); + } + }} + aria-pressed={isActive} + aria-label={`${pet.name}${isActive ? " (active)" : ""}`} + > + + {/* Avatar with active indicator */} +
+ + {pet.avatarKey && ( + + )} + + {getInitials(pet.name)} + + + {isActive && ( +
+ + {/* Pet name */} +

+ {pet.name} +

+ + {/* Species badge */} + {pet.species.name} + + {/* Active label */} + {isActive && ( + Active + )} +
+
+ ); +} diff --git a/src/components/pet/PetForm.tsx b/src/components/pet/PetForm.tsx new file mode 100644 index 0000000..5afef81 --- /dev/null +++ b/src/components/pet/PetForm.tsx @@ -0,0 +1,338 @@ +"use client"; + +import { useState } from "react"; +import { useForm, Controller } from "react-hook-form"; +import { zodResolver } from "@hookform/resolvers/zod"; +import { z } from "zod"; +import { toast } from "sonner"; +import { Loader2, Camera } from "lucide-react"; +import { useQuery, useMutation, useQueryClient } from "@tanstack/react-query"; +import { useTRPC } from "@/trpc/client"; +import { Button } from "@/components/ui/button"; +import { Input } from "@/components/ui/input"; +import { Label } from "@/components/ui/label"; +import { Textarea } from "@/components/ui/textarea"; +import { + Select, + SelectContent, + SelectItem, + SelectTrigger, + SelectValue, +} from "@/components/ui/select"; + +/** + * PetForm — reusable create/edit form for pet profiles. + * + * Covers PET-01 (create) and PET-02 (edit) requirements. + * Species and breeds are fetched from DB — never hardcoded (anti-pattern prevention). + * Zod schema enforces character limits at the form layer (server-side also enforces via tRPC T-3-02). + * Avatar upload zone stub is rendered; wired in Plan 04. + */ + +const petFormSchema = z.object({ + name: z + .string() + .min(1, "This field is required.") + .max(30, "Pet name must be 30 characters or fewer."), + speciesId: z.string().min(1, "Please select a species."), + breedId: z.string().nullable().optional(), + bio: z + .string() + .max(150, "Bio must be 150 characters or fewer.") + .optional() + .nullable(), + adoptionStory: z + .string() + .max(500, "Adoption story must be 500 characters or fewer.") + .optional() + .nullable(), +}); + +type PetFormValues = z.infer; + +interface PetFormProps { + mode: "create" | "edit"; + initialValues?: Partial; + petId?: string; + onSuccess?: (petId: string) => void; +} + +/** + * Avatar upload zone stub — Plan 04 will wire the actual upload logic. + * In create mode, petId is undefined so upload is disabled. + */ +function AvatarUploadZone({ petId }: { petId?: string }) { + const isDisabled = !petId; + + return ( +
+ + Upload photo + {isDisabled && ( + + Save your pet first to upload a photo. + + )} +
+ ); +} + +export function PetForm({ + mode, + initialValues, + petId, + onSuccess, +}: PetFormProps) { + const trpc = useTRPC(); + const queryClient = useQueryClient(); + const [selectedSpeciesId, setSelectedSpeciesId] = useState( + initialValues?.speciesId ?? "" + ); + + const { data: speciesList = [] } = useQuery({ + ...trpc.pets.listSpecies.queryOptions(), + staleTime: Infinity, + }); + + const { data: breedList = [] } = useQuery({ + ...trpc.pets.listBreeds.queryOptions( + { speciesId: selectedSpeciesId || "__none__" }, + ), + enabled: !!selectedSpeciesId, + staleTime: Infinity, + }); + + const createMutation = useMutation({ + ...trpc.pets.create.mutationOptions(), + onSuccess: (pet) => { + void queryClient.invalidateQueries(); + toast.success("Pet profile created"); + onSuccess?.(pet.id); + }, + onError: () => { + toast.error("Something went wrong. Please try again."); + }, + }); + + const updateMutation = useMutation({ + ...trpc.pets.update.mutationOptions(), + onSuccess: () => { + void queryClient.invalidateQueries(); + toast.success("Changes saved"); + onSuccess?.(petId!); + }, + onError: () => { + toast.error("Something went wrong. Please try again."); + }, + }); + + const isLoading = createMutation.isPending || updateMutation.isPending; + + const { + register, + handleSubmit, + control, + watch, + setValue, + formState: { errors }, + } = useForm({ + resolver: zodResolver(petFormSchema), + defaultValues: { + name: initialValues?.name ?? "", + speciesId: initialValues?.speciesId ?? "", + breedId: initialValues?.breedId ?? null, + bio: initialValues?.bio ?? "", + adoptionStory: initialValues?.adoptionStory ?? "", + }, + }); + + const petName = watch("name"); + const bioValue = watch("bio") ?? ""; + const adoptionStoryValue = watch("adoptionStory") ?? ""; + + const onSubmit = (data: PetFormValues) => { + if (mode === "create") { + createMutation.mutate({ + name: data.name, + speciesId: data.speciesId, + breedId: data.breedId ?? undefined, + bio: data.bio ?? undefined, + adoptionStory: data.adoptionStory ?? undefined, + }); + } else { + updateMutation.mutate({ + petId: petId!, + name: data.name, + breedId: data.breedId ?? null, + bio: data.bio ?? null, + adoptionStory: data.adoptionStory ?? null, + }); + } + }; + + return ( +
+ {/* Avatar upload zone (stub — Plan 04 wires actual upload) */} +
+ +
+ + {/* Pet name */} +
+ + + {errors.name && ( +

{errors.name.message}

+ )} +
+ + {/* Species selector */} +
+ + ( + + )} + /> + {errors.speciesId && ( +

+ {errors.speciesId.message} +

+ )} +
+ + {/* Breed selector */} +
+ + ( + + )} + /> + {errors.breedId && ( +

{errors.breedId.message}

+ )} +
+ + {/* Bio */} +
+ +