1- import { usePage } from '@inertiajs/react' ;
1+ import { useForm , usePage } from '@inertiajs/react' ;
22import { Heart , LoaderCircle , X } from 'lucide-react' ;
3- import { Dispatch , SetStateAction , useState } from 'react' ;
4- import { toggleLikedStatus } from '../queries' ;
53import { Puppy , SharedData } from '../types' ;
64
7- export function Shortlist ( { puppies, setPuppies } : { puppies : Puppy [ ] ; setPuppies : Dispatch < SetStateAction < Puppy [ ] > > } ) {
5+ export function Shortlist ( { puppies } : { puppies : Puppy [ ] } ) {
86 const { auth } = usePage < SharedData > ( ) . props ;
97 return (
108 < div >
@@ -22,34 +20,33 @@ export function Shortlist({ puppies, setPuppies }: { puppies: Puppy[]; setPuppie
2220 >
2321 < img height = { 32 } width = { 32 } alt = { puppy . name } className = "aspect-square w-8 object-cover" src = { puppy . imageUrl } />
2422 < p className = "px-3 text-sm text-slate-800" > { puppy . name } </ p >
25- < DeleteButton id = { puppy . id } setPuppies = { setPuppies } />
23+ < DeleteButton id = { puppy . id } />
2624 </ li >
2725 ) ) }
2826 </ ul >
2927 </ div >
3028 ) ;
3129}
3230
33- function DeleteButton ( { id, setPuppies } : { id : Puppy [ 'id' ] ; setPuppies : Dispatch < SetStateAction < Puppy [ ] > > } ) {
34- const [ pending , setPending ] = useState ( false ) ;
31+ function DeleteButton ( { id } : { id : Puppy [ 'id' ] } ) {
32+ const { processing , patch } = useForm ( ) ;
3533 return (
36- < button
37- onClick = { async ( ) => {
38- setPending ( true ) ;
39- const updatedPuppy = await toggleLikedStatus ( id ) ;
40- setPuppies ( ( prevPups ) => {
41- return prevPups . map ( ( existingPuppy ) => ( existingPuppy . id === updatedPuppy . id ? updatedPuppy : existingPuppy ) ) ;
34+ < form
35+ className = "h-full"
36+ onSubmit = { ( e ) => {
37+ e . preventDefault ( ) ;
38+ patch ( route ( 'puppies.like' , id ) , {
39+ preserveScroll : true ,
4240 } ) ;
43- setPending ( false ) ;
4441 } }
45- className = "group h-full border-l border-slate-100 px-2 hover:bg-slate-100"
46- disabled = { pending }
4742 >
48- { pending ? (
49- < LoaderCircle className = "size-4 animate-spin stroke-slate-300" />
50- ) : (
51- < X className = "size-4 stroke-slate-400 group-hover:stroke-red-400" />
52- ) }
53- </ button >
43+ < button type = "submit" className = "group h-full border-l border-slate-100 px-2 hover:bg-slate-100" disabled = { processing } >
44+ { processing ? (
45+ < LoaderCircle className = "size-4 animate-spin stroke-slate-300" />
46+ ) : (
47+ < X className = "size-4 stroke-slate-400 group-hover:stroke-red-400" />
48+ ) }
49+ </ button >
50+ </ form >
5451 ) ;
5552}
0 commit comments