Rallly is an open-source scheduling and collaboration tool designed to make organizing events and meetings easier.
TypeScript i18nextnext-authnextjspostgresqlprismareact-emailt3-stacktailwindcsstrpcturborepotypescriptzod
💄 Send switch to yearly straight to Stripe (#3518)
Luke Vellaee6394c45bf8
diff --git a/apps/web/public/locales/en/app.json b/apps/web/public/locales/en/app.jsonindex 92dace239..977424c86 100644--- a/apps/web/public/locales/en/app.json+++ b/apps/web/public/locales/en/app.json@@ -1076,8 +1076,7 @@"support": "Support","supportDescription": "Need help with anything?","switchToYearly": "Switch to yearly",- "switchToYearlyDescription": "Save {percent}% when you pay yearly.",- "switchToYearlyTitle": "Switch to yearly billing",+ "switchToYearlySave": "Save {amount}","syncCalendar": "Sync calendar","syncCalendarError": "There was an issue syncing your calendar","syncCalendarLoading": "Syncing calendar…",diff --git a/apps/web/src/app/[locale]/(space)/settings/billing/components/pro-plan-card.test.tsx b/apps/web/src/app/[locale]/(space)/settings/billing/components/pro-plan-card.test.tsxindex adca3833e..2ac9e31d2 100644--- a/apps/web/src/app/[locale]/(space)/settings/billing/components/pro-plan-card.test.tsx+++ b/apps/web/src/app/[locale]/(space)/settings/billing/components/pro-plan-card.test.tsx@@ -42,12 +42,6 @@ vi.mock("./cancel-plan-dialog", () => ({),}));-vi.mock("./switch-to-yearly-dialog", () => ({- SwitchToYearlyDialog: ({ children }: { children: React.ReactNode }) => (- <Dialog>{children}</Dialog>- ),-}));-const baseProps = {amount: 1000,currency: "usd",diff --git a/apps/web/src/app/[locale]/(space)/settings/billing/components/pro-plan-card.tsx b/apps/web/src/app/[locale]/(space)/settings/billing/components/pro-plan-card.tsxindex 8fb0d8291..2ed7665dc 100644--- a/apps/web/src/app/[locale]/(space)/settings/billing/components/pro-plan-card.tsx+++ b/apps/web/src/app/[locale]/(space)/settings/billing/components/pro-plan-card.tsx@@ -2,6 +2,7 @@import { mutationOptions } from "@next-safe-action/adapter-tanstack-query";import { posthog } from "@rallly/posthog/client";+import { Badge } from "@rallly/ui/badge";import { Button } from "@rallly/ui/button";import { DialogTrigger, useDialog } from "@rallly/ui/dialog";import { toast } from "@rallly/ui/sonner";@@ -10,6 +11,7 @@ import { TriangleAlertIcon } from "lucide-react";import {openPaymentMethodUpdateAction,resumePlanAction,+ switchToYearlyAction,} from "@/features/billing/actions";import { EarlySupporterBadge } from "@/features/billing/components/early-supporter-badge";import { SubscriptionStatusBadge } from "@/features/billing/components/subscription-status-badge";@@ -32,7 +34,6 @@ import {PlanCardHeader,PlanCardTitle,} from "./plan-card";-import { SwitchToYearlyDialog } from "./switch-to-yearly-dialog";export function ProPlanCard({amount,@@ -73,11 +74,13 @@ export function ProPlanCard({const { t } = useTranslation();const { locale } = useDateTimeConfig();const { formatDateTime } = useDateTime();- const switchToYearlyDialog = useDialog();const cancelPlanDialog = useDialog();const openPaymentMethodUpdate = useMutation(mutationOptions(openPaymentMethodUpdateAction),);+ const switchToYearlyMutation = useMutation(+ mutationOptions(switchToYearlyAction),+ );const resumePlan = useMutation(mutationOptions(resumePlanAction, {onSuccess: () => {@@ -93,14 +96,18 @@ export function ProPlanCard({// Coupons are applied by Stripe at the invoice level, so the stored// discount is applied here to show what the customer is actually charged.+ const applyDiscount = (subtotal: number) => {+ let discounted = subtotal;+ if (discountPercentOff) {+ discounted = Math.round(discounted * (1 - discountPercentOff / 100));+ }+ if (discountAmountOff) {+ discounted = Math.max(0, discounted - discountAmountOff);+ }+ return discounted;+ };const subtotal = amount * seats;- let total = subtotal;- if (discountPercentOff) {- total = Math.round(total * (1 - discountPercentOff / 100));- }- if (discountAmountOff) {- total = Math.max(0, total - discountAmountOff);- }+ const total = applyDiscount(subtotal);const hasDiscount = total !== subtotal;const perMonth = interval === "year" ? Math.round(total / 12) : total;@@ -204,26 +211,28 @@ export function ProPlanCard({</Button>) : null}{switchToYearly && !needsPayment ? (- <SwitchToYearlyDialog- {...switchToYearlyDialog.dialogProps}- monthlyAmount={switchToYearly.monthlyAmount}- yearlyAmount={switchToYearly.yearlyAmount}+ <Button+ loading={switchToYearlyMutation.isPending}+ onClick={() => {+ posthog?.capture("space_billing:change_plan_click");+ switchToYearlyMutation.mutate();+ }}>- <DialogTrigger- render={- <Button- onClick={() => {- posthog?.capture("space_billing:change_plan_click");- }}- />- }- >+ <Trans i18nKey="switchToYearly" defaults="Switch to yearly" />+ <Badge variant="green"><Trans- i18nKey="switchToYearly"- defaults="Switch to yearly"+ i18nKey="switchToYearlySave"+ defaults="Save {amount}"+ values={{+ amount: formatCurrency(+ applyDiscount(switchToYearly.monthlyAmount * seats) *+ 12 -+ applyDiscount(switchToYearly.yearlyAmount * seats),+ ),+ }}/>- </DialogTrigger>- </SwitchToYearlyDialog>+ </Badge>+ </Button>) : null}<ManageSeatsDialog usedSeats={usedSeats} currentSeats={seats}><DialogTriggerdiff --git a/apps/web/src/app/[locale]/(space)/settings/billing/components/switch-to-yearly-dialog.tsx b/apps/web/src/app/[locale]/(space)/settings/billing/components/switch-to-yearly-dialog.tsxdeleted file mode 100644index c450e8d89..000000000--- a/apps/web/src/app/[locale]/(space)/settings/billing/components/switch-to-yearly-dialog.tsx+++ /dev/null@@ -1,72 +0,0 @@-"use client";--import { mutationOptions } from "@next-safe-action/adapter-tanstack-query";-import { yearlySavingsPercent } from "@rallly/billing/pricing";-import { Button } from "@rallly/ui/button";-import type { DialogProps } from "@rallly/ui/dialog";-import {- Dialog,- DialogClose,- DialogContent,- DialogDescription,- DialogFooter,- DialogHeader,- DialogTitle,-} from "@rallly/ui/dialog";-import { useMutation } from "@tanstack/react-query";-import { switchToYearlyAction } from "@/features/billing/actions";-import { Trans } from "@/i18n/client";--export function SwitchToYearlyDialog({- children,- monthlyAmount,- yearlyAmount,- ...dialogProps-}: DialogProps & {- /** Per seat monthly amount in the currency's minor unit. */- monthlyAmount: number;- /** Per seat yearly amount in the currency's minor unit. */- yearlyAmount: number;-}) {- const switchToYearly = useMutation(mutationOptions(switchToYearlyAction));-- return (- <Dialog {...dialogProps}>- {children}- <DialogContent size="sm">- <DialogHeader>- <DialogTitle>- <Trans- i18nKey="switchToYearlyTitle"- defaults="Switch to yearly billing"- />- </DialogTitle>- <DialogDescription>- <Trans- i18nKey="switchToYearlyDescription"- defaults="Save {percent}% when you pay yearly."- values={{- percent: yearlySavingsPercent({- monthly: monthlyAmount,- yearly: yearlyAmount,- }),- }}- />- </DialogDescription>- </DialogHeader>- <DialogFooter>- <DialogClose render={<Button />}>- <Trans i18nKey="cancel" defaults="Cancel" />- </DialogClose>- <Button- variant="primary"- loading={switchToYearly.isPending}- onClick={() => switchToYearly.mutate()}- >- <Trans i18nKey="switchToYearly" defaults="Switch to yearly" />- </Button>- </DialogFooter>- </DialogContent>- </Dialog>- );-}