MCPcopy
hub / github.com/shadcn-ui/taxonomy / PostCreateButton

Function PostCreateButton

components/post-create-button.tsx:13–81  ·  view source on GitHub ↗
({
  className,
  variant,
  ...props
}: PostCreateButtonProps)

Source from the content-addressed store, hash-verified

11interface PostCreateButtonProps extends ButtonProps {}
12
13export function PostCreateButton({
14 className,
15 variant,
16 ...props
17}: PostCreateButtonProps) {
18 const router = useRouter()
19 const [isLoading, setIsLoading] = React.useState<boolean>(false)
20
21 async function onClick() {
22 setIsLoading(true)
23
24 const response = await fetch("/api/posts", {
25 method: "POST",
26 headers: {
27 "Content-Type": "application/json",
28 },
29 body: JSON.stringify({
30 title: "Untitled Post",
31 }),
32 })
33
34 setIsLoading(false)
35
36 if (!response?.ok) {
37 if (response.status === 402) {
38 return toast({
39 title: "Limit of 3 posts reached.",
40 description: "Please upgrade to the PRO plan.",
41 variant: "destructive",
42 })
43 }
44
45 return toast({
46 title: "Something went wrong.",
47 description: "Your post was not created. Please try again.",
48 variant: "destructive",
49 })
50 }
51
52 const post = await response.json()
53
54 // This forces a cache invalidation.
55 router.refresh()
56
57 router.push(`/editor/${post.id}`)
58 }
59
60 return (
61 <button
62 onClick={onClick}
63 className={cn(
64 buttonVariants({ variant }),
65 {
66 "cursor-not-allowed opacity-60": isLoading,
67 },
68 className
69 )}
70 disabled={isLoading}

Callers

nothing calls this directly

Calls 1

cnFunction · 0.90

Tested by

no test coverage detected