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 reward creation flow and validation logic

Kiran Kd7c86d9eb972
diff --git a/apps/web/app/app.dub.co/(dashboard)/[slug]/programs/[programId]/settings/rewards/reward-settings.tsx b/apps/web/app/app.dub.co/(dashboard)/[slug]/programs/[programId]/settings/rewards/reward-settings.tsx
index 4c838e487e..806e0c3b7a 100644
--- a/apps/web/app/app.dub.co/(dashboard)/[slug]/programs/[programId]/settings/rewards/reward-settings.tsx
+++ b/apps/web/app/app.dub.co/(dashboard)/[slug]/programs/[programId]/settings/rewards/reward-settings.tsx
@@ -26,9 +26,8 @@ const SaleReward = () => {
program?.defaultRewardId &&
rewards?.find((r) => r.id === program.defaultRewardId);
- const { rewardSheet, setIsOpen } = useRewardSheet({
+ const { RewardSheet, setIsOpen } = useRewardSheet({
event: "sale",
- isDefault: !!defaultReward,
});
return (
@@ -56,7 +55,7 @@ const SaleReward = () => {
description="Create a default reward that will be offered to all partners"
onClick={() => setIsOpen(true)}
/>
- {rewardSheet}
+ {RewardSheet}
</>
)}
</div>
@@ -67,6 +66,7 @@ const SaleReward = () => {
const AdditionalRewards = () => {
const { program } = useProgram();
const { rewards, loading } = useRewards();
+
const additionalRewards = rewards?.filter(
(reward) => reward.id !== program?.defaultRewardId,
);
@@ -140,7 +140,7 @@ const Reward = ({ reward }: { reward: RewardType }) => {
const CreateRewardButton = () => {
const [openPopover, setOpenPopover] = useState(false);
const [selectedEvent, setSelectedEvent] = useState<EventType | null>(null);
- const { rewardSheet, setIsOpen } = useRewardSheet({
+ const { RewardSheet, setIsOpen } = useRewardSheet({
event: selectedEvent || "sale", // default to sale, but it won't show unless setIsOpen is true
});
@@ -210,7 +210,7 @@ const CreateRewardButton = () => {
</svg>
</button>
</Popover>
- {rewardSheet}
+ {RewardSheet}
</>
);
};
diff --git a/apps/web/lib/actions/partners/create-reward.ts b/apps/web/lib/actions/partners/create-reward.ts
index cc9eb2bc47..6ea10ee0e5 100644
--- a/apps/web/lib/actions/partners/create-reward.ts
+++ b/apps/web/lib/actions/partners/create-reward.ts
@@ -18,15 +18,8 @@ export const createRewardAction = authActionClient
.schema(schema)
.action(async ({ parsedInput, ctx }) => {
const { workspace } = ctx;
- const {
- programId,
- event,
- isDefault,
- partnerIds,
- amount,
- type,
- maxDuration,
- } = parsedInput;
+ const { programId, partnerIds, event, amount, type, maxDuration } =
+ parsedInput;
const program = await getProgramOrThrow({
workspaceId: workspace.id,
@@ -53,21 +46,11 @@ export const createRewardAction = authActionClient
}
}
- if (isDefault) {
- if (program.defaultRewardId) {
- throw new Error(
- `A program can only have one default reward and you've already set one with id ${program.defaultRewardId}.`,
- );
- }
+ const hasDefaultReward = !!program.defaultRewardId;
- if (event !== "sale") {
- throw new Error("Default reward must be of type `sale`.");
- }
+ // if (event === "sale") {
- if (partnerIds && partnerIds.length > 0) {
- throw new Error("Default reward should not be partner specific.");
- }
- }
+ // }
// TODO:
// Partners can't be more than one reward of the same type.
@@ -79,7 +62,7 @@ export const createRewardAction = authActionClient
event,
type,
amount,
- maxDuration,
+ maxDuration: maxDuration ? parseInt(maxDuration) : null,
...(programEnrollments && {
partners: {
createMany: {
@@ -92,7 +75,7 @@ export const createRewardAction = authActionClient
},
});
- if (isDefault) {
+ if (!hasDefaultReward) {
await prisma.program.update({
where: {
id: programId,
diff --git a/apps/web/lib/zod/schemas/rewards.ts b/apps/web/lib/zod/schemas/rewards.ts
index 0094c99ac6..93ebb15fba 100644
--- a/apps/web/lib/zod/schemas/rewards.ts
+++ b/apps/web/lib/zod/schemas/rewards.ts
@@ -1,17 +1,22 @@
-import {
- CommissionInterval,
- CommissionType,
- EventType,
-} from "@dub/prisma/client";
+import { CommissionType, EventType } from "@dub/prisma/client";
import { z } from "zod";
+export const RECURRING_MAX_DURATIONS = [
+ "0",
+ "3",
+ "6",
+ "12",
+ "18",
+ "24",
+] as const;
+
export const rewardSchema = z.object({
id: z.string(),
programId: z.string(),
event: z.nativeEnum(EventType),
type: z.nativeEnum(CommissionType),
amount: z.number(),
- maxDuration: z.number().int().positive().nullish(),
+ maxDuration: z.number().nullish(),
createdAt: z.date(),
updatedAt: z.date(),
});
@@ -20,9 +25,8 @@ export const createOrUpdateRewardSchema = z.object({
event: z.nativeEnum(EventType),
type: z.nativeEnum(CommissionType).default("flat"),
amount: z.number().int().min(0),
- maxDuration: z.number().int().positive().nullish(),
+ maxDuration: z.enum(RECURRING_MAX_DURATIONS).nullish(),
partnerIds: z.array(z.string()).nullish(),
- isDefault: z.boolean().default(false),
});
export const createRewardSchema = createOrUpdateRewardSchema.superRefine(
@@ -32,7 +36,7 @@ export const createRewardSchema = createOrUpdateRewardSchema.superRefine(
ctx.addIssue({
code: z.ZodIssueCode.custom,
message: "Duration is only allowed for sale rewards.",
- path: ["duration"],
+ path: ["maxDuration"],
});
}
diff --git a/apps/web/ui/partners/add-edit-reward-sheet.tsx b/apps/web/ui/partners/add-edit-reward-sheet.tsx
index ef687635ab..2a40daf29d 100644
--- a/apps/web/ui/partners/add-edit-reward-sheet.tsx
+++ b/apps/web/ui/partners/add-edit-reward-sheet.tsx
@@ -5,7 +5,10 @@ import { mutatePrefix } from "@/lib/swr/mutate";
import usePartners from "@/lib/swr/use-partners";
import useProgram from "@/lib/swr/use-program";
import useWorkspace from "@/lib/swr/use-workspace";
-import { createRewardSchema } from "@/lib/zod/schemas/rewards";
+import {
+ createRewardSchema,
+ RECURRING_MAX_DURATIONS,
+} from "@/lib/zod/schemas/rewards";
import { SelectEligiblePartnersSheet } from "@/ui/partners/select-eligible-partners-sheet";
import { X } from "@/ui/shared/icons";
import { EventType } from "@dub/prisma/client";
@@ -18,30 +21,21 @@ import {
Users,
useTable,
} from "@dub/ui";
-import { cn, INFINITY_NUMBER, pluralize } from "@dub/utils";
+import { cn, pluralize } from "@dub/utils";
import { DICEBEAR_AVATAR_URL } from "@dub/utils/src/constants";
import { useAction } from "next-safe-action/hooks";
-import { Dispatch, SetStateAction, useRef, useState } from "react";
+import { Dispatch, SetStateAction, useEffect, useRef, useState } from "react";
import { useForm } from "react-hook-form";
import { toast } from "sonner";
+import { mutate } from "swr";
import { z } from "zod";
-// TODO:
-// Can create 1 "all partner" reward
-// Can create infinite "specific parter" rewards
-// Partners can't be on more than one "specific reward"
-
interface RewardSheetProps {
setIsOpen: Dispatch<SetStateAction<boolean>>;
event: EventType;
- isDefault?: boolean;
}
-type FormData = z.infer<typeof createRewardSchema> & {
- partnerIds: string[];
- commissionDuration: number;
- commissionInterval: "month" | "year";
-};
+type FormData = z.infer<typeof createRewardSchema>;
const partnerTypes = [
{
@@ -69,19 +63,19 @@ const commissionTypes = [
},
];
-function RewardSheetContent({
- setIsOpen,
- event,
- isDefault = false,
-}: RewardSheetProps) {
+function RewardSheetContent({ setIsOpen, event }: RewardSheetProps) {
const { program } = useProgram();
const { data: partners } = usePartners();
const { id: workspaceId } = useWorkspace();
const formRef = useRef<HTMLFormElement>(null);
- const [selectedPartnerType, setSelectedPartnerType] =
- useState<(typeof partnerTypes)[number]["key"]>("all");
const [isAddPartnersOpen, setIsAddPartnersOpen] = useState(false);
+ const [selectedPartnerType, setSelectedPartnerType] = useState<
+ (typeof partnerTypes)[number]["key"]
+ >(event === "sale" ? "specific" : "all");
+
+ const [isRecurring, setIsRecurring] = useState(false);
+
const {
register,
handleSubmit,
@@ -91,59 +85,59 @@ function RewardSheetContent({
formState: { errors },
} = useForm<FormData>({
defaultValues: {
- type: "flat",
- isDefault,
- maxDuration: null,
- partnerIds: [],
event,
- commissionDuration: INFINITY_NUMBER,
- commissionInterval: "month",
+ type: "flat",
+ maxDuration: "0",
},
});
+ const [partnerIds = [], amount, type, maxDuration] = watch([
+ "partnerIds",
+ "amount",
+ "type",
+ "maxDuration",
+ ]);
+
const { executeAsync, isPending } = useAction(createRewardAction, {
- onSuccess: async (res) => {
+ onSuccess: async () => {
setIsOpen(false);
toast.success("Successfully created reward!");
+ await mutate(`/api/programs/${program?.id}`);
await mutatePrefix(`/api/programs/${program?.id}/rewards`);
},
onError({ error }) {
+ console.log({ error });
toast.error(error.serverError);
},
});
const onSubmit = async (data: FormData) => {
if (!workspaceId || !program) {
- toast.error("Please fill all required fields");
return;
}
await executeAsync({
+ ...data,
workspaceId,
programId: program.id,
- ...data,
- amount: data.amount * 100,
+ amount: type === "flat" ? data.amount * 100 : data.amount,
+ ...(event === "sale"
+ ? {
+ // @ts-ignore
+ maxDuration: maxDuration === "" ? null : maxDuration,
+ }
+ : {
+ maxDuration: undefined,
+ }),
});
};
- const [
- partnerIds = [],
- amount,
- type,
- isDefaultReward,
- commissionDuration,
- commissionInterval,
- ] = watch([
- "partnerIds",
- "amount",
- "type",
- "isDefault",
- "commissionDuration",
- "commissionInterval",
- ]);
+ useEffect(() => {
+ setIsRecurring(maxDuration === "0" ? false : true);
+ }, [maxDuration]);
const selectedPartnersTable = useTable({
- data: partners?.filter((p) => partnerIds.includes(p.id)) || [],
+ data: partners?.filter((p) => partnerIds?.includes(p.id)) || [],
columns: [
{
header: "Partner",
@@ -180,7 +174,7 @@ function RewardSheetContent({
onClick={() => {
setValue(
"partnerIds",
- partnerIds.filter((id) => id !== row.original.id),
+ (partnerIds || []).filter((id) => id !== row.original.id),
);
}}
/>
@@ -197,6 +191,10 @@ function RewardSheetContent({
});
const buttonDisabled = isPending || amount == null;
+ const hasDefaultReward = !!program?.defaultRewardId;
+ const displayAddPartnerButton =
+ (event !== "sale" && selectedPartnerType === "specific") ||
+ (event === "sale" && hasDefaultReward);
return (
<>
@@ -208,7 +206,7 @@ function RewardSheetContent({
<div>
<div className="flex items-start justify-between border-b border-neutral-200 p-6">
<Sheet.Title className="text-xl font-semibold">
- {`Create ${isDefault ? "default" : ""} ${event} reward`}
+ {`Create ${!program?.defaultRewardId ? "default" : ""} ${event} reward`}
</Sheet.Title>
<Sheet.Close asChild>
<Button
@@ -272,8 +270,9 @@ function RewardSheetContent({
onChange={(e) => {
if (e.target.checked) {
setSelectedPartnerType(partnerType.key);
+
if (partnerType.key === "all") {
- setValue("partnerIds", []);
+ setValue("partnerIds", null);
}
}
}}
@@ -297,40 +296,6 @@ function RewardSheetContent({
</div>
)}
- {selectedPartnerType === "specific" && !isDefault && (
- <div className="mt-2">
- <div className="flex items-center justify-between">
- <label className="text-sm font-medium text-neutral-800">
- Eligible partners
- </label>
- <Button
- text="Add partner"
- className="h-7 w-fit"
- onClick={() => setIsAddPartnersOpen(true)}
- />
- </div>
- <div className="mt-4">
- {partnerIds.length > 0 ? (
- <Table {...selectedPartnersTable} />
- ) : (
- <div className="flex flex-col items-center justify-center gap-4 rounded-lg bg-neutral-50 py-12">
- <div className="flex items-center justify-center">
- <Users className="size-6 text-neutral-800" />
- </div>
- <div className="flex flex-col items-center gap-1 px-4 text-center">
- <p className="text-base font-medium text-neutral-900">
- Eligible partners
- </p>
- <p className="text-sm text-neutral-600">
- No eligible partners added yet
- </p>
- </div>
- </div>
- )}
- </div>
- </div>
- )}
-
{event === "sale" && (
<>
<div>
@@ -347,70 +312,59 @@ function RewardSheetContent({
>
<div className="p-1">
<div className="grid grid-cols-1 gap-3 lg:grid-cols-2">
- {commissionTypes.map((commissionType) => {
- const isSelected =
- commissionDuration &&
- commissionDuration > 1 ===
- commissionType.recurring
- ? true
- : false;
-
- return (
- <label
- key={commissionType.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={commissionType.label}
- className="hidden"
- checked={isSelected}
- onChange={(e) => {
- if (e.target.checked) {
- setValue(
- "commissionDuration",
- commissionType.recurring ? 12 : 1,
- { shouldDirty: true },
- );
- }
- }}
- />
- <div className="flex grow flex-col text-sm">
- <span className="font-medium">
- {commissionType.label}
- </span>
- <span>{commissionType.description}</span>
- </div>
- <CircleCheckFill
+ {commissionTypes.map(
+ ({ label, description, recurring }) => {
+ const isSelected = isRecurring === recurring;
+
+ return (
+ <label
+ key={label}
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",
+ "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",
)}
- />
- </label>
- );
- })}
+ >
+ <input
+ type="radio"
+ value={label}
+ className="hidden"
+ checked={isSelected}
+ onChange={(e) => {
+ if (e.target.checked) {
+ setIsRecurring(recurring);
+
+ if (!recurring) {
+ setValue("maxDuration", "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>
<div
className={cn(
"transition-opacity duration-200",
- commissionDuration && commissionDuration > 1
- ? "h-auto"
- : "h-0 opacity-0",
+ isRecurring ? "h-auto" : "h-0 opacity-0",
)}
- aria-hidden={
- !(commissionDuration && commissionDuration > 1)
- }
+ aria-hidden={!isRecurring}
{...{
- inert: !(
- commissionDuration && commissionDuration > 1
- ),
+ inert: !isRecurring,
}}
>
<div className="pt-6">
@@ -423,16 +377,16 @@ function RewardSheetContent({
<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("commissionDuration")}
+ {...register("maxDuration")}
>
- <option value={INFINITY_NUMBER}>
- Lifetime
- </option>
- {[1, 3, 6, 12, 18, 24].map((v) => (
+ {RECURRING_MAX_DURATIONS.filter(
+ (v) => v !== "0",
+ ).map((v) => (
<option value={v} key={v}>
- {v} {pluralize("month", v)}
+ {v} {pluralize("month", Number(v))}
</option>
))}
+ <option value="">Lifetime</option>
</select>
</div>
</div>
@@ -504,6 +458,40 @@ function RewardSheetContent({
</div>
</>
)}
+
+ {displayAddPartnerButton && (
+ <div className="mt-2">
+ <div className="flex items-center justify-between">
+ <label className="text-sm font-medium text-neutral-800">
+ Eligible partners
+ </label>
+ <Button
+ text="Add partner"
+ className="h-7 w-fit"
+ onClick={() => setIsAddPartnersOpen(true)}
+ />
+ </div>
+ <div className="mt-4">
+ {(partnerIds || []).length > 0 ? (
+ <Table {...selectedPartnersTable} />
+ ) : (
+ <div className="flex flex-col items-center justify-center gap-4 rounded-lg bg-neutral-50 py-12">
+ <div className="flex items-center justify-center">
+ <Users className="size-6 text-neutral-800" />
+ </div>
+ <div className="flex flex-col items-center gap-1 px-4 text-center">
+ <p className="text-base font-medium text-neutral-900">
+ Eligible partners
+ </p>
+ <p className="text-sm text-neutral-600">
+ No eligible partners added yet
+ </p>
+ </div>
+ </div>
+ )}
+ </div>
+ </div>
+ )}
</div>
</div>
@@ -532,7 +520,7 @@ function RewardSheetContent({
<SelectEligiblePartnersSheet
isOpen={isAddPartnersOpen}
setIsOpen={setIsAddPartnersOpen}
- selectedPartnerIds={partnerIds}
+ selectedPartnerIds={partnerIds || []}
onSelect={(ids) => {
setValue("partnerIds", ids);
}}
@@ -562,7 +550,7 @@ export function useRewardSheet(
const [isOpen, setIsOpen] = useState(false);
return {
- rewardSheet: (
+ RewardSheet: (
<RewardSheet setIsOpen={setIsOpen} isOpen={isOpen} {...props} />
),
setIsOpen,