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.

wip createClawbackAction

Kiran K63dc53b26258
diff --git a/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/commissions/create-clawback-sheet.tsx b/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/commissions/create-clawback-sheet.tsx
index c85bf020e0..54eb6bff7f 100644
--- a/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/commissions/create-clawback-sheet.tsx
+++ b/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/commissions/create-clawback-sheet.tsx
@@ -1,3 +1,5 @@
+import { createClawbackAction } from "@/lib/actions/partners/create-clawback";
+import { mutatePrefix } from "@/lib/swr/mutate";
import useWorkspace from "@/lib/swr/use-workspace";
import {
CLAWBACK_REASONS,
@@ -6,8 +8,10 @@ import {
import { PartnerSelector } from "@/ui/partners/partner-selector";
import { X } from "@/ui/shared/icons";
import { Button, Sheet } from "@dub/ui";
+import { useAction } from "next-safe-action/hooks";
import { useState } from "react";
import { Controller, useForm } from "react-hook-form";
+import { toast } from "sonner";
import { z } from "zod";
interface CreateClawbackSheetProps {
@@ -22,26 +26,53 @@ function CreateClawbackSheetContent(
props: Omit<CreateClawbackSheetProps, "nested">,
) {
const { setIsOpen } = props;
- const { id: workspaceId } = useWorkspace();
+ const { id: workspaceId, defaultProgramId } = useWorkspace();
const {
control,
handleSubmit,
- formState: { errors },
- setValue,
+ reset,
+ watch,
+ getValues,
+ formState: { errors, isSubmitting, isSubmitSuccessful },
} = useForm<FormData>({
defaultValues: {
- partnerId: "",
- amount: undefined,
- reason: "",
+ description: "",
},
});
- // TODO: Implement submit logic
- const onSubmit = (data: FormData) => {
- // TODO: Handle submit action
+ const [partnerId, amount, description] = watch([
+ "partnerId",
+ "amount",
+ "description",
+ ]);
+
+ const { executeAsync, isPending } = useAction(createClawbackAction, {
+ onSuccess: () => {
+ toast.success("A clawback has been created for the partner!");
+ setIsOpen(false);
+ mutatePrefix(`/api/commissions?workspaceId=${workspaceId}`);
+ const currentValues = getValues();
+ reset(currentValues);
+ },
+ onError({ error }) {
+ toast.error(error.serverError || "Failed to create clawback.");
+ },
+ });
+
+ const onSubmit = async (data: FormData) => {
+ if (!workspaceId || !defaultProgramId) {
+ return;
+ }
+
+ await executeAsync({
+ ...data,
+ workspaceId,
+ });
};
+ const disableSubmitButton = !partnerId || !amount || !description;
+
return (
<form onSubmit={handleSubmit(onSubmit)} className="flex h-full flex-col">
<div className="sticky top-0 z-10 border-b border-neutral-200 bg-white">
@@ -134,19 +165,19 @@ function CreateClawbackSheetContent(
<div>
<label
- htmlFor="reason"
+ htmlFor="description"
className="text-sm font-medium text-neutral-900"
>
Reason
</label>
<div className="relative mt-2 rounded-md shadow-sm">
<Controller
- name="reason"
+ name="description"
control={control}
rules={{ required: true }}
render={({ field }) => (
<select
- id="reason"
+ id="description"
className="block w-full rounded-md border-neutral-300 pr-10 text-neutral-900 placeholder-neutral-400 focus:border-neutral-500 focus:outline-none focus:ring-neutral-500 sm:text-sm"
value={field.value}
onChange={field.onChange}
@@ -162,9 +193,9 @@ function CreateClawbackSheetContent(
</select>
)}
/>
- {errors.reason && (
+ {errors.description && (
<span className="text-xs text-red-600">
- {errors.reason.message}
+ {errors.description.message}
</span>
)}
</div>
@@ -179,13 +210,15 @@ function CreateClawbackSheetContent(
onClick={() => setIsOpen(false)}
text="Cancel"
className="w-fit"
+ disabled={isPending || isSubmitting || isSubmitSuccessful}
/>
<Button
type="submit"
variant="primary"
text="Create clawback"
className="w-fit"
- // disabled={...} // Optionally disable if fields are empty
+ loading={isPending || isSubmitting || isSubmitSuccessful}
+ disabled={disableSubmitButton}
/>
</div>
</div>
diff --git a/apps/web/lib/actions/partners/create-clawback.ts b/apps/web/lib/actions/partners/create-clawback.ts
index 6699aea13b..f04ac0512d 100644
--- a/apps/web/lib/actions/partners/create-clawback.ts
+++ b/apps/web/lib/actions/partners/create-clawback.ts
@@ -1,8 +1,10 @@
"use server";
+import { createId } from "@/lib/api/create-id";
import { getDefaultProgramIdOrThrow } from "@/lib/api/programs/get-default-program-id-or-throw";
import { getProgramEnrollmentOrThrow } from "@/lib/api/programs/get-program-enrollment-or-throw";
import { createClawbackSchema } from "@/lib/zod/schemas/commissions";
+import { prisma } from "@dub/prisma";
import { authActionClient } from "../safe-action";
export const createClawbackAction = authActionClient
@@ -11,12 +13,22 @@ export const createClawbackAction = authActionClient
const { workspace } = ctx;
const programId = getDefaultProgramIdOrThrow(workspace);
- const { partnerId, amount, reason } = parsedInput;
+ const { partnerId, amount, description } = parsedInput;
await getProgramEnrollmentOrThrow({
programId,
partnerId,
});
- // TODO: Implement clawback logic
+ await prisma.commission.create({
+ data: {
+ id: createId({ prefix: "cm_" }),
+ programId,
+ partnerId,
+ description,
+ type: "custom",
+ amount: -amount,
+ quantity: 1,
+ },
+ });
});
diff --git a/apps/web/lib/zod/schemas/commissions.ts b/apps/web/lib/zod/schemas/commissions.ts
index 0af1954aa1..7041a12d8b 100644
--- a/apps/web/lib/zod/schemas/commissions.ts
+++ b/apps/web/lib/zod/schemas/commissions.ts
@@ -179,7 +179,8 @@ export const CLAWBACK_REASONS = [
];
export const createClawbackSchema = z.object({
+ workspaceId: z.string(),
partnerId: z.string(),
amount: z.number().min(0, "Amount must be positive."),
- reason: z.string(),
-});
\ No newline at end of file
+ description: z.enum(CLAWBACK_REASONS.map((r) => r.value) as [string, ...string[]]),
+});