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.

configure reward page

Kiran K2803bb46e68f
diff --git a/apps/web/app/app.dub.co/(dashboard)/[slug]/programs/(onboarding)/onboarding/(steps)/rewards/form.tsx b/apps/web/app/app.dub.co/(dashboard)/[slug]/programs/(onboarding)/onboarding/(steps)/rewards/form.tsx
index e71fee5330..342a512855 100644
--- a/apps/web/app/app.dub.co/(dashboard)/[slug]/programs/(onboarding)/onboarding/(steps)/rewards/form.tsx
+++ b/apps/web/app/app.dub.co/(dashboard)/[slug]/programs/(onboarding)/onboarding/(steps)/rewards/form.tsx
@@ -1,28 +1,49 @@
"use client";
-import { Button, useMediaQuery } from "@dub/ui";
+import { handleMoneyInputChange, handleMoneyKeyDown } from "@/lib/form-utils";
+import {
+ COMMISSION_TYPES,
+ createOrUpdateRewardSchema,
+ RECURRING_MAX_DURATIONS,
+} from "@/lib/zod/schemas/rewards";
+import { Button, CircleCheckFill } from "@dub/ui";
+import { cn } from "@dub/utils";
+import { useState } from "react";
import { useForm } from "react-hook-form";
import { z } from "zod";
-const formSchema = z.object({
- // TODO: Add form schema
-});
+type Form = z.infer<typeof createOrUpdateRewardSchema>;
-type Form = z.infer<typeof formSchema>;
+const PROGRAM_SOURCES = [
+ {
+ value: "new",
+ label: "New program",
+ description: "Start a brand-new program",
+ },
+ {
+ value: "import",
+ label: "Import program",
+ description: "Migrate an existing program",
+ },
+] as const;
+
+type ProgramSource = (typeof PROGRAM_SOURCES)[number]["value"];
export function Form() {
- const { isMobile } = useMediaQuery();
+ const [isRecurring, setIsRecurring] = useState(false);
+ const [programSource, setProgramSource] = useState<ProgramSource>("new");
const {
register,
handleSubmit,
- formState: { isSubmitting, isValid },
+ formState: { isSubmitting },
watch,
} = useForm<Form>({
defaultValues: {
- linkType: "short",
+ event: "sale",
+ type: "flat",
+ maxDuration: 0,
},
- mode: "onChange",
});
const onSubmit = async (data: Form) => {
@@ -30,13 +51,193 @@ export function Form() {
// TODO: Handle form submission
};
+ const type = watch("type");
+
return (
<form onSubmit={handleSubmit(onSubmit)} className="space-y-10">
+ <div className="space-y-6">
+ <div className="space-y-1">
+ <h2 className="text-base font-medium text-neutral-900">
+ Reward creation
+ </h2>
+ <p className="text-sm font-normal text-neutral-600">
+ Create a brand new reward or import an existing program.
+ </p>
+ </div>
+
+ <div className="grid grid-cols-1 gap-3 lg:grid-cols-2">
+ {PROGRAM_SOURCES.map((source) => {
+ const isSelected = programSource === source.value;
+
+ return (
+ <label
+ key={source.value}
+ 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={source.value}
+ className="hidden"
+ onChange={() => setProgramSource(source.value)}
+ />
+ <div className="flex grow flex-col text-sm">
+ <span className="text-sm font-semibold text-neutral-900">
+ {source.label}
+ </span>
+ <span className="text-sm font-normal text-neutral-600">
+ {source.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 className="space-y-6">
+ <div className="space-y-1">
+ <h2 className="text-base font-medium text-neutral-900">
+ Commission structure
+ </h2>
+ <p className="text-sm font-normal text-neutral-600">
+ Set how the affiliate will get rewarded
+ </p>
+ </div>
+
+ <div className="grid grid-cols-1 gap-3 lg:grid-cols-2">
+ {COMMISSION_TYPES.map(({ value, label, description }) => {
+ const isSelected = (value === "recurring") === isRecurring;
+
+ return (
+ <label
+ key={value}
+ 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={value}
+ className="hidden"
+ checked={isSelected}
+ onChange={(e) => {
+ if (e.target.checked) {
+ setIsRecurring(value === "recurring");
+ register("maxDuration", {
+ value: value === "recurring" ? 3 : 0,
+ valueAsNumber: true,
+ });
+ }
+ }}
+ />
+ <div className="flex grow flex-col text-sm">
+ <span className="text-sm font-semibold text-neutral-900">
+ {label}
+ </span>
+ <span className="text-sm font-normal text-neutral-600">
+ {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>
+ <label className="text-sm font-medium text-neutral-800">
+ Duration
+ </label>
+ <select
+ {...register("maxDuration", { valueAsNumber: true })}
+ className="mt-2 block w-full rounded-md border border-neutral-300 bg-white py-2 pl-3 pr-10 text-sm text-neutral-900 focus:border-neutral-500 focus:outline-none focus:ring-neutral-500"
+ >
+ {RECURRING_MAX_DURATIONS.filter((v) => v !== 0).map(
+ (duration) => (
+ <option key={duration} value={duration}>
+ {duration} {duration === 1 ? "month" : "months"}
+ </option>
+ ),
+ )}
+ <option value={Infinity}>Lifetime</option>
+ </select>
+ </div>
+ )}
+ </div>
+
+ <div className="space-y-6">
+ <div className="space-y-1">
+ <h2 className="text-base font-medium text-neutral-900">Payout</h2>
+ <p className="text-sm font-normal text-neutral-600">
+ Set how much the affiliate will get rewarded
+ </p>
+ </div>
+
+ <div>
+ <label className="text-sm font-medium text-neutral-800">
+ Payout model
+ </label>
+ <select
+ {...register("type")}
+ className="mt-2 block w-full rounded-md border border-neutral-300 bg-white py-2 pl-3 pr-10 text-sm text-neutral-900 focus:border-neutral-500 focus:outline-none focus:ring-neutral-500"
+ >
+ <option value="flat">Flat</option>
+ <option value="percentage">Percentage</option>
+ </select>
+ </div>
+
+ <div>
+ <label className="text-sm font-medium text-neutral-800">Amount</label>
+ <div className="relative mt-2 rounded-md shadow-sm">
+ <span className="absolute inset-y-0 left-0 flex items-center pl-3 text-sm text-neutral-400">
+ {type === "flat" && "$"}
+ </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",
+ type === "flat" ? "pl-6 pr-12" : "pr-7",
+ )}
+ {...register("amount", {
+ required: true,
+ valueAsNumber: true,
+ min: 0,
+ max: type === "flat" ? 1000 : 100,
+ })}
+ onChange={handleMoneyInputChange}
+ onKeyDown={handleMoneyKeyDown}
+ />
+ <span className="absolute inset-y-0 right-0 flex items-center pr-3 text-sm text-neutral-400">
+ {type === "flat" ? "USD" : "%"}
+ </span>
+ </div>
+ </div>
+ </div>
+
<Button
text="Continue"
className="w-full"
loading={isSubmitting}
- disabled={!isValid || isSubmitting}
+ disabled={isSubmitting}
/>
</form>
);
diff --git a/apps/web/lib/zod/schemas/rewards.ts b/apps/web/lib/zod/schemas/rewards.ts
index 4f4fbfc0c2..eff9a8ae01 100644
--- a/apps/web/lib/zod/schemas/rewards.ts
+++ b/apps/web/lib/zod/schemas/rewards.ts
@@ -4,6 +4,19 @@ import { getPaginationQuerySchema } from "./misc";
export const RECURRING_MAX_DURATIONS = [0, 3, 6, 12, 18, 24];
+export const COMMISSION_TYPES = [
+ {
+ value: "one-off",
+ label: "One-off",
+ description: "Pay a one-time payout",
+ },
+ {
+ value: "recurring",
+ label: "Recurring",
+ description: "Pay an ongoing payout",
+ },
+] as const;
+
export const RewardSchema = z.object({
id: z.string(),
event: z.nativeEnum(EventType),
diff --git a/apps/web/ui/partners/add-edit-reward-sheet.tsx b/apps/web/ui/partners/add-edit-reward-sheet.tsx
index b2d2ee8892..f3a3784a1f 100644
--- a/apps/web/ui/partners/add-edit-reward-sheet.tsx
+++ b/apps/web/ui/partners/add-edit-reward-sheet.tsx
@@ -12,6 +12,7 @@ import useRewards from "@/lib/swr/use-rewards";
import useWorkspace from "@/lib/swr/use-workspace";
import { EnrolledPartnerProps, RewardProps } from "@/lib/types";
import {
+ COMMISSION_TYPES,
createRewardSchema,
RECURRING_MAX_DURATIONS,
} from "@/lib/zod/schemas/rewards";
@@ -54,7 +55,7 @@ interface RewardSheetProps {
type FormData = z.infer<typeof createRewardSchema>;
-const partnerTypes = [
+const PARTNER_TYPES = [
{
key: "all",
label: "All Partners",
@@ -67,19 +68,6 @@ const partnerTypes = [
},
] as const;
-const commissionTypes = [
- {
- label: "One-off",
- description: "Pay a one-time payout",
- recurring: false,
- },
- {
- label: "Recurring",
- description: "Pay an ongoing payout",
- recurring: true,
- },
-] as const;
-
function RewardSheetContent({ setIsOpen, event, reward }: RewardSheetProps) {
const formRef = useRef<HTMLFormElement>(null);
@@ -91,7 +79,7 @@ function RewardSheetContent({ setIsOpen, event, reward }: RewardSheetProps) {
const [isAddPartnersOpen, setIsAddPartnersOpen] = useState(false);
const [selectedPartnerType, setSelectedPartnerType] =
- useState<(typeof partnerTypes)[number]["key"]>("all");
+ useState<(typeof PARTNER_TYPES)[number]["key"]>("all");
const [isRecurring, setIsRecurring] = useState(
reward ? reward.maxDuration !== 0 : false,
@@ -429,7 +417,7 @@ function RewardSheetContent({ setIsOpen, event, reward }: RewardSheetProps) {
{event !== "sale" && (
<div className="mt-2">
<div className="grid grid-cols-1 gap-3 lg:grid-cols-2">
- {partnerTypes.map((partnerType) => {
+ {PARTNER_TYPES.map((partnerType) => {
const isSelected = selectedPartnerType === partnerType.key;
const isDisabled =
@@ -515,9 +503,10 @@ function RewardSheetContent({ setIsOpen, event, reward }: RewardSheetProps) {
>
<div className="p-1">
<div className="grid grid-cols-1 gap-3 lg:grid-cols-2">
- {commissionTypes.map(
- ({ label, description, recurring }) => {
- const isSelected = isRecurring === recurring;
+ {COMMISSION_TYPES.map(
+ ({ label, description, value }) => {
+ const isSelected =
+ (value === "recurring") === isRecurring;
return (
<label
@@ -531,15 +520,15 @@ function RewardSheetContent({ setIsOpen, event, reward }: RewardSheetProps) {
>
<input
type="radio"
- value={label}
+ value={value}
className="hidden"
checked={isSelected}
onChange={(e) => {
if (e.target.checked) {
- setIsRecurring(recurring);
+ setIsRecurring(value === "recurring");
setValue(
"maxDuration",
- recurring
+ value === "recurring"
? reward?.maxDuration || 3
: 0,
);