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.

Update add-edit-discount-sheet.tsx

Kiran K07f10a3eeaf2
diff --git a/apps/web/ui/partners/add-edit-discount-sheet.tsx b/apps/web/ui/partners/add-edit-discount-sheet.tsx
index ce4e56b9c4..02da291f82 100644
--- a/apps/web/ui/partners/add-edit-discount-sheet.tsx
+++ b/apps/web/ui/partners/add-edit-discount-sheet.tsx
@@ -9,24 +9,23 @@ 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 { X } from "@/ui/shared/icons";
-import { AnimatedSizeContainer, Button, CircleCheckFill, Sheet } from "@dub/ui";
-import { cn, pluralize } from "@dub/utils";
-import { BadgePercent } from "lucide-react";
+import { Button, Sheet } from "@dub/ui";
+import { CircleCheckFill, Tag } from "@dub/ui/icons";
+import { cn } from "@dub/utils";
import { useAction } from "next-safe-action/hooks";
-import { Dispatch, SetStateAction, useRef, useState } from "react";
-import { useForm } from "react-hook-form";
+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 { ProgramRewardDescription } from "./program-reward-description";
-import {
- ProgramSheetAccordion,
- ProgramSheetAccordionContent,
- ProgramSheetAccordionItem,
- ProgramSheetAccordionTrigger,
-} from "./program-sheet-accordion";
interface DiscountSheetProps {
setIsOpen: Dispatch<SetStateAction<boolean>>;
@@ -36,6 +35,8 @@ interface DiscountSheetProps {
type FormData = z.infer<typeof createDiscountSchema>;
+export const useAddEditDiscountForm = () => useFormContext<FormData>();
+
const discountTypes = [
{
label: "One-off",
@@ -49,14 +50,14 @@ const discountTypes = [
},
] as const;
-const couponTypes = [
+const COUPON_CREATION_OPTIONS = [
{
- label: "New coupon",
+ label: "New Stripe coupon",
description: "Create a new coupon",
useExisting: false,
},
{
- label: "Existing coupon",
+ label: "Use Stripe coupon ID",
description: "Use an existing coupon",
useExisting: true,
},
@@ -83,19 +84,7 @@ function DiscountSheetContent({
Boolean(discount?.couponId),
);
- const [accordionValues, setAccordionValues] = useState<string[]>([
- "discount-type",
- "discount-details",
- "stripe-coupon-details",
- ]);
-
- const {
- register,
- handleSubmit,
- watch,
- setValue,
- formState: { errors },
- } = useForm<FormData>({
+ const form = useForm<FormData>({
defaultValues: {
amount:
defaultValuesSource?.type === "flat"
@@ -111,7 +100,8 @@ function DiscountSheetContent({
},
});
- const [type, amount] = watch(["type", "amount"]);
+ const { handleSubmit, watch, setValue, register } = form;
+ const [type, amount, maxDuration] = watch(["type", "amount", "maxDuration"]);
const { executeAsync: createDiscount, isPending: isCreating } = useAction(
createDiscountAction,
@@ -200,7 +190,7 @@ function DiscountSheetContent({
};
return (
- <>
+ <FormProvider {...form}>
<form
ref={formRef}
onSubmit={handleSubmit(onSubmit)}
@@ -219,367 +209,75 @@ function DiscountSheetContent({
</Sheet.Close>
</div>
- <div className="flex-1 overflow-y-auto p-6">
- <ProgramSheetAccordion
- type="multiple"
- value={accordionValues}
- onValueChange={setAccordionValues}
- >
- {!discount && (
- <ProgramSheetAccordionItem value="discount-type">
- <ProgramSheetAccordionTrigger>
- Discount Type
- </ProgramSheetAccordionTrigger>
- <ProgramSheetAccordionContent>
- <div className="space-y-4">
- <p className="text-sm text-neutral-600">
- Set how the discount will be applied
- </p>
- <div className="-m-1">
- <AnimatedSizeContainer
- height
- transition={{ ease: "easeInOut", duration: 0.2 }}
- >
- <div className="p-1">
- <div
- className={cn(
- "space-y-4 transition-opacity duration-200",
- )}
- aria-hidden={!isRecurring}
- {...{
- inert: !isRecurring,
- }}
- >
- <div className="grid grid-cols-1 gap-3 lg:grid-cols-2">
- {discountTypes.map(
- ({ label, description, recurring }) => {
- const isSelected = isRecurring === recurring;
-
- 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) {
- setIsRecurring(recurring);
- setValue(
- "maxDuration",
- recurring ? 3 : 0,
- );
- }
- }}
- />
- <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>
-
- {isRecurring && (
- <div className="space-y-4">
- <div>
- <label
- htmlFor="duration"
- className="text-sm font-medium text-neutral-800"
- >
- Duration
- </label>
- <div className="relative mt-2 rounded-md shadow-sm">
- <select
- className="block w-full rounded-md border-neutral-300 text-neutral-900 focus:border-neutral-500 focus:outline-none focus:ring-neutral-500 sm:text-sm"
- {...register("maxDuration", {
- valueAsNumber: true,
- })}
- >
- {RECURRING_MAX_DURATIONS.filter(
- (v) => v !== 0,
- ).map((v) => (
- <option value={v} key={v}>
- {v} {pluralize("month", Number(v))}
- </option>
- ))}
- <option value={Infinity}>Lifetime</option>
- </select>
- </div>
- </div>
- </div>
- )}
- </div>
- </div>
- </AnimatedSizeContainer>
- </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>
- </ProgramSheetAccordionContent>
- </ProgramSheetAccordionItem>
- )}
+ </div>
- {!discount && (
- <ProgramSheetAccordionItem value="discount-details">
- <ProgramSheetAccordionTrigger>
- Discount Details
- </ProgramSheetAccordionTrigger>
- <ProgramSheetAccordionContent>
- <div className="space-y-4">
- <p className="text-sm text-neutral-600">
- Set the discount amount and configuration
- </p>
-
- <div>
- <label
- htmlFor="type"
- className="text-sm font-medium text-neutral-800"
- >
- Discount model
- </label>
- <div className="relative mt-2 rounded-md shadow-sm">
- <select
- className="block w-full rounded-md border-neutral-300 text-neutral-900 focus:border-neutral-500 focus:outline-none focus:ring-neutral-500 sm:text-sm"
- {...register("type")}
- >
- <option value="percentage">Percentage</option>
- <option value="flat">Flat</option>
- </select>
- </div>
- </div>
-
- <div>
- <label
- htmlFor="amount"
- className="text-sm font-medium text-neutral-800"
- >
- Amount
- </label>
- <div className="relative mt-2 rounded-md shadow-sm">
- {type === "flat" && (
- <span className="absolute inset-y-0 left-0 flex items-center pl-3 text-sm text-neutral-400">
- $
- </span>
- )}
- <input
- className={cn(
- "block w-full rounded-md border-neutral-300 text-neutral-900 placeholder-neutral-400 focus:border-neutral-500 focus:outline-none focus:ring-neutral-500 sm:text-sm",
- errors.amount &&
- "border-red-600 focus:border-red-500 focus:ring-red-600",
- type === "flat" ? "pl-6 pr-12" : "pr-7",
- )}
- {...register("amount", {
- valueAsNumber: true,
- min: 0,
- max: 100,
- onChange: handleMoneyInputChange,
- required: true,
- })}
- onKeyDown={handleMoneyKeyDown}
- placeholder={"0"}
- />
- <span className="absolute inset-y-0 right-0 flex items-center pr-3 text-sm text-neutral-400">
- {type === "flat" ? "USD" : "%"}
- </span>
- </div>
- </div>
-
- {/* Display the coupon switcher if the discount is being created */}
- {!discount && (
- <div>
- <p className="text-sm text-neutral-600">
- Create a new discount code or connect an existing one
- </p>
- <div className="mt-4">
- <div className="grid grid-cols-1 gap-3 lg:grid-cols-2">
- {couponTypes.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>
- </div>
- </div>
- )}
-
- {useExistingCoupon && (
- <>
- <div>
- <label
- htmlFor="couponId"
- className="text-sm font-medium text-neutral-800"
- >
- Stripe coupon ID
- </label>
- <div className="relative mt-2 rounded-md shadow-sm">
- <input
- className={cn(
- "block w-full rounded-md border-neutral-300 text-neutral-900 placeholder-neutral-400 focus:border-neutral-500 focus:outline-none focus:ring-neutral-500 sm:text-sm",
- errors.couponId &&
- "border-red-600 pr-7 focus:border-red-500 focus:ring-red-600",
- )}
- {...register("couponId", {
- required: useExistingCoupon,
- })}
- placeholder="XZuejd0Q"
- />
- </div>
+ <div className="grid grid-cols-1 gap-3 lg:grid-cols-2">
+ {COUPON_CREATION_OPTIONS.map(
+ ({ label, description, useExisting }) => {
+ const isSelected = useExistingCoupon === useExisting;
- <p className="mt-1 text-xs text-neutral-500">
- Learn more about{" "}
- <a
- href="https://docs.stripe.com/billing/subscriptions/coupons"
- target="_blank"
- rel="noopener noreferrer"
- className="underline"
- >
- Stripe coupon codes here
- </a>
- </p>
- </div>
-
- <div>
- <label
- htmlFor="couponTestId"
- className="text-sm font-medium text-neutral-800"
- >
- Stripe test coupon ID (optional)
- </label>
- <div className="relative mt-2 rounded-md shadow-sm">
- <input
- className={cn(
- "block w-full rounded-md border-neutral-300 text-neutral-900 placeholder-neutral-400 focus:border-neutral-500 focus:outline-none focus:ring-neutral-500 sm:text-sm",
- )}
- {...register("couponTestId")}
- placeholder="2NMXz81x"
- />
- </div>
- </div>
- </>
- )}
- </div>
- </ProgramSheetAccordionContent>
- </ProgramSheetAccordionItem>
- )}
-
- {discount && (
- <ProgramSheetAccordionItem value="stripe-coupon-details">
- <ProgramSheetAccordionTrigger>
- Stripe coupon
- </ProgramSheetAccordionTrigger>
- <ProgramSheetAccordionContent>
- <div className="space-y-4">
- <div>
- <label
- htmlFor="couponId"
- className="text-sm font-medium text-neutral-800"
- >
- Stripe coupon ID
- </label>
- <div className="relative mt-2 rounded-md shadow-sm">
- <input
+ return (
+ <label
+ key={label}
className={cn(
- "block w-full rounded-md border-neutral-300 text-neutral-900 placeholder-neutral-400 read-only:bg-neutral-100 read-only:text-neutral-500 focus:border-neutral-500 focus:outline-none focus:ring-neutral-500 sm:text-sm",
+ "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",
)}
- readOnly
- defaultValue={discount.couponId || ""}
- />
- </div>
- </div>
-
- {discount.couponTestId && (
- <div>
- <label
- htmlFor="couponTestId"
- className="text-sm font-medium text-neutral-800"
>
- Stripe test coupon ID (optional)
- </label>
- <div className="relative mt-2 rounded-md shadow-sm">
<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(
- "block w-full rounded-md border-neutral-300 text-neutral-900 placeholder-neutral-400 read-only:bg-neutral-100 read-only:text-neutral-500 focus:border-neutral-500 focus:outline-none focus:ring-neutral-500 sm:text-sm",
+ "-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",
)}
- readOnly
- defaultValue={discount.couponTestId || ""}
/>
- </div>
- </div>
- )}
-
- <div className="space-y-3">
- <div className="flex items-center gap-4 rounded-lg border border-neutral-200 p-4 transition-all">
- <div className="flex size-10 items-center justify-center rounded-full border border-neutral-200 bg-white">
- <BadgePercent className="size-4 text-neutral-600" />
- </div>
- <span className="items-center text-balance text-sm text-neutral-600">
- <ProgramRewardDescription discount={discount} />
- </span>
- </div>
- <p className="text-xs font-normal leading-4 text-neutral-500">
- Discounts cannot be changed after creation, only their
- partner eligibility.
- </p>
- </div>
- </div>
- </ProgramSheetAccordionContent>
- </ProgramSheetAccordionItem>
- )}
- </ProgramSheetAccordion>
+ </label>
+ );
+ },
+ )}
+ </div>
+ </div>
+ }
+ />
+
+ <VerticalLine />
</div>
<div className="flex items-center justify-between border-t border-neutral-200 p-5">
@@ -616,7 +314,73 @@ function DiscountSheetContent({
</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>
);
}