-
Notifications
You must be signed in to change notification settings - Fork 61
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(saas): Added Pause, Cancel, Resume current subscription on billi…
…ng page
- Loading branch information
1 parent
2c95e82
commit c49b4a6
Showing
3 changed files
with
247 additions
and
15 deletions.
There are no files selected for viewing
112 changes: 112 additions & 0 deletions
112
starterkits/saas/src/app/(app)/(user)/org/billing/_components/cancel-pause-resume-btns.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,112 @@ | ||
"use client"; | ||
|
||
import { Button } from "@/components/ui/button"; | ||
import { | ||
cancelPlan, | ||
pausePlan, | ||
resumePlan, | ||
} from "@/server/actions/plans/mutations"; | ||
import { type getOrgSubscription } from "@/server/actions/plans/query"; | ||
import { useMutation } from "@tanstack/react-query"; | ||
import { useRouter } from "next/navigation"; | ||
import { toast } from "sonner"; | ||
import { useAwaitableTransition } from "@/hooks/use-awaitable-transition"; | ||
import { Icons } from "@/components/ui/icons"; | ||
|
||
type CancelAndPauseBtnProps = { | ||
subscription: Awaited<ReturnType<typeof getOrgSubscription>>; | ||
}; | ||
|
||
export function CancelPauseResumeBtns({ | ||
subscription, | ||
}: CancelAndPauseBtnProps) { | ||
const router = useRouter(); | ||
|
||
const [, startAwaitableTransition] = useAwaitableTransition(); | ||
|
||
const { isPending: isCancelling, mutate: cancelMutate } = useMutation({ | ||
mutationFn: async () => { | ||
const response = await cancelPlan(); | ||
await startAwaitableTransition(() => { | ||
router.refresh(); | ||
}); | ||
return response; | ||
}, | ||
onError: () => { | ||
toast.error("Failed to cancel plan"); | ||
}, | ||
onSuccess: () => { | ||
toast.success("Plan cancelled successfully"); | ||
}, | ||
}); | ||
|
||
const { isPending: isResuming, mutate: resumeMutate } = useMutation({ | ||
mutationFn: async () => { | ||
const response = await resumePlan(); | ||
await startAwaitableTransition(() => { | ||
router.refresh(); | ||
}); | ||
return response; | ||
}, | ||
onError: () => { | ||
toast.error("Failed to resume plan"); | ||
}, | ||
onSuccess: () => { | ||
toast.success("Plan resumed successfully"); | ||
}, | ||
}); | ||
|
||
const { isPending: isPausing, mutate: pauseMutate } = useMutation({ | ||
mutationFn: async () => { | ||
const response = await pausePlan(); | ||
await startAwaitableTransition(() => { | ||
router.refresh(); | ||
}); | ||
return response; | ||
}, | ||
onError: () => { | ||
toast.error("Failed to pause plan"); | ||
}, | ||
onSuccess: () => { | ||
toast.success("Plan paused successfully"); | ||
}, | ||
}); | ||
|
||
const isAllActionsPending = isCancelling || isResuming || isPausing; | ||
|
||
if (!subscription) return null; | ||
|
||
if (subscription.status === "active") { | ||
return ( | ||
<div className="flex items-center gap-2"> | ||
<Button | ||
disabled={isAllActionsPending} | ||
onClick={() => pauseMutate()} | ||
variant="outline" | ||
> | ||
{isPausing && <Icons.loader className="mr-2 h-4 w-4" />} | ||
Pause Plan | ||
</Button> | ||
<Button | ||
onClick={() => cancelMutate()} | ||
disabled={isAllActionsPending} | ||
variant="destructive" | ||
> | ||
{isCancelling && <Icons.loader className="mr-2 h-4 w-4" />} | ||
Cancel Plan | ||
</Button> | ||
</div> | ||
); | ||
} | ||
|
||
return ( | ||
<Button | ||
disabled={isAllActionsPending} | ||
onClick={() => resumeMutate()} | ||
variant="outline" | ||
> | ||
{isResuming && <Icons.loader className="mr-2 h-4 w-4" />} | ||
Resume Plan | ||
</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