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.

Implement default reward type selection and update related forms

Kiran K5f45a73991f0
diff --git a/apps/web/app/app.dub.co/(new-program)/[slug]/programs/new/form.tsx b/apps/web/app/app.dub.co/(new-program)/[slug]/programs/new/form.tsx
index e9fd8fe00e..fd7d7a9ff9 100644
--- a/apps/web/app/app.dub.co/(new-program)/[slug]/programs/new/form.tsx
+++ b/apps/web/app/app.dub.co/(new-program)/[slug]/programs/new/form.tsx
@@ -89,11 +89,9 @@ export function Form() {
if (!uploadResponse.ok) throw new Error("Failed to upload to signed URL");
setValue("logo", destinationUrl, { shouldDirty: true });
- console.log(destinationUrl);
toast.success(`${file.name} uploaded!`);
} catch (e) {
toast.error("Failed to upload logo");
- console.error(e);
} finally {
setIsUploading(false);
}
diff --git a/apps/web/app/app.dub.co/(new-program)/[slug]/programs/new/overview/page-client.tsx b/apps/web/app/app.dub.co/(new-program)/[slug]/programs/new/overview/page-client.tsx
index fcb021b1fc..6afeacd5b3 100644
--- a/apps/web/app/app.dub.co/(new-program)/[slug]/programs/new/overview/page-client.tsx
+++ b/apps/web/app/app.dub.co/(new-program)/[slug]/programs/new/overview/page-client.tsx
@@ -76,7 +76,7 @@ export function PageClient() {
type: (data.type ?? "flat") as CommissionType,
amount: data.amount ?? 0,
maxDuration: data.maxDuration ?? 0,
- event: "sale" as EventType,
+ event: data.defaultRewardType,
};
const SECTIONS = [
diff --git a/apps/web/app/app.dub.co/(new-program)/[slug]/programs/new/rewards/form.tsx b/apps/web/app/app.dub.co/(new-program)/[slug]/programs/new/rewards/form.tsx
index 979aea00d5..fd0470523a 100644
--- a/apps/web/app/app.dub.co/(new-program)/[slug]/programs/new/rewards/form.tsx
+++ b/apps/web/app/app.dub.co/(new-program)/[slug]/programs/new/rewards/form.tsx
@@ -43,6 +43,19 @@ export const PROGRAM_TYPES = [
},
] as const;
+const DEFAULT_REWARD_TYPES = [
+ {
+ key: "lead",
+ label: "Lead",
+ description: "For sign ups and leads",
+ },
+ {
+ key: "sale",
+ label: "Sale",
+ description: "For sales and subscriptions",
+ },
+] as const;
+
const IMPORT_SOURCES = [
{
id: "rewardful",
@@ -76,7 +89,6 @@ export function Form() {
mutate();
},
onError: ({ error }) => {
- console.log(error);
toast.error(error.serverError);
},
});
@@ -89,6 +101,7 @@ export function Form() {
...(programType === "new" && {
rewardful: null,
apiKeyPrefix: null,
+ amount: data.amount ? data.amount * 100 : null,
}),
...(programType === "import" && {
type: null,
@@ -188,31 +201,36 @@ export function Form() {
const NewProgramForm = ({ register, watch, setValue }: FormProps) => {
const [isRecurring, setIsRecurring] = useState(false);
- const [type, maxDuration] = watch(["type", "maxDuration"]);
+
+ const [type, maxDuration, defaultRewardType] = watch([
+ "type",
+ "maxDuration",
+ "defaultRewardType",
+ ]);
useEffect(() => {
setIsRecurring(maxDuration !== 0);
}, [maxDuration]);
return (
- <>
- <div className="space-y-6">
- <div className="space-y-1">
+ <div className="flex flex-col gap-10">
+ <div className="grid grid-cols-1 gap-6">
+ <div>
<h2 className="text-base font-medium text-neutral-900">
- Commission structure
+ Reward type
</h2>
- <p className="text-sm font-normal text-neutral-600">
- Set how the affiliate will get rewarded
+ <p className="mt-1 text-sm font-normal text-neutral-600">
+ Set the default reward type for all your affiliates
</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;
+ {DEFAULT_REWARD_TYPES.map(({ key, label, description }) => {
+ const isSelected = key === defaultRewardType;
return (
<label
- key={value}
+ key={key}
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",
@@ -222,20 +240,15 @@ const NewProgramForm = ({ register, watch, setValue }: FormProps) => {
>
<input
type="radio"
- value={value}
+ value={key}
className="hidden"
checked={isSelected}
onChange={(e) => {
- if (value === "one-off") {
- setIsRecurring(false);
- setValue("maxDuration", 0, { shouldValidate: true });
- }
+ setValue("defaultRewardType", key, { shouldDirty: true });
- if (value === "recurring") {
- setIsRecurring(true);
- setValue("maxDuration", 3, {
- shouldValidate: true,
- });
+ if (key === "lead") {
+ setValue("type", "flat", { shouldDirty: true });
+ setValue("maxDuration", 0, { shouldDirty: true });
}
}}
/>
@@ -257,56 +270,125 @@ const NewProgramForm = ({ register, watch, setValue }: FormProps) => {
);
})}
</div>
+ </div>
+
+ {defaultRewardType === "sale" && (
+ <div className="grid grid-cols-1 gap-6">
+ <div>
+ <h2 className="text-base font-medium text-neutral-900">
+ Commission structure
+ </h2>
+ <p className="mt-1 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 (value === "one-off") {
+ setIsRecurring(false);
+ setValue("maxDuration", 0, { shouldValidate: true });
+ }
+
+ if (value === "recurring") {
+ setIsRecurring(true);
+ setValue("maxDuration", 3, {
+ shouldValidate: 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}
+ selected={duration === 12}
+ >
+ {duration} {duration === 1 ? "month" : "months"}
+ </option>
+ ),
+ )}
+ <option value="">Lifetime</option>
+ </select>
+ </div>
+ )}
+ </div>
+ )}
- {isRecurring && (
+ <div className="grid grid-cols-1 gap-6">
+ <div>
+ <h2 className="text-base font-medium text-neutral-900">Payout</h2>
+ <p className="mt-1 text-sm font-normal text-neutral-600">
+ Set how much the affiliate will get rewarded
+ </p>
+ </div>
+
+ {defaultRewardType === "sale" && (
<div>
<label className="text-sm font-medium text-neutral-800">
- Duration
+ Payout model
</label>
<select
- {...register("maxDuration", { valueAsNumber: true })}
+ {...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"
>
- {RECURRING_MAX_DURATIONS.filter((v) => v !== 0).map(
- (duration) => (
- <option
- key={duration}
- value={duration}
- selected={duration === 12}
- >
- {duration} {duration === 1 ? "month" : "months"}
- </option>
- ),
- )}
- <option value="">Lifetime</option>
+ <option value="flat">Flat</option>
+ <option value="percentage">Percentage</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
+ Amount {defaultRewardType != "sale" ? "per lead" : ""}
</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" && "$"}
@@ -331,7 +413,7 @@ const NewProgramForm = ({ register, watch, setValue }: FormProps) => {
</div>
</div>
</div>
- </>
+ </div>
);
};
diff --git a/apps/web/app/app.dub.co/(new-program)/form-wrapper.tsx b/apps/web/app/app.dub.co/(new-program)/form-wrapper.tsx
index 17d95db753..1f60377c0c 100644
--- a/apps/web/app/app.dub.co/(new-program)/form-wrapper.tsx
+++ b/apps/web/app/app.dub.co/(new-program)/form-wrapper.tsx
@@ -19,6 +19,7 @@ export function FormWrapper({ children }: { children: React.ReactNode }) {
values: programOnboarding
? {
...programOnboarding,
+ defaultRewardType: programOnboarding.defaultRewardType ?? "lead",
linkType: programOnboarding.linkType ?? "short",
programType: programOnboarding.programType ?? "new",
type: programOnboarding.type ?? "flat",
diff --git a/apps/web/app/app.dub.co/(new-program)/header.tsx b/apps/web/app/app.dub.co/(new-program)/header.tsx
index 4aec9dda57..a60010a09f 100644
--- a/apps/web/app/app.dub.co/(new-program)/header.tsx
+++ b/apps/web/app/app.dub.co/(new-program)/header.tsx
@@ -27,7 +27,6 @@ export function ProgramOnboardingHeader() {
const { executeAsync, isPending } = useAction(onboardProgramAction, {
onError: ({ error }) => {
- console.log(error);
toast.error(error.serverError);
},
});
diff --git a/apps/web/lib/actions/partners/create-program.ts b/apps/web/lib/actions/partners/create-program.ts
index 6d91b6e339..b5381aa858 100644
--- a/apps/web/lib/actions/partners/create-program.ts
+++ b/apps/web/lib/actions/partners/create-program.ts
@@ -30,6 +30,7 @@ export const createProgram = async ({
name,
domain,
url,
+ defaultRewardType,
type,
amount,
maxDuration,
@@ -73,7 +74,7 @@ export const createProgram = async ({
type,
amount,
maxDuration,
- event: "sale",
+ event: defaultRewardType,
},
},
}),
diff --git a/apps/web/lib/zod/schemas/program-onboarding.ts b/apps/web/lib/zod/schemas/program-onboarding.ts
index 658318b523..fb96ad668a 100644
--- a/apps/web/lib/zod/schemas/program-onboarding.ts
+++ b/apps/web/lib/zod/schemas/program-onboarding.ts
@@ -30,6 +30,7 @@ export const programRewardSchema = z
})
.merge(
z.object({
+ defaultRewardType: z.enum(["lead", "sale"]).default("lead"),
type: z.nativeEnum(CommissionType).nullish(),
amount: z.number().min(0).nullish(),
maxDuration: z.coerce
diff --git a/apps/web/ui/partners/add-edit-reward-sheet.tsx b/apps/web/ui/partners/add-edit-reward-sheet.tsx
index 0de0ce466f..ccc1d36a2d 100644
--- a/apps/web/ui/partners/add-edit-reward-sheet.tsx
+++ b/apps/web/ui/partners/add-edit-reward-sheet.tsx
@@ -82,9 +82,6 @@ function RewardSheetContent({
const [selectedPartnerType, setSelectedPartnerType] =
useState<(typeof PARTNER_TYPES)[number]["key"]>("all");
- const [selectedDefaultRewardType, setSelectedDefaultRewardType] =
- useState<(typeof DEFAULT_REWARD_TYPES)[number]["key"]>("lead");
-
const [isRecurring, setIsRecurring] = useState(
reward ? reward.maxDuration !== 0 : false,
);
@@ -295,8 +292,7 @@ function RewardSheetContent({
</label>
<div className="grid grid-cols-1 gap-3 lg:grid-cols-2">
{DEFAULT_REWARD_TYPES.map((rewardType) => {
- const isSelected =
- selectedDefaultRewardType === rewardType.key;
+ const isSelected = selectedEvent === rewardType.key;
const labelContent = (
<label
@@ -315,7 +311,6 @@ function RewardSheetContent({
checked={isSelected}
onChange={(e) => {
if (e.target.checked) {
- setSelectedDefaultRewardType(rewardType.key);
setValue("event", rewardType.key);
setValue("type", "flat");
}