-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat (session): optimistic updates for lip dnd
- Loading branch information
Showing
10 changed files
with
302 additions
and
93 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,53 @@ | ||
import { api } from '@repo/api/client' | ||
import { Session } from '@repo/db' | ||
import Button from '@repo/ui/components/Button' | ||
import { cn } from '@repo/ui/helpers' | ||
import { CirclePlus } from 'lucide-react' | ||
import { useEffect, useState } from 'react' | ||
|
||
export default function AddDemoLipButton({ | ||
session, | ||
}: Readonly<{ session: Session }>) { | ||
const { isFetching } = api.lip.getBySessionId.useQuery({ | ||
id: session.id, | ||
}) | ||
|
||
const utils = api.useUtils() | ||
|
||
const { mutate: createDemoLip, isPending } = api.lip.createDemo.useMutation( | ||
{ | ||
const { mutate: createDemoLip, isPending: isCreateDemoPending } = | ||
api.lip.createDemo.useMutation({ | ||
onSettled: () => { | ||
void utils.lip.getBySessionId.invalidate({ id: session.id }) | ||
}, | ||
}, | ||
) | ||
}) | ||
|
||
const handleAddButtonClick = () => { | ||
void createDemoLip({ id: session.id }) | ||
} | ||
|
||
const [isPending, setIsPending] = useState(false) | ||
|
||
useEffect(() => { | ||
if (!isFetching && !isCreateDemoPending) { | ||
setIsPending(false) | ||
} | ||
if (isCreateDemoPending) { | ||
setIsPending(true) | ||
} | ||
}, [isFetching, isCreateDemoPending]) | ||
|
||
return ( | ||
<Button | ||
variant='ghost' | ||
title='Add demo lip' | ||
disabled={isPending} | ||
onClick={handleAddButtonClick} | ||
> | ||
<CirclePlus className='h-12 w-12 fill-pink-600 text-black' /> | ||
<CirclePlus | ||
className={cn('h-12 w-12 fill-pink-600 text-black', { | ||
'animate-spin': isPending, | ||
})} | ||
/> | ||
</Button> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.