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.

Refactor discount sheet and update related logic

Kiran Kcc3d03b8e162
diff --git a/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/groups/[groupSlug]/discount/group-discount.tsx b/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/groups/[groupSlug]/discount/group-discount.tsx
index 168463f42a..cc77bc57d6 100644
--- a/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/groups/[groupSlug]/discount/group-discount.tsx
+++ b/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/groups/[groupSlug]/discount/group-discount.tsx
@@ -3,7 +3,7 @@
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/add-edit-discount-sheet";
+import { useDiscountSheet } from "@/ui/partners/discounts/add-edit-discount-sheet";
import { ProgramRewardDescription } from "@/ui/partners/program-reward-description";
import { X } from "@/ui/shared/icons";
import {
diff --git a/apps/web/lib/actions/partners/create-discount.ts b/apps/web/lib/actions/partners/create-discount.ts
index 7b8efc0fc1..8850664e59 100644
--- a/apps/web/lib/actions/partners/create-discount.ts
+++ b/apps/web/lib/actions/partners/create-discount.ts
@@ -16,7 +16,7 @@ export const createDiscountAction = authActionClient
.schema(createDiscountSchema)
.action(async ({ parsedInput, ctx }) => {
const { workspace, user } = ctx;
- let { amount, type, maxDuration, couponId, couponTestId, groupId } =
+ let { amount, type, maxDuration, couponId, couponTestId, groupId, enableCouponTracking } =
parsedInput;
const programId = getDefaultProgramIdOrThrow(workspace);
diff --git a/apps/web/lib/actions/partners/update-discount.ts b/apps/web/lib/actions/partners/update-discount.ts
deleted file mode 100644
index eef952eb36..0000000000
--- a/apps/web/lib/actions/partners/update-discount.ts
+++ /dev/null
@@ -1,11 +0,0 @@
-"use server";
-
-import { updateDiscountSchema } from "@/lib/zod/schemas/discount";
-import { authActionClient } from "../safe-action";
-
-export const updateDiscountAction = authActionClient
- .schema(updateDiscountSchema)
- .action(async ({ parsedInput, ctx }) => {
- // TODO:
- // Remove this, Stripe coupon can't be updated
- });
diff --git a/apps/web/lib/zod/schemas/discount.ts b/apps/web/lib/zod/schemas/discount.ts
index 1c0afa7e71..856e7bf91e 100644
--- a/apps/web/lib/zod/schemas/discount.ts
+++ b/apps/web/lib/zod/schemas/discount.ts
@@ -29,6 +29,7 @@ export const createDiscountSchema = z.object({
couponId: z.string().nullish(),
couponTestId: z.string().nullish(),
groupId: z.string(),
+ enableCouponTracking: z.boolean().default(false),
});
export const updateDiscountSchema = createDiscountSchema
diff --git a/apps/web/ui/partners/add-edit-discount-sheet.tsx b/apps/web/ui/partners/add-edit-discount-sheet.tsx
deleted file mode 100644
index f451a310e2..0000000000
--- a/apps/web/ui/partners/add-edit-discount-sheet.tsx
+++ /dev/null
@@ -1,518 +0,0 @@
-"use client";
-
-import { createDiscountAction } from "@/lib/actions/partners/create-discount";
-import { deleteDiscountAction } from "@/lib/actions/partners/delete-discount";
-import { updateDiscountAction } from "@/lib/actions/partners/update-discount";
-import { handleMoneyInputChange, handleMoneyKeyDown } from "@/lib/form-utils";
-import useGroup from "@/lib/swr/use-group";
-import useProgram from "@/lib/swr/use-program";
-import useWorkspace from "@/lib/swr/use-workspace";
-import { DiscountProps } from "@/lib/types";
-import { createDiscountSchema } from "@/lib/zod/schemas/discount";
-import { X } from "@/ui/shared/icons";
-import {
- Button,
- InfoTooltip,
- Sheet,
- SimpleTooltipContent,
- Switch,
-} from "@dub/ui";
-import { CircleCheckFill, Tag } from "@dub/ui/icons";
-import { cn } from "@dub/utils";
-import { useAction } from "next-safe-action/hooks";
-import {
- Dispatch,
- PropsWithChildren,
- ReactNode,
- SetStateAction,
- useRef,
- useState,
-} from "react";
-import { FormProvider, useForm, useFormContext } from "react-hook-form";
-import { toast } from "sonner";
-import { mutate } from "swr";
-import { z } from "zod";
-
-interface DiscountSheetProps {
- setIsOpen: Dispatch<SetStateAction<boolean>>;
- discount?: DiscountProps;
- defaultDiscountValues?: DiscountProps;
-}
-
-type FormData = z.infer<typeof createDiscountSchema>;
-
-export const useAddEditDiscountForm = () => useFormContext<FormData>();
-
-const discountTypes = [
- {
- label: "One-off",
- description: "Offer a one-time discount",
- recurring: false,
- },
- {
- label: "Recurring",
- description: "Offer an ongoing discount",
- recurring: true,
- },
-] as const;
-
-const COUPON_CREATION_OPTIONS = [
- {
- label: "New Stripe coupon",
- description: "Create a new coupon",
- useExisting: false,
- },
- {
- label: "Use Stripe coupon ID",
- description: "Use an existing coupon",
- useExisting: true,
- },
-] as const;
-
-function DiscountSheetContent({
- setIsOpen,
- discount,
- defaultDiscountValues,
-}: DiscountSheetProps) {
- const formRef = useRef<HTMLFormElement>(null);
-
- const { group, mutateGroup } = useGroup();
- const { mutate: mutateProgram } = useProgram();
- const { id: workspaceId, defaultProgramId } = useWorkspace();
-
- const defaultValuesSource = discount || defaultDiscountValues;
-
- const [isRecurring, setIsRecurring] = useState(
- defaultValuesSource ? defaultValuesSource.maxDuration !== 0 : false,
- );
-
- const [useExistingCoupon, setUseExistingCoupon] = useState(
- Boolean(discount?.couponId),
- );
-
- const [useStripeTestCouponId, setUseStripeTestCouponId] = useState(
- Boolean(discount?.couponTestId),
- );
-
- const form = useForm<FormData>({
- defaultValues: {
- amount:
- defaultValuesSource?.type === "flat"
- ? defaultValuesSource.amount / 100
- : defaultValuesSource?.amount,
- type: defaultValuesSource?.type || "percentage",
- maxDuration:
- defaultValuesSource?.maxDuration === null
- ? Infinity
- : defaultValuesSource?.maxDuration || 0,
- couponId: defaultValuesSource?.couponId || "",
- couponTestId: defaultValuesSource?.couponTestId || "",
- },
- });
-
- const { handleSubmit, watch, setValue, register } = form;
- const [type, amount, maxDuration] = watch(["type", "amount", "maxDuration"]);
-
- const { executeAsync: createDiscount, isPending: isCreating } = useAction(
- createDiscountAction,
- {
- onSuccess: async () => {
- setIsOpen(false);
- toast.success("Discount created!");
- await mutateProgram();
- await mutateGroup();
- },
- onError({ error }) {
- toast.error(error.serverError);
- },
- },
- );
-
- const { executeAsync: updateDiscount, isPending: isUpdating } = useAction(
- updateDiscountAction,
- {
- onSuccess: async () => {
- setIsOpen(false);
- toast.success("Discount updated!");
- await mutateProgram();
- await mutateGroup();
- },
- onError({ error }) {
- toast.error(error.serverError);
- },
- },
- );
-
- const { executeAsync: deleteDiscount, isPending: isDeleting } = useAction(
- deleteDiscountAction,
- {
- onSuccess: async () => {
- setIsOpen(false);
- toast.success("Discount deleted!");
- await mutate(`/api/programs/${defaultProgramId}`);
- await mutateGroup();
- },
- onError({ error }) {
- toast.error(error.serverError);
- },
- },
- );
-
- const onSubmit = async (data: FormData) => {
- if (!workspaceId || !defaultProgramId || !group) {
- return;
- }
-
- const payload = {
- ...data,
- workspaceId,
- amount: data.type === "flat" ? data.amount * 100 : data.amount || 0,
- maxDuration:
- Number(data.maxDuration) === Infinity ? null : data.maxDuration,
- };
-
- if (!discount) {
- await createDiscount({
- ...payload,
- groupId: group.id,
- });
- } else {
- await updateDiscount({
- ...payload,
- discountId: discount.id,
- });
- }
- };
-
- const onDelete = async () => {
- if (!workspaceId || !defaultProgramId || !discount) {
- return;
- }
-
- if (!confirm("Are you sure you want to delete this discount?")) {
- return;
- }
-
- await deleteDiscount({
- workspaceId,
- discountId: discount.id,
- });
- };
-
- return (
- <FormProvider {...form}>
- <form
- ref={formRef}
- onSubmit={handleSubmit(onSubmit)}
- className="flex h-full flex-col"
- >
- <div className="flex h-16 items-center justify-between border-b border-neutral-200 px-6 py-4">
- <Sheet.Title className="text-lg font-semibold">
- {discount ? "Edit" : "Create"} discount
- </Sheet.Title>
- <Sheet.Close asChild>
- <Button
- variant="outline"
- icon={<X className="size-5" />}
- className="h-auto w-fit p-1"
- />
- </Sheet.Close>
- </div>
-
- <div className="flex flex-1 flex-col overflow-y-auto p-6">
- <DiscountSheetCard
- title={
- <>
- <div className="flex size-7 shrink-0 items-center justify-center rounded-md bg-neutral-100">
- <Tag className="size-4 text-neutral-800" />
- </div>
- <span className="leading-relaxed">Coupon connection</span>
- </>
- }
- content={
- <div className="space-y-4">
- <div className="flex flex-col gap-2">
- <label className="text-content-emphasis text-sm font-medium">
- Payment provider
- </label>
- <div className="">
- <select className="block w-full rounded-md border-neutral-300 px-3 py-2 text-sm text-neutral-900 placeholder-neutral-400 focus:border-neutral-500 focus:outline-none focus:ring-neutral-500">
- <option value="stripe">Stripe</option>
- </select>
- </div>
- </div>
-
- <div className="grid grid-cols-1 gap-3 lg:grid-cols-2">
- {COUPON_CREATION_OPTIONS.map(
- ({ label, description, useExisting }) => {
- const isSelected = useExistingCoupon === useExisting;
-
- return (
- <label
- key={label}
- className={cn(
- "relative flex w-full cursor-pointer items-start gap-0.5 rounded-md border border-neutral-200 bg-white p-3 text-neutral-600 hover:bg-neutral-50",
- "transition-all duration-150",
- isSelected &&
- "border-black bg-neutral-50 text-neutral-900 ring-1 ring-black",
- )}
- >
- <input
- type="radio"
- value={label}
- className="hidden"
- checked={isSelected}
- onChange={(e) => {
- if (e.target.checked) {
- setUseExistingCoupon(useExisting);
- }
- }}
- />
- <div className="flex grow flex-col text-sm">
- <span className="font-medium">{label}</span>
- <span>{description}</span>
- </div>
- <CircleCheckFill
- className={cn(
- "-mr-px -mt-px flex size-4 scale-75 items-center justify-center rounded-full opacity-0 transition-[transform,opacity] duration-150",
- isSelected && "scale-100 opacity-100",
- )}
- />
- </label>
- );
- },
- )}
- </div>
-
- {useExistingCoupon && (
- <>
- <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 focus:border-neutral-500 focus:outline-none focus:ring-neutral-500 sm:text-sm"
- {...register("couponId")}
- />
- </div>
- </div>
-
- <div className="flex items-center gap-3">
- <Switch
- fn={setUseStripeTestCouponId}
- 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={
- <SimpleTooltipContent title="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 focus:border-neutral-500 focus:outline-none focus:ring-neutral-500 sm:text-sm"
- {...register("couponTestId")}
- />
- </div>
- </div>
- )}
- </>
- )}
-
- <div className="flex items-center gap-3">
- <Switch
- fn={() =>
- setValue(
- "enableCouponTracking",
- !watch("enableCouponTracking"),
- )
- }
- checked={watch("enableCouponTracking")}
- 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">
- Enable automatic coupon code tracking
- </h3>
-
- <InfoTooltip
- content={
- <SimpleTooltipContent
- title="Enabling this will allow the partners to create a promo code for their links."
- href="https://dub.co/help/article/coupon-codes-tracking"
- cta="Learn more"
- />
- }
- />
- </div>
- </div>
- </div>
- }
- />
-
- <VerticalLine />
- </div>
-
- <div className="flex items-center justify-between border-t border-neutral-200 p-5">
- <div>
- {discount && (
- <Button
- type="button"
- variant="outline"
- text="Remove discount"
- onClick={onDelete}
- loading={isDeleting}
- />
- )}
- </div>
-
- <div className="flex items-center gap-2">
- <Button
- type="button"
- variant="secondary"
- onClick={() => setIsOpen(false)}
- text="Cancel"
- className="w-fit"
- disabled={isCreating || isUpdating || isDeleting}
- />
-
- <Button
- type="submit"
- variant="primary"
- text={discount ? "Update discount" : "Create discount"}
- className="w-fit"
- loading={isCreating || isUpdating || isDeleting}
- disabled={!discount && amount == null}
- />
- </div>
- </div>
- </form>
- </FormProvider>
- );
-}
-
-function DiscountSheetCard({
- title,
- content,
-}: PropsWithChildren<{ title: ReactNode; content: ReactNode }>) {
- return (
- <div className="border-border-subtle rounded-xl border bg-white text-sm shadow-sm">
- <div className="text-content-emphasis flex items-center gap-2.5 p-2.5 font-medium">
- {title}
- </div>
- {content && (
- <div className="border-border-subtle -mx-px rounded-xl border-x border-t bg-neutral-100 p-2.5">
- {content}
- </div>
- )}
- </div>
- );
-}
-
-const VerticalLine = () => (
- <div className="bg-border-subtle ml-6 h-4 w-px shrink-0" />
-);
-
-function DiscountIconSquare({
- icon: Icon,
-}: {
- icon: React.ComponentType<any>;
-}) {
- return (
- <div className="flex size-5 items-center justify-center rounded bg-neutral-100">
- <Icon className="size-3 text-neutral-600" />
- </div>
- );
-}
-
-function AmountInput() {
- const { watch, register } = useAddEditDiscountForm();
- const type = watch("type");
-
- return (
- <div className="relative rounded-md shadow-sm">
- {type === "flat" && (
- <span className="absolute inset-y-0 left-0 flex items-center pl-1.5 text-sm text-neutral-400">
- $
- </span>
- )}
- <input
- className={cn(
- "block w-full rounded-md border-neutral-300 px-1.5 py-1 text-neutral-900 placeholder-neutral-400 focus:border-neutral-500 focus:outline-none focus:ring-neutral-500 sm:text-sm",
- type === "flat" ? "pl-4 pr-12" : "pr-7",
- )}
- {...register("amount", {
- required: true,
- setValueAs: (value: string) => (value === "" ? undefined : +value),
- min: 0,
- max: type === "percentage" ? 100 : undefined,
- onChange: handleMoneyInputChange,
- })}
- onKeyDown={handleMoneyKeyDown}
- />
- <span className="absolute inset-y-0 right-0 flex items-center pr-1.5 text-sm text-neutral-400">
- {type === "flat" ? "USD" : "%"}
- </span>
- </div>
- );
-}
-
-export function DiscountSheet({
- isOpen,
- nested,
- ...rest
-}: DiscountSheetProps & {
- isOpen: boolean;
- nested?: boolean;
-}) {
- return (
- <Sheet open={isOpen} onOpenChange={rest.setIsOpen} nested={nested}>
- <DiscountSheetContent {...rest} />
- </Sheet>
- );
-}
-
-export function useDiscountSheet(
- props: { nested?: boolean } & Omit<DiscountSheetProps, "setIsOpen">,
-) {
- const [isOpen, setIsOpen] = useState(false);
-
- return {
- DiscountSheet: (
- <DiscountSheet setIsOpen={setIsOpen} isOpen={isOpen} {...props} />
- ),
- setIsOpen,
- };
-}
diff --git a/apps/web/ui/partners/discounts/add-edit-discount-sheet.tsx b/apps/web/ui/partners/discounts/add-edit-discount-sheet.tsx
new file mode 100644
index 0000000000..d8aeac56c0
--- /dev/null
+++ b/apps/web/ui/partners/discounts/add-edit-discount-sheet.tsx
@@ -0,0 +1,563 @@
+"use client";
+
+import { createDiscountAction } from "@/lib/actions/partners/create-discount";
+import { deleteDiscountAction } from "@/lib/actions/partners/delete-discount";
+import { constructRewardAmount } from "@/lib/api/sales/construct-reward-amount";
+import { handleMoneyInputChange, handleMoneyKeyDown } from "@/lib/form-utils";
+import useGroup from "@/lib/swr/use-group";
+import useProgram from "@/lib/swr/use-program";
+import useWorkspace from "@/lib/swr/use-workspace";
+import { DiscountProps } from "@/lib/types";
+import { createDiscountSchema } from "@/lib/zod/schemas/discount";
+import { RECURRING_MAX_DURATIONS } from "@/lib/zod/schemas/misc";
+import { Stripe } from "@/ui/guides/icons/stripe";
+import { X } from "@/ui/shared/icons";
+import {
+ Button,
+ InfoTooltip,
+ Sheet,
+ SimpleTooltipContent,
+ Switch,
+} from "@dub/ui";
+import { CircleCheckFill, Tag } from "@dub/ui/icons";
+import { capitalize, cn, pluralize } from "@dub/utils";
+import { useAction } from "next-safe-action/hooks";
+import {
+ Dispatch,
+ PropsWithChildren,
+ ReactNode,
+ SetStateAction,
+ useRef,
+ useState,
+} from "react";
+import { FormProvider, useForm, useFormContext } from "react-hook-form";
+import { toast } from "sonner";
+import { mutate } from "swr";
+import { z } from "zod";
+import { RewardDiscountPartnersCard } from "../groups/reward-discount-partners-card";
+import {
+ InlineBadgePopover,
+ InlineBadgePopoverMenu,
+} from "../rewards/inline-badge-popover";
+
+interface DiscountSheetProps {
+ setIsOpen: Dispatch<SetStateAction<boolean>>;
+ discount?: DiscountProps;
+ defaultDiscountValues?: DiscountProps;
+}
+
+type FormData = z.infer<typeof createDiscountSchema>;
+
+export const useAddEditDiscountForm = () => useFormContext<FormData>();
+
+const COUPON_CREATION_OPTIONS = [
+ {
+ label: "New Stripe coupon",
+ description: "Create a new coupon",
+ useExisting: false,
+ },
+ {
+ label: "Use Stripe coupon ID",
+ description: "Use an existing coupon",
+ useExisting: true,
+ },
+] as const;
+
+function DiscountSheetContent({
+ setIsOpen,
+ discount,
+ defaultDiscountValues,
+}: DiscountSheetProps) {
+ const formRef = useRef<HTMLFormElement>(null);
+
+ const { group, mutateGroup } = useGroup();
+ const { mutate: mutateProgram } = useProgram();
+ const { id: workspaceId, defaultProgramId } = useWorkspace();
+
+ const [useExistingCoupon, setUseExistingCoupon] = useState(
+ Boolean(discount?.couponId),
+ );
+
+ const [useStripeTestCouponId, setUseStripeTestCouponId] = useState(
+ Boolean(discount?.couponTestId),
+ );
+
+ const defaultValuesSource = discount ||
+ defaultDiscountValues || {
+ amount: 10,
+ type: "percentage",
+ maxDuration: 6,
+ couponId: "",
+ couponTestId: "",
+ }; // default is 10% for 6 months
+
+ const form = useForm<FormData>({
+ defaultValues: {
+ amount:
+ defaultValuesSource.type === "flat"
+ ? defaultValuesSource.amount / 100
+ : defaultValuesSource.amount,
+ type: defaultValuesSource.type,
+ maxDuration:
+ defaultValuesSource.maxDuration === null
+ ? Infinity
+ : defaultValuesSource.maxDuration,
+ couponId: defaultValuesSource.couponId,
+ couponTestId: defaultValuesSource.couponTestId,
+ },
+ });
+
+ const { handleSubmit, watch, setValue, register } = form;
+ const [type, amount, maxDuration] = watch(["type", "amount", "maxDuration"]);
+
+ const { executeAsync: createDiscount, isPending: isCreating } = useAction(
+ createDiscountAction,
+ {
+ onSuccess: async () => {
+ setIsOpen(false);
+ toast.success("Discount created!");
+ await mutateProgram();
+ await mutateGroup();
+ },
+ onError({ error }) {
+ toast.error(error.serverError);
+ },
+ },
+ );
+
+ const { executeAsync: deleteDiscount, isPending: isDeleting } = useAction(
+ deleteDiscountAction,
+ {
+ onSuccess: async () => {
+ setIsOpen(false);
+ toast.success("Discount deleted!");
+ await mutate(`/api/programs/${defaultProgramId}`);
+ await mutateGroup();
+ },
+ onError({ error }) {
+ toast.error(error.serverError);
+ },
+ },
+ );
+
+ const onSubmit = async (data: FormData) => {
+ if (!workspaceId || !defaultProgramId || !group) {
+ return;
+ }
+
+ await createDiscount({
+ ...data,
+ workspaceId,
+ groupId: group.id,
+ amount: data.type === "flat" ? data.amount * 100 : data.amount || 0,
+ maxDuration:
+ Number(data.maxDuration) === Infinity ? null : data.maxDuration,
+ });
+ };
+
+ const onDelete = async () => {
+ if (!workspaceId || !defaultProgramId || !discount) {
+ return;
+ }
+
+ if (!confirm("Are you sure you want to delete this discount?")) {
+ return;
+ }
+
+ await deleteDiscount({
+ workspaceId,
+ discountId: discount.id,
+ });
+ };
+
+ return (
+ <FormProvider {...form}>
+ <form
+ ref={formRef}
+ onSubmit={handleSubmit(onSubmit)}
+ className="flex h-full flex-col"
+ >
+ <div className="flex h-16 items-center justify-between border-b border-neutral-200 px-6 py-4">
+ <Sheet.Title className="text-lg font-semibold">
+ {discount ? "Edit" : "Create"} discount
+ </Sheet.Title>
+ <Sheet.Close asChild>
+ <Button
+ variant="outline"
+ icon={<X className="size-5" />}
+ className="h-auto w-fit p-1"
+ />
+ </Sheet.Close>
+ </div>
+
+ <div className="flex flex-1 flex-col overflow-y-auto p-6">
+ <DiscountSheetCard
+ title={
+ <>
+ <div className="flex size-7 shrink-0 items-center justify-center rounded-md bg-neutral-100">
+ <Tag className="size-4 text-neutral-800" />
+ </div>
+ <span className="leading-relaxed">Coupon connection</span>
+ </>
+ }
+ content={
+ <div className="border-border-subtle -mx-px rounded-xl border-x border-t bg-neutral-100 p-2.5">
+ <div className="space-y-4">
+ <div className="flex flex-col gap-2">
+ <label className="text-content-emphasis text-sm font-medium">
+ Payment provider
+ </label>
+ <div className="">
+ <select className="block w-full rounded-md border-neutral-300 px-3 py-2 text-sm text-neutral-900 placeholder-neutral-400 focus:border-neutral-500 focus:outline-none focus:ring-neutral-500">
+ <option value="stripe">Stripe</option>
+ </select>
+ </div>
+ </div>
+
+ <div className="grid grid-cols-1 gap-3 lg:grid-cols-2">
+ {COUPON_CREATION_OPTIONS.map(
+ ({ label, description, useExisting }) => {
+ const isSelected = useExistingCoupon === useExisting;
+
+ return (
+ <label
+ key={label}
+ className={cn(
+ "relative flex w-full cursor-pointer items-start gap-0.5 rounded-md border border-neutral-200 bg-white p-3 text-neutral-600 hover:bg-neutral-50",
+ "transition-all duration-150",
+ isSelected &&
+ "border-black bg-neutral-50 text-neutral-900 ring-1 ring-black",
+ )}
+ >
+ <input
+ type="radio"
+ value={label}
+ className="hidden"
+ checked={isSelected}
+ onChange={(e) => {
+ if (e.target.checked) {
+ setUseExistingCoupon(useExisting);
+ }
+ }}
+ />
+ <div className="flex grow flex-col text-sm">
+ <span className="font-medium">{label}</span>
+ <span>{description}</span>
+ </div>
+ <CircleCheckFill
+ className={cn(
+ "-mr-px -mt-px flex size-4 scale-75 items-center justify-center rounded-full opacity-0 transition-[transform,opacity] duration-150",
+ isSelected && "scale-100 opacity-100",
+ )}
+ />
+ </label>
+ );
+ },
+ )}
+ </div>
+
+ {useExistingCoupon && (
+ <>
+ <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 focus:border-neutral-500 focus:outline-none focus:ring-neutral-500 sm:text-sm"
+ {...register("couponId")}
+ />
+ </div>
+ </div>
+
+ <div className="flex items-center gap-3">
+ <Switch
+ fn={setUseStripeTestCouponId}
+ 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={
+ <SimpleTooltipContent title="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 focus:border-neutral-500 focus:outline-none focus:ring-neutral-500 sm:text-sm"
+ {...register("couponTestId")}
+ />
+ </div>
+ </div>
+ )}
+ </>
+ )}
+
+ <div className="flex items-center gap-3">
+ <Switch
+ fn={() =>
+ setValue(
+ "enableCouponTracking",
+ !watch("enableCouponTracking"),
+ )
+ }
+ checked={watch("enableCouponTracking")}
+ 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">
+ Enable automatic coupon code tracking
+ </h3>
+
+ <InfoTooltip
+ content={
+ <SimpleTooltipContent
+ title="Enabling this will allow the partners to create a promo code for their links."
+ href="https://dub.co/help/article/coupon-codes-tracking"
+ cta="Learn more"
+ />
+ }
+ />
+ </div>
+ </div>
+ </div>
+ </div>
+ }
+ />
+
+ {!useExistingCoupon && (
+ <>
+ <VerticalLine />
+
+ <DiscountSheetCard
+ title={
+ <>
+ <Stripe className="size-7" />
+ <span className="leading-relaxed">
+ Discount a{" "}
+ <InlineBadgePopover text={capitalize(type)}>
+ <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
+ ? constructRewardAmount({
+ amount: type === "flat" ? amount * 100 : amount,
+ type,
+ })
+ : "amount"
+ }
+ invalid={!amount}
+ >
+ <AmountInput />
+ </InlineBadgePopover>{" "}
+ <InlineBadgePopover
+ text={
+ maxDuration === 0
+ ? "one time"
+ : maxDuration === Infinity
+ ? "for the customer's lifetime"
+ : `for ${maxDuration} ${pluralize("month", Number(maxDuration))}`
+ }
+ >
+ <InlineBadgePopoverMenu
+ selectedValue={maxDuration?.toString()}
+ onSelect={(value) =>
+ setValue("maxDuration", Number(value), {
+ shouldDirty: true,
+ })
+ }
+ items={[
+ {
+ text: "one time",
+ value: "0",
+ },
+ ...RECURRING_MAX_DURATIONS.filter(
+ (v) => v !== 0,
+ ).map((v) => ({
+ text: `for ${v} ${pluralize("month", Number(v))}`,
+ value: v.toString(),
+ })),
+ {
+ text: "for the customer's lifetime",
+ value: "Infinity",
+ },
+ ]}
+ />
+ </InlineBadgePopover>
+ </span>
+ </>
+ }
+ content={<></>}
+ />
+ </>
+ )}
+
+ <VerticalLine />
+
+ {group && <RewardDiscountPartnersCard groupId={group.id} />}
+ </div>
+
+ <div className="flex items-center justify-between border-t border-neutral-200 p-5">
+ <div>
+ {discount && (
+ <Button
+ type="button"
+ variant="outline"
+ text="Remove discount"
+ onClick={onDelete}
+ loading={isDeleting}
+ />
+ )}
+ </div>
+
+ <div className="flex items-center gap-2">
+ <Button
+ type="button"
+ variant="secondary"
+ onClick={() => setIsOpen(false)}
+ text="Cancel"
+ className="w-fit"
+ disabled={isCreating || isDeleting}
+ />
+
+ <Button
+ type="submit"
+ variant="primary"
+ text="Create discount"
+ className="w-fit"
+ loading={isCreating || isDeleting}
+ disabled={!discount && amount == null}
+ />
+ </div>
+ </div>
+ </form>
+ </FormProvider>
+ );
+}
+
+function DiscountSheetCard({
+ title,
+ content,
+}: PropsWithChildren<{ title: ReactNode; content: ReactNode }>) {
+ return (
+ <div className="border-border-subtle rounded-xl border bg-white text-sm shadow-sm">
+ <div className="text-content-emphasis flex items-center gap-2.5 p-2.5 font-medium">
+ {title}
+ </div>
+ {content && <>{content}</>}
+ </div>
+ );
+}
+
+const VerticalLine = () => (
+ <div className="bg-border-subtle ml-6 h-4 w-px shrink-0" />
+);
+
+function AmountInput() {
+ const { watch, register } = useAddEditDiscountForm();
+ const type = watch("type");
+
+ return (
+ <div className="relative rounded-md shadow-sm">
+ {type === "flat" && (
+ <span className="absolute inset-y-0 left-0 flex items-center pl-1.5 text-sm text-neutral-400">
+ $
+ </span>
+ )}
+ <input
+ className={cn(
+ "block w-full rounded-md border-neutral-300 px-1.5 py-1 text-neutral-900 placeholder-neutral-400 focus:border-neutral-500 focus:outline-none focus:ring-neutral-500 sm:text-sm",
+ type === "flat" ? "pl-4 pr-12" : "pr-7",
+ )}
+ {...register("amount", {
+ required: true,
+ setValueAs: (value: string) => (value === "" ? undefined : +value),
+ min: 0,
+ max: type === "percentage" ? 100 : undefined,
+ onChange: handleMoneyInputChange,
+ })}
+ onKeyDown={handleMoneyKeyDown}
+ />
+ <span className="absolute inset-y-0 right-0 flex items-center pr-1.5 text-sm text-neutral-400">
+ {type === "flat" ? "USD" : "%"}
+ </span>
+ </div>
+ );
+}
+
+export function DiscountSheet({
+ isOpen,
+ nested,
+ ...rest
+}: DiscountSheetProps & {
+ isOpen: boolean;
+ nested?: boolean;
+}) {
+ return (
+ <Sheet open={isOpen} onOpenChange={rest.setIsOpen} nested={nested}>
+ <DiscountSheetContent {...rest} />
+ </Sheet>
+ );
+}
+
+export function useDiscountSheet(
+ props: { nested?: boolean } & Omit<DiscountSheetProps, "setIsOpen">,
+) {
+ const [isOpen, setIsOpen] = useState(false);
+
+ return {
+ DiscountSheet: (
+ <DiscountSheet setIsOpen={setIsOpen} isOpen={isOpen} {...props} />
+ ),
+ setIsOpen,
+ };
+}
diff --git a/apps/web/ui/partners/rewards/reward-partners-card.tsx b/apps/web/ui/partners/groups/reward-discount-partners-card.tsx
similarity index 96%
rename from apps/web/ui/partners/rewards/reward-partners-card.tsx
rename to apps/web/ui/partners/groups/reward-discount-partners-card.tsx
index fd2eba7a6d..08f7fa9e15 100644
--- a/apps/web/ui/partners/rewards/reward-partners-card.tsx
+++ b/apps/web/ui/partners/groups/reward-discount-partners-card.tsx
@@ -8,15 +8,19 @@ import { motion } from "framer-motion";
import Link from "next/link";
import { useParams } from "next/navigation";
import { useState } from "react";
-import { RewardIconSquare } from "./reward-icon-square";
+import { RewardIconSquare } from "../rewards/reward-icon-square";
+
+export function RewardDiscountPartnersCard({ groupId }: { groupId: string }) {
+ const [isExpanded, setIsExpanded] = useState<boolean>(false);
-export function RewardPartnersCard({ groupId }: { groupId: string }) {
- const { partners } = usePartners({
- query: { groupId, pageSize: 10 },
- });
const { partnersCount } = usePartnersCount<number | undefined>({ groupId });
- const [isExpanded, setIsExpanded] = useState<boolean>(false);
+ const { partners } = usePartners({
+ query: {
+ groupId,
+ pageSize: 10,
+ },
+ });
return (
<div className="border-border-subtle rounded-xl border bg-white text-sm shadow-sm">
diff --git a/apps/web/ui/partners/rewards/add-edit-reward-sheet.tsx b/apps/web/ui/partners/rewards/add-edit-reward-sheet.tsx
index 558b367ded..9b380def2f 100644
--- a/apps/web/ui/partners/rewards/add-edit-reward-sheet.tsx
+++ b/apps/web/ui/partners/rewards/add-edit-reward-sheet.tsx
@@ -42,6 +42,7 @@ import { FormProvider, useForm, useFormContext } from "react-hook-form";
import { toast } from "sonner";
import { mutate } from "swr";
import { z } from "zod";
+import { RewardDiscountPartnersCard } from "../groups/reward-discount-partners-card";
import { usePartnersUpgradeModal } from "../partners-upgrade-modal";
import {
InlineBadgePopover,
@@ -49,7 +50,6 @@ import {
InlineBadgePopoverMenu,
} from "./inline-badge-popover";
import { RewardIconSquare } from "./reward-icon-square";
-import { RewardPartnersCard } from "./reward-partners-card";
import { RewardsLogic } from "./rewards-logic";
interface RewardSheetProps {
@@ -362,7 +362,7 @@ function RewardSheetContent({
<VerticalLine />
- {group && <RewardPartnersCard groupId={group.id} />}
+ {group && <RewardDiscountPartnersCard groupId={group.id} />}
</div>
<div className="flex items-center justify-between border-t border-neutral-200 p-5">