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 manual payout creation: update payout schema to handle optional partnerId, improve amount parsing, and enhance UI with partner selection combobox.

Kiran Ke9b60dab6ee1
diff --git a/apps/web/lib/actions/partners/create-manual-payout.ts b/apps/web/lib/actions/partners/create-manual-payout.ts
index 634a9d1bcb..ac967564d9 100644
--- a/apps/web/lib/actions/partners/create-manual-payout.ts
+++ b/apps/web/lib/actions/partners/create-manual-payout.ts
@@ -26,7 +26,7 @@ export const createManualPayoutAction = authActionClient
const { programId, partnerId, start, end, type, amount, description } =
parsedInput;
- const [program, programEnrollment] = await Promise.all([
+ const [_program, programEnrollment] = await Promise.all([
getProgramOrThrow({
workspaceId: workspace.id,
programId,
diff --git a/apps/web/lib/zod/schemas/payouts.ts b/apps/web/lib/zod/schemas/payouts.ts
index b6c757b38e..19874b6e43 100644
--- a/apps/web/lib/zod/schemas/payouts.ts
+++ b/apps/web/lib/zod/schemas/payouts.ts
@@ -12,10 +12,12 @@ export const createManualPayoutSchema = z.object({
partnerId: z.string({ required_error: "Please select a partner" }),
start: parseDateSchema.optional(),
end: parseDateSchema.optional(),
- amount: z.preprocess(
- (val) => parseFloat(val as string),
- z.number().default(0),
- ),
+ amount: z
+ .preprocess((val) => {
+ const parsed = parseFloat(val as string);
+ return isNaN(parsed) ? 0 : parsed;
+ }, z.number())
+ .optional(),
type: z.nativeEnum(PayoutType),
description: z
.string()
diff --git a/apps/web/ui/partners/create-payout-sheet.tsx b/apps/web/ui/partners/create-payout-sheet.tsx
index 47471e7c55..951d378b44 100644
--- a/apps/web/ui/partners/create-payout-sheet.tsx
+++ b/apps/web/ui/partners/create-payout-sheet.tsx
@@ -10,7 +10,13 @@ import useWorkspace from "@/lib/swr/use-workspace";
import { createManualPayoutSchema } from "@/lib/zod/schemas/payouts";
import { X } from "@/ui/shared/icons";
import { PayoutType } from "@dub/prisma/client";
-import { Button, DateRangePicker, Sheet, useEnterSubmit } from "@dub/ui";
+import {
+ Button,
+ Combobox,
+ DateRangePicker,
+ Sheet,
+ useEnterSubmit,
+} from "@dub/ui";
import {
capitalize,
cn,
@@ -48,7 +54,7 @@ import { z } from "zod";
interface CreatePayoutSheetProps {
setIsOpen: Dispatch<SetStateAction<boolean>>;
- partnerId: string;
+ partnerId?: string;
}
const schema = createManualPayoutSchema
@@ -56,6 +62,7 @@ const schema = createManualPayoutSchema
type: true,
amount: true,
description: true,
+ partnerId: true,
})
.and(
z.object({
@@ -75,17 +82,15 @@ const schema = createManualPayoutSchema
type FormData = z.infer<typeof schema>;
-function CreatePayoutSheetContent({
- setIsOpen,
- partnerId,
-}: CreatePayoutSheetProps) {
+function CreatePayoutSheetContent(props: CreatePayoutSheetProps) {
+ const { setIsOpen } = props;
+
const dateRangePickerId = useId();
const { program } = useProgram();
const { data: partners } = usePartners();
const { id: workspaceId } = useWorkspace();
const router = useRouter();
const { slug, programId } = useParams();
-
const formRef = useRef<HTMLFormElement>(null);
const { handleKeyDown } = useEnterSubmit(formRef);
@@ -99,14 +104,31 @@ function CreatePayoutSheetContent({
} = useForm<FormData>({
defaultValues: {
type: "custom",
+ partnerId: props.partnerId,
},
});
- const { type: payoutType, start, end, amount } = watch();
+ const { type: payoutType, start, end, amount, partnerId } = watch();
const isPercentageBased =
payoutType === "sales" && program?.commissionType === "percentage";
+ const partnerOptions = useMemo(() => {
+ return partners?.map((partner) => ({
+ value: partner.id,
+ label: partner.name,
+ icon: (
+ <img
+ src={
+ partner.image ||
+ `https://api.dicebear.com/9.x/micah/svg?seed=${partner.id}`
+ }
+ className="size-4 rounded-full"
+ />
+ ),
+ }));
+ }, [partners]);
+
const { executeAsync, isExecuting } = useAction(createManualPayoutAction, {
onSuccess: async (res) => {
toast.success("Successfully created payout!");
@@ -122,12 +144,13 @@ function CreatePayoutSheetContent({
}
},
onError({ error }) {
+ console.log(error);
toast.error(error.serverError);
},
});
const onSubmit = async (data: FormData) => {
- if (workspaceId && program) {
+ if (workspaceId && program && partnerId) {
const startDate = data.start
? data.start.getTime() - data.start.getTimezoneOffset() * 60000
: undefined;
@@ -141,7 +164,9 @@ function CreatePayoutSheetContent({
programId: program.id,
start: startDate ? new Date(startDate).toISOString() : undefined,
end: endDate ? new Date(endDate).toISOString() : undefined,
- amount: isPercentageBased ? amount : amount * 100,
+ ...(payoutType != "sales" && {
+ amount: amount! * 100,
+ }),
partnerId,
});
}
@@ -202,21 +227,19 @@ function CreatePayoutSheetContent({
fetcher,
);
+ // Calculate payout amount
const payoutAmount = useMemo(() => {
- const quantity =
- payoutType === "sales" ? salesCount?.pending : totalEvents?.[payoutType];
-
if (payoutType === "custom") {
return amount;
}
+ const quantity =
+ payoutType === "sales" ? salesCount?.pending : totalEvents?.[payoutType];
+
if (isPercentageBased && salesAmount?.amount) {
return (
calculateEarnings({
- program: {
- commissionAmount: amount,
- commissionType: "percentage",
- },
+ program,
partner: selectedPartner,
sales: 1,
saleAmount: salesAmount.amount,
@@ -235,6 +258,7 @@ function CreatePayoutSheetContent({
program,
]);
+ // Invoice summary
const invoiceData = useMemo(() => {
const quantity =
payoutType === "sales" ? salesCount?.pending : totalEvents?.[payoutType];
@@ -290,7 +314,7 @@ function CreatePayoutSheetContent({
: "-",
}),
- ...(isPercentageBased && {
+ ...(payoutType === "sales" && {
Revenue: isValidatingSalesAmount ? (
<div className="h-4 w-12 animate-pulse rounded-md bg-neutral-200" />
) : salesAmount?.amount ? (
@@ -356,6 +380,41 @@ function CreatePayoutSheetContent({
</Sheet.Close>
</div>
<div className="flex flex-col gap-4 p-6">
+ <div className="flex flex-col gap-2">
+ <label className="text-sm font-medium text-gray-900">
+ Partner
+ <span className="font-normal text-neutral-500"> (required)</span>
+ </label>
+ <Combobox
+ selected={
+ partnerOptions?.find((o) => o.value === partnerId) ?? null
+ }
+ setSelected={(option) => {
+ if (option) {
+ setValue("partnerId", option.value);
+ clearErrors("partnerId");
+ }
+ }}
+ options={partnerOptions}
+ caret={true}
+ placeholder="Select partners"
+ searchPlaceholder="Search..."
+ matchTriggerWidth
+ buttonProps={{
+ className: cn(
+ "w-full justify-start border-gray-300 px-3",
+ "data-[state=open]:ring-1 data-[state=open]:ring-gray-500 data-[state=open]:border-gray-500",
+ "focus:ring-1 focus:ring-gray-500 focus:border-gray-500 transition-none",
+ !partnerId && "text-gray-400",
+ errors.partnerId && "border-red-500",
+ ),
+ }}
+ />
+ {errors.partnerId && (
+ <p className="text-xs text-red-600">{errors.partnerId.message}</p>
+ )}
+ </div>
+
<div className="flex flex-col gap-2">
<label
htmlFor={dateRangePickerId}