Skip to content

armgit / dub

Public Imported from GitHub
0

The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.

Group discount edit disabled (#3945)

Pedro Ladeiraa91b241a9c5c
diff --git a/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/groups/[groupSlug]/discounts/group-discounts.tsx b/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/groups/[groupSlug]/discounts/group-discounts.tsx
index 99c182516f..3f28c8157d 100644
--- a/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/groups/[groupSlug]/discounts/group-discounts.tsx
+++ b/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/groups/[groupSlug]/discounts/group-discounts.tsx
@@ -3,7 +3,10 @@
import useGroup from "@/lib/swr/use-group";
import type { DiscountProps, GroupProps } from "@/lib/types";
import { DEFAULT_PARTNER_GROUP } from "@/lib/zod/schemas/groups";
-import { useDiscountSheet } from "@/ui/partners/discounts/add-edit-discount-sheet";
+import {
+ DiscountSheet,
+ useDiscountSheet,
+} from "@/ui/partners/discounts/add-edit-discount-sheet";
import { ProgramRewardDescription } from "@/ui/partners/program-reward-description";
import { Button, useRouterStuff } from "@dub/ui";
import { cn, isClickOnInteractiveChild } from "@dub/utils";
@@ -37,19 +40,18 @@ export const GroupDiscounts = () => {
? group.discount
: undefined;
const isNewDiscount = discountSheetState.discountId === "new";
- const { DiscountSheet, setIsOpen: setDiscountSheetOpen } = useDiscountSheet({
- ...(currentDiscount && { discount: currentDiscount }),
- });
-
- useEffect(() => {
- setDiscountSheetOpen(discountSheetState.open);
- }, [discountSheetState.open, setDiscountSheetOpen]);
return (
<div>
- {discountSheetState.discountId &&
- (currentDiscount || isNewDiscount) &&
- DiscountSheet}
+ {discountSheetState.discountId && (currentDiscount || isNewDiscount) && (
+ <DiscountSheet
+ isOpen={discountSheetState.open}
+ setIsOpen={(open) =>
+ setDiscountSheetState((s) => ({ ...s, open }) as typeof s)
+ }
+ {...(currentDiscount && { discount: currentDiscount })}
+ />
+ )}
{loading || !group ? (
<DiscountSkeleton />
diff --git a/apps/web/ui/partners/discounts/add-edit-discount-sheet.tsx b/apps/web/ui/partners/discounts/add-edit-discount-sheet.tsx
index 1521686a58..7510583f2a 100644
--- a/apps/web/ui/partners/discounts/add-edit-discount-sheet.tsx
+++ b/apps/web/ui/partners/discounts/add-edit-discount-sheet.tsx
@@ -20,7 +20,14 @@ import {
} from "@/ui/shared/inline-badge-popover";
import { UpgradeRequiredToast } from "@/ui/shared/upgrade-required-toast";
import { DiscountProvider } from "@dub/prisma/client";
-import { Button, InfoTooltip, Sheet, Switch, useRouterStuff } from "@dub/ui";
+import {
+ Button,
+ InfoTooltip,
+ Sheet,
+ Switch,
+ Tooltip,
+ useRouterStuff,
+} from "@dub/ui";
import { CircleCheckFill, StripeIcon, Tag } from "@dub/ui/icons";
import { capitalize, cn, pluralize } from "@dub/utils";
import { useAction } from "next-safe-action/hooks";
@@ -76,10 +83,14 @@ function DiscountSheetContent({
const { mutate: mutateProgram } = useProgram();
const { id: workspaceId, defaultProgramId } = useWorkspace();
- const [useExistingCoupon, setUseExistingCoupon] = useState(false);
+ const isEdit = Boolean(discount?.id);
+
+ const [useExistingCoupon, setUseExistingCoupon] = useState(
+ Boolean(discount?.couponId || defaultDiscountValues?.couponId),
+ );
const [useStripeTestCouponId, setUseStripeTestCouponId] = useState(
- Boolean(discount?.couponTestId),
+ Boolean(discount?.couponTestId ?? defaultDiscountValues?.couponTestId),
);
const discountProvider =
@@ -124,6 +135,12 @@ function DiscountSheetContent({
"provider",
]);
+ const effectiveProvider = isEdit ? discountProvider : provider;
+
+ const showStripeCouponFields =
+ effectiveProvider === DiscountProvider.stripe &&
+ (isEdit || useExistingCoupon);
+
const { executeAsync: createDiscount, isPending: isCreating } = useAction(
createDiscountAction,
{
@@ -276,7 +293,7 @@ function DiscountSheetContent({
{...(() => {
const { onChange: onProviderChange, ...rest } =
register("provider", {
- disabled: Boolean(discount),
+ disabled: isEdit,
});
return {
@@ -302,7 +319,7 @@ function DiscountSheetContent({
</div>
</div>
- {!discount && provider === DiscountProvider.stripe && (
+ {!isEdit && effectiveProvider === DiscountProvider.stripe && (
<div className="grid grid-cols-1 gap-3 p-px lg:grid-cols-2">
{COUPON_CREATION_OPTIONS.map(
({ label, description, useExisting }) => {
@@ -326,6 +343,11 @@ function DiscountSheetContent({
onChange={(e) => {
if (e.target.checked) {
setUseExistingCoupon(useExisting);
+ if (!useExisting) {
+ setValue("couponId", "");
+ setValue("couponTestId", "");
+ setUseStripeTestCouponId(false);
+ }
}
}}
/>
@@ -346,73 +368,74 @@ function DiscountSheetContent({
</div>
)}
- {(useExistingCoupon || discount) &&
- provider === DiscountProvider.stripe && (
- <>
+ {showStripeCouponFields && (
+ <>
+ <div>
+ <label
+ htmlFor="couponId"
+ className="flex items-center space-x-2"
+ >
+ <h2 className="text-sm font-medium text-neutral-900">
+ Stripe coupon ID
+ </h2>
+ </label>
+ <div className="mt-2">
+ <input
+ type="text"
+ id="couponId"
+ className="border-border-subtle block w-full rounded-lg bg-white px-3 py-2 text-neutral-800 placeholder-neutral-400 focus:border-neutral-500 focus:outline-none focus:ring-neutral-500 sm:text-sm"
+ {...register("couponId")}
+ placeholder="XZuejd0Q"
+ disabled={isEdit} // we don't allow updating the coupon ID for existing discounts
+ />
+ </div>
+ </div>
+
+ <div className="flex items-center gap-3">
+ <Switch
+ fn={(checked) => {
+ setUseStripeTestCouponId(checked);
+ if (!checked) {
+ setValue("couponTestId", "");
+ }
+ }}
+ checked={useStripeTestCouponId}
+ trackDimensions="w-8 h-4"
+ thumbDimensions="w-3 h-3"
+ thumbTranslate="translate-x-4"
+ />
+ <div className="flex items-center gap-2">
+ <h3 className="text-sm font-medium text-neutral-800">
+ Use Stripe test coupon ID
+ </h3>
+
+ <InfoTooltip content="Enabling this will allow you to test your coupon code before going live by entering your Stripe test coupon ID." />
+ </div>
+ </div>
+
+ {useStripeTestCouponId && (
<div>
<label
- htmlFor="couponId"
+ htmlFor="couponTestId"
className="flex items-center space-x-2"
>
<h2 className="text-sm font-medium text-neutral-900">
- Stripe coupon ID
+ Stripe test coupon ID
</h2>
</label>
<div className="mt-2">
<input
type="text"
- id="couponId"
- className="border-border-subtle block w-full rounded-lg bg-white px-3 py-2 text-neutral-800 placeholder-neutral-400 focus:border-neutral-500 focus:outline-none focus:ring-neutral-500 sm:text-sm"
- {...register("couponId")}
- placeholder="XZuejd0Q"
- disabled={!!discount} // we don't allow updating the coupon ID for existing discounts
+ id="couponTestId"
+ className="border-border-subtle block w-full rounded-lg px-3 py-2 text-neutral-800 placeholder-neutral-400 focus:border-neutral-500 focus:outline-none focus:ring-neutral-500 sm:text-sm"
+ {...register("couponTestId")}
+ placeholder="2NMXz81x"
/>
</div>
</div>
-
- <div className="flex items-center gap-3">
- <Switch
- fn={() => {
- setUseStripeTestCouponId(!useStripeTestCouponId);
- setValue("couponTestId", "");
- }}
- checked={useStripeTestCouponId}
- trackDimensions="w-8 h-4"
- thumbDimensions="w-3 h-3"
- thumbTranslate="translate-x-4"
- />
- <div className="flex items-center gap-2">
- <h3 className="text-sm font-medium text-neutral-800">
- Use Stripe test coupon ID
- </h3>
-
- <InfoTooltip content="Enabling this will allow you to test your coupon code before going live by entering your Stripe test coupon ID." />
- </div>
- </div>
-
- {useStripeTestCouponId && (
- <div>
- <label
- htmlFor="couponTestId"
- className="flex items-center space-x-2"
- >
- <h2 className="text-sm font-medium text-neutral-900">
- Stripe test coupon ID
- </h2>
- </label>
- <div className="mt-2">
- <input
- type="text"
- id="couponTestId"
- className="border-border-subtle block w-full rounded-lg px-3 py-2 text-neutral-800 placeholder-neutral-400 focus:border-neutral-500 focus:outline-none focus:ring-neutral-500 sm:text-sm"
- {...register("couponTestId")}
- placeholder="2NMXz81x"
- />
- </div>
- </div>
- )}
- </>
- )}
+ )}
+ </>
+ )}
</div>
</div>
}
@@ -420,80 +443,88 @@ function DiscountSheetContent({
<VerticalLine />
- <DiscountSheetCard
- className={cn(
- discount && "pointer-events-none cursor-not-allowed select-none",
- )}
- title={
- <>
- {provider === DiscountProvider.shopify ? (
- <Shopify className="h-7 w-auto" />
- ) : (
- <StripeIcon className="size-7" />
- )}
- <span className="leading-relaxed">
- Discount a{" "}
- <InlineBadgePopover
- text={capitalize(type)}
- disabled={!!discount}
- >
- <InlineBadgePopoverMenu
- selectedValue={type}
- onSelect={(value) =>
- setValue("type", value as "flat" | "percentage", {
- shouldDirty: true,
- })
- }
- items={[
- {
- text: "Flat",
- value: "flat",
- },
- {
- text: "Percentage",
- value: "percentage",
- },
- ]}
- />
- </InlineBadgePopover>{" "}
- {type === "percentage" && "of "}
- <InlineBadgePopover
- text={
- amount
- ? constructDiscountAmount({
- amount: type === "flat" ? amount * 100 : amount,
- type,
- })
- : "amount"
- }
- invalid={!amount}
- disabled={!!discount}
- >
- <AmountInput disabled={!!discount} />
- </InlineBadgePopover>{" "}
- <InlineBadgePopover
- text={
- maxDuration === 0
- ? "one time"
- : maxDuration === Infinity
- ? "for the customer's lifetime"
- : `for ${maxDuration} ${pluralize("month", Number(maxDuration))}`
- }
- disabled={!!discount}
- >
- <DurationPopoverContent
- value={Number(maxDuration)}
- onChange={(value) =>
- setValue("maxDuration", value, { shouldDirty: true })
- }
- presetDurations={RECURRING_MAX_DURATIONS}
- />
- </InlineBadgePopover>
- </span>
- </>
- }
- content={<></>}
- />
+ <Tooltip
+ content="To change the conditions, delete the discount and create a new one."
+ side="top"
+ disabled={!isEdit}
+ >
+ <div>
+ <DiscountSheetCard
+ className={cn(isEdit && "cursor-not-allowed select-none")}
+ title={
+ <>
+ {effectiveProvider === DiscountProvider.shopify ? (
+ <Shopify className="h-7 w-auto" />
+ ) : (
+ <StripeIcon className="size-7" />
+ )}
+ <span className="leading-relaxed">
+ Discount a{" "}
+ <InlineBadgePopover
+ text={capitalize(type)}
+ disabled={isEdit}
+ >
+ <InlineBadgePopoverMenu
+ selectedValue={type}
+ onSelect={(value) =>
+ setValue("type", value as "flat" | "percentage", {
+ shouldDirty: true,
+ })
+ }
+ items={[
+ {
+ text: "Flat",
+ value: "flat",
+ },
+ {
+ text: "Percentage",
+ value: "percentage",
+ },
+ ]}
+ />
+ </InlineBadgePopover>{" "}
+ {type === "percentage" && "of "}
+ <InlineBadgePopover
+ text={
+ amount
+ ? constructDiscountAmount({
+ amount: type === "flat" ? amount * 100 : amount,
+ type,
+ })
+ : "amount"
+ }
+ invalid={!amount}
+ disabled={isEdit}
+ >
+ <AmountInput disabled={isEdit} />
+ </InlineBadgePopover>{" "}
+ <InlineBadgePopover
+ text={
+ maxDuration === 0
+ ? "one time"
+ : maxDuration === Infinity
+ ? "for the customer's lifetime"
+ : `for ${maxDuration} ${pluralize("month", Number(maxDuration))}`
+ }
+ disabled={isEdit}
+ >
+ <DurationPopoverContent
+ value={Number(maxDuration)}
+ onChange={(value) =>
+ setValue("maxDuration", value, {
+ shouldDirty: true,
+ })
+ }
+ presetDurations={RECURRING_MAX_DURATIONS}
+ />
+ </InlineBadgePopover>
+ </span>
+ </>
+ }
+ content={<></>}
+ />
+ </div>
+ </Tooltip>
<VerticalLine />
@@ -507,7 +538,9 @@ function DiscountSheetContent({
</div>
<Switch
- fn={() => setValue("autoProvision", !autoProvision)}
+ fn={(checked) =>
+ setValue("autoProvision", checked, { shouldDirty: true })
+ }
checked={autoProvision}
trackDimensions="w-8 h-4"
thumbDimensions="w-3 h-3"
@@ -630,14 +663,17 @@ export function DiscountSheet({
}) {
const { queryParams } = useRouterStuff();
+ const setIsOpen: DiscountSheetProps["setIsOpen"] = (value) => {
+ const nextOpen = typeof value === "function" ? value(isOpen) : value;
+ rest.setIsOpen(value);
+ if (!nextOpen) {
+ queryParams({ del: "discountId", scroll: false });
+ }
+ };
+
return (
- <Sheet
- open={isOpen}
- onOpenChange={rest.setIsOpen}
- nested={nested}
- onClose={() => queryParams({ del: "discountId", scroll: false })}
- >
- <DiscountSheetContent {...rest} />
+ <Sheet open={isOpen} onOpenChange={setIsOpen} nested={nested}>
+ <DiscountSheetContent {...rest} setIsOpen={setIsOpen} />
</Sheet>
);
}
diff --git a/apps/web/ui/shared/inline-badge-popover.tsx b/apps/web/ui/shared/inline-badge-popover.tsx
index cff9e83cb2..64cd81def4 100644
--- a/apps/web/ui/shared/inline-badge-popover.tsx
+++ b/apps/web/ui/shared/inline-badge-popover.tsx
@@ -79,11 +79,13 @@ export function InlineBadgePopover({
disabled={disabled}
className={cn(
"inline-block rounded px-1.5 text-left text-sm font-semibold transition-colors",
- invalid
- ? "bg-orange-50 text-orange-500 hover:bg-orange-100 data-[state=open]:bg-orange-100"
- : showOptional
- ? "bg-neutral-100 text-neutral-500 hover:bg-neutral-200 data-[state=open]:bg-neutral-200"
- : "bg-blue-50 text-blue-700 hover:bg-blue-100 data-[state=open]:bg-blue-100",
+ disabled
+ ? "cursor-not-allowed bg-neutral-200 text-neutral-500 hover:bg-neutral-200 data-[state=open]:bg-neutral-200"
+ : invalid
+ ? "bg-orange-50 text-orange-500 hover:bg-orange-100 data-[state=open]:bg-orange-100"
+ : showOptional
+ ? "bg-neutral-100 text-neutral-500 hover:bg-neutral-200 data-[state=open]:bg-neutral-200"
+ : "bg-blue-50 text-blue-700 hover:bg-blue-100 data-[state=open]:bg-blue-100",
buttonClassName,
)}
>