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 */} + + Pet name + + {errors.name && ( + {errors.name.message} + )} + + + {/* Species selector */} + + Species + ( + { + const newVal = val ?? ""; + field.onChange(newVal); + setSelectedSpeciesId(newVal); + // Reset breed when species changes (species and breed are coupled) + setValue("breedId", null); + }} + > + + + + + {speciesList.map((species) => ( + + {species.name} + + ))} + + + )} + /> + {errors.speciesId && ( + + {errors.speciesId.message} + + )} + + + {/* Breed selector */} + + Breed + ( + { + field.onChange(val ?? null); + }} + disabled={!selectedSpeciesId} + > + + + + + {breedList.length === 0 ? ( + + No breeds found for this species. + + ) : ( + breedList.map((breed) => ( + + {breed.name} + + )) + )} + + + )} + /> + {errors.breedId && ( + {errors.breedId.message} + )} + + + {/* Bio */} + + Bio + + + {(bioValue ?? "").length}/150 + + {errors.bio && ( + {errors.bio.message} + )} + + + {/* Adoption story */} + + Adoption story + + + {(adoptionStoryValue ?? "").length}/500 + + {errors.adoptionStory && ( + + {errors.adoptionStory.message} + + )} + + + {/* Submit CTA */} + + {isLoading && } + {mode === "create" ? "Add Pet" : "Save changes"} + + + ); +}
+ {pet.name} +
{errors.name.message}
+ {errors.speciesId.message} +
{errors.breedId.message}
{errors.bio.message}
+ {errors.adoptionStory.message} +