-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Disable the connect/disconnect button while an action is in progress
- Loading branch information
Showing
3 changed files
with
63 additions
and
57 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
81 changes: 42 additions & 39 deletions
81
app/(main)/settings/components/github-connection-button.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 |
---|---|---|
@@ -1,43 +1,46 @@ | ||
"use client"; | ||
|
||
import { Button } from "@/components/ui/button"; | ||
import { cn } from "@/lib/utils"; | ||
import { useActionState } from "react"; | ||
|
||
type ButtonWithActionProps = { | ||
action: () => Promise<void>; | ||
}; | ||
|
||
export type GitHubConnectButtonMode = "connect" | "disconnect" | "hidden"; | ||
const buttonClassNames = "w-fit font-avenir text-sm font-medium"; | ||
|
||
export function GitHubConnectButton({ | ||
mode, | ||
connectAction, | ||
disconnectAction, | ||
}: { | ||
mode: GitHubConnectButtonMode; | ||
connectAction: () => Promise<void>; | ||
disconnectAction: () => Promise<void>; | ||
}) { | ||
const className = "w-fit font-avenir text-sm font-medium"; | ||
switch (mode) { | ||
case "connect": | ||
return ( | ||
<form> | ||
<Button | ||
className={className} | ||
type="submit" | ||
formAction={connectAction} | ||
> | ||
Connect | ||
</Button> | ||
</form> | ||
); | ||
case "disconnect": | ||
return ( | ||
<form> | ||
<Button | ||
className={className} | ||
type="submit" | ||
formAction={disconnectAction} | ||
> | ||
Disconnect | ||
</Button> | ||
</form> | ||
); | ||
case "hidden": | ||
return null; | ||
} | ||
export function GitHubConnectButton({ action }: ButtonWithActionProps) { | ||
const [_, formAction, isPending] = useActionState(async () => { | ||
await action(); | ||
}, null); | ||
return ( | ||
<form> | ||
<Button | ||
className={buttonClassNames} | ||
type="submit" | ||
formAction={formAction} | ||
disabled={isPending} | ||
> | ||
Connect | ||
</Button> | ||
</form> | ||
); | ||
} | ||
export function GitHubDisconnectButton({ action }: ButtonWithActionProps) { | ||
const [_, formAction, isPending] = useActionState(async () => { | ||
await action(); | ||
}, null); | ||
return ( | ||
<form> | ||
<Button | ||
className={cn(buttonClassNames, "text-red-500")} | ||
type="submit" | ||
formAction={formAction} | ||
disabled={isPending} | ||
> | ||
Disconnect | ||
</Button> | ||
</form> | ||
); | ||
} |
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