-
-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
example of a better pattern for setting the active protocol
- Loading branch information
Showing
10 changed files
with
207 additions
and
111 deletions.
There are no files selected for viewing
72 changes: 72 additions & 0 deletions
72
app/(dashboard)/dashboard/_components/ProtocolsTable/ActiveButton.tsx
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 |
---|---|---|
@@ -0,0 +1,72 @@ | ||
'use client'; | ||
import { api } from '~/trpc/client'; | ||
import { BadgeCheck } from 'lucide-react'; | ||
|
||
const ActiveButton = ({ | ||
active, | ||
protocolId, | ||
}: { | ||
active: boolean; | ||
protocolId: string; | ||
}) => { | ||
const utils = api.useUtils(); | ||
|
||
const { mutateAsync: setActiveProtocol } = | ||
api.protocol.active.set.useMutation({ | ||
// Optimistic update | ||
onMutate: async (newActiveProtocolId: string) => { | ||
await utils.protocol.get.all.cancel(); | ||
await utils.protocol.active.get.cancel(); | ||
|
||
const protocolGetAll = utils.protocol.get.all.getData(); | ||
const protocolActiveGet = utils.protocol.active.get.getData(); | ||
|
||
utils.protocol.active.get.setData(undefined, protocolId); | ||
utils.protocol.get.all.setData( | ||
undefined, | ||
(protocolGetAll) => | ||
protocolGetAll?.map((protocol) => { | ||
if (protocol.id === newActiveProtocolId) { | ||
return { | ||
...protocol, | ||
active: true, | ||
}; | ||
} | ||
|
||
return { | ||
...protocol, | ||
active: false, | ||
}; | ||
}), | ||
); | ||
|
||
return { protocolGetAll, protocolActiveGet }; | ||
}, | ||
onSettled: () => { | ||
void utils.protocol.get.all.invalidate(); | ||
void utils.protocol.active.get.invalidate(); | ||
}, | ||
onError: (_error, _newActiveProtocolId, context) => { | ||
utils.protocol.get.all.setData(undefined, context?.protocolGetAll); | ||
utils.protocol.active.get.setData( | ||
undefined, | ||
context?.protocolActiveGet, | ||
); | ||
}, | ||
}); | ||
|
||
if (active) { | ||
return <BadgeCheck className="fill-white text-purple-500" />; | ||
} | ||
|
||
return ( | ||
<button | ||
title="Make active..." | ||
onClick={() => setActiveProtocol(protocolId)} | ||
> | ||
<BadgeCheck className="cursor-pointer fill-white text-primary/20 hover:scale-150 hover:fill-purple-500 hover:text-white" /> | ||
</button> | ||
); | ||
}; | ||
|
||
export default ActiveButton; |
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
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 |
---|---|---|
@@ -0,0 +1,8 @@ | ||
// Display options for dates: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/DateTimeFormat#using_options | ||
export const dateOptions: Intl.DateTimeFormatOptions = { | ||
year: 'numeric', | ||
month: 'numeric', | ||
day: 'numeric', | ||
hour: 'numeric', | ||
minute: 'numeric', | ||
}; |
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 |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import * as React from "react" | ||
import { cva, type VariantProps } from "class-variance-authority" | ||
|
||
import { cn } from "~/utils/shadcn" | ||
|
||
const badgeVariants = cva( | ||
"inline-flex items-center rounded-full border px-2.5 py-0.5 text-xs font-semibold transition-colors focus:outline-none focus:ring-2 focus:ring-ring focus:ring-offset-2", | ||
{ | ||
variants: { | ||
variant: { | ||
default: | ||
"border-transparent bg-primary text-primary-foreground hover:bg-primary/80", | ||
secondary: | ||
"border-transparent bg-secondary text-secondary-foreground hover:bg-secondary/80", | ||
destructive: | ||
"border-transparent bg-destructive text-destructive-foreground hover:bg-destructive/80", | ||
outline: "text-foreground", | ||
}, | ||
}, | ||
defaultVariants: { | ||
variant: "default", | ||
}, | ||
} | ||
) | ||
|
||
export interface BadgeProps | ||
extends React.HTMLAttributes<HTMLDivElement>, | ||
VariantProps<typeof badgeVariants> {} | ||
|
||
function Badge({ className, variant, ...props }: BadgeProps) { | ||
return ( | ||
<div className={cn(badgeVariants({ variant }), className)} {...props} /> | ||
) | ||
} | ||
|
||
export { Badge, badgeVariants } |
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 +0,0 @@ | ||
|
||
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.