The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.
TypeScript clickhousemysqlnextjsopen-sourceplanetscaleprismatailwindcsstinybirdtypescriptupstashvercel
Add application form
Tim Wilsond6e99e5390d2
diff --git a/apps/web/app/partners.dub.co/(apply)/apply/[programSlug]/application/form.tsx b/apps/web/app/partners.dub.co/(apply)/apply/[programSlug]/application/form.tsxnew file mode 100644index 0000000000..7ab2208f51--- /dev/null+++ b/apps/web/app/partners.dub.co/(apply)/apply/[programSlug]/application/form.tsx@@ -0,0 +1,165 @@+"use client";++import { createProgramApplicationAction } from "@/lib/actions/partners/create-program-application";+import { Button, useMediaQuery } from "@dub/ui";+import { LoadingSpinner } from "@dub/ui/src/icons";+import { cn } from "@dub/utils";+import { Program } from "@prisma/client";+import { useAction } from "next-safe-action/hooks";+import { useRouter } from "next/navigation";+import { useForm } from "react-hook-form";+import ReactTextareaAutosize from "react-textarea-autosize";+import { toast } from "sonner";++type FormData = {+ name: string;+ email: string;+ website?: string;+ plan: string;+ comments?: string;+};++export function ProgramApplicationForm({+ program,+}: {+ program: Pick<Program, "id" | "slug" | "name">;+}) {+ const { isMobile } = useMediaQuery();+ const router = useRouter();++ const {+ register,+ handleSubmit,+ formState: { errors, isSubmitting, isSubmitSuccessful },+ } = useForm<FormData>();++ const { executeAsync } = useAction(createProgramApplicationAction);++ const isLoading = isSubmitting || isSubmitSuccessful;++ return (+ <form+ onSubmit={handleSubmit(async (data) => {+ const response = await executeAsync({+ programId: program.id,+ ...data,+ });++ if (!response?.data?.ok) {+ toast.error(+ response?.data?.message ?? "Failed to submit application",+ );+ return;+ }++ toast.success("Application submitted successfully");+ router.push(`/apply/${program.slug}/application/success`);+ })}+ className="flex flex-col gap-6"+ >+ <label>+ <span className="text-sm font-medium text-gray-800">Name</span>+ <input+ type="text"+ className={cn(+ "mt-2 block w-full rounded-md focus:outline-none sm:text-sm",+ errors.name+ ? "border-red-400 pr-10 text-red-900 placeholder-red-300 focus:border-red-500 focus:ring-red-500"+ : "border-gray-300 text-gray-900 placeholder-gray-400 focus:border-gray-500 focus:ring-gray-500",+ )}+ placeholder="Acme, Inc."+ autoFocus={!isMobile}+ {...register("name", {+ required: true,+ })}+ />+ </label>++ <label>+ <span className="text-sm font-medium text-gray-800">Email</span>+ <input+ type="text"+ className={cn(+ "mt-2 block w-full rounded-md focus:outline-none sm:text-sm",+ errors.email+ ? "border-red-400 pr-10 text-red-900 placeholder-red-300 focus:border-red-500 focus:ring-red-500"+ : "border-gray-300 text-gray-900 placeholder-gray-400 focus:border-gray-500 focus:ring-gray-500",+ )}+ placeholder="[email protected]"+ autoFocus={!isMobile}+ {...register("email", {+ required: true,+ })}+ />+ </label>++ <label>+ <span className="text-sm font-medium text-gray-800">+ Website / Social media channel+ <span className="font-normal text-neutral-500"> (optional)</span>+ </span>+ <input+ type="text"+ className={cn(+ "mt-2 block w-full rounded-md focus:outline-none sm:text-sm",+ errors.website+ ? "border-red-400 pr-10 text-red-900 placeholder-red-300 focus:border-red-500 focus:ring-red-500"+ : "border-gray-300 text-gray-900 placeholder-gray-400 focus:border-gray-500 focus:ring-gray-500",+ )}+ placeholder="https://example.com"+ autoFocus={!isMobile}+ {...register("website")}+ />+ </label>++ <label>+ <span className="text-sm font-medium text-gray-800">+ How do you plan to promote {program.name}?+ </span>+ <ReactTextareaAutosize+ className={cn(+ "mt-2 block max-h-48 min-h-12 w-full rounded-md focus:outline-none sm:text-sm",+ errors.plan+ ? "border-red-400 pr-10 text-red-900 placeholder-red-300 focus:border-red-500 focus:ring-red-500"+ : "border-gray-300 text-gray-900 placeholder-gray-400 focus:border-gray-500 focus:ring-gray-500",+ )}+ placeholder=""+ minRows={3}+ {...register("plan", { required: true })}+ />+ </label>++ <label>+ <span className="text-sm font-medium text-gray-800">+ Any additional questions or comments?+ <span className="font-normal text-neutral-500"> (optional)</span>+ </span>+ <ReactTextareaAutosize+ className={cn(+ "mt-2 block max-h-48 min-h-12 w-full rounded-md focus:outline-none sm:text-sm",+ errors.comments+ ? "border-red-400 pr-10 text-red-900 placeholder-red-300 focus:border-red-500 focus:ring-red-500"+ : "border-gray-300 text-gray-900 placeholder-gray-400 focus:border-gray-500 focus:ring-gray-500",+ )}+ placeholder=""+ minRows={3}+ {...register("comments")}+ />+ </label>++ <Button+ text="Submit application"+ className={cn(+ "mt-4 border-[var(--brand)] bg-[var(--brand)] hover:bg-[var(--brand)] hover:ring-[var(--brand-ring)]",+ isLoading && "text-white/70",+ )}+ icon={+ isLoading ? (+ <LoadingSpinner className="opacity-70 brightness-200" />+ ) : null+ }+ disabled={isLoading}+ />+ </form>+ );+}diff --git a/apps/web/app/partners.dub.co/(apply)/apply/[programSlug]/application/page.tsx b/apps/web/app/partners.dub.co/(apply)/apply/[programSlug]/application/page.tsxnew file mode 100644index 0000000000..cf647825f4--- /dev/null+++ b/apps/web/app/partners.dub.co/(apply)/apply/[programSlug]/application/page.tsx@@ -0,0 +1,74 @@+import { prisma } from "@/lib/prisma";+import { notFound } from "next/navigation";+import { CSSProperties } from "react";+import { DetailsGrid } from "../details-grid";+import { Header } from "../header";+import { ProgramApplicationForm } from "./form";++export default async function ApplicationPage({+ params,+}: {+ params: { programSlug: string };+}) {+ const { programSlug } = params;+ const program = await prisma.program.findUnique({+ select: {+ id: true,+ name: true,+ slug: true,+ logo: true,+ wordmark: true,+ brandColor: true,+ commissionType: true,+ commissionAmount: true,+ isLifetimeRecurring: true,+ },+ where: {+ slug: programSlug,+ },+ });++ if (!program) notFound();++ return (+ <div+ className="relative"+ style={+ {+ "--brand": program.brandColor || "#3b82f6",+ "--brand-ring": "rgb(from var(--brand) r g b / 0.4)",+ } as CSSProperties+ }+ >+ <Header+ program={{+ logo: program.logo,+ wordmark: program.wordmark,+ }}+ slug={programSlug}+ />+ <div className="p-6">+ {/* Hero section */}+ <div className="grid grid-cols-1 gap-5 sm:pt-20">+ <p className="font-mono text-xs font-medium uppercase text-[var(--brand)]">+ Affiliate Program+ </p>+ <h1 className="text-4xl font-semibold">{program.name} application</h1>+ <p className="text-base text-neutral-700">+ Submit your application to join the affiliate program.+ </p>+ </div>++ {/* Program details grid */}+ <DetailsGrid program={program} className="mt-10" />++ {/* Application form */}+ <div className="mt-10">+ <ProgramApplicationForm+ program={{ id: program.id, name: program.name, slug: program.slug }}+ />+ </div>+ </div>+ </div>+ );+}diff --git a/apps/web/app/partners.dub.co/(apply)/apply/[programSlug]/application/success/page.tsx b/apps/web/app/partners.dub.co/(apply)/apply/[programSlug]/application/success/page.tsxnew file mode 100644index 0000000000..f3b1b43454--- /dev/null+++ b/apps/web/app/partners.dub.co/(apply)/apply/[programSlug]/application/success/page.tsx@@ -0,0 +1,58 @@+import { prisma } from "@/lib/prisma";+import { notFound } from "next/navigation";+import { CSSProperties } from "react";+import { Header } from "../../header";++export default async function SuccessPage({+ params,+}: {+ params: { programSlug: string };+}) {+ const { programSlug } = params;+ const program = await prisma.program.findUnique({+ select: {+ id: true,+ name: true,+ slug: true,+ logo: true,+ wordmark: true,+ brandColor: true,+ commissionType: true,+ commissionAmount: true,+ isLifetimeRecurring: true,+ },+ where: {+ slug: programSlug,+ },+ });++ if (!program) notFound();++ return (+ <div+ className="relative"+ style={+ {+ "--brand": program.brandColor || "#3b82f6",+ "--brand-ring": "rgb(from var(--brand) r g b / 0.4)",+ } as CSSProperties+ }+ >+ <Header+ program={{+ logo: program.logo,+ wordmark: program.wordmark,+ }}+ slug={programSlug}+ />+ <div className="p-6">+ <div className="grid grid-cols-1 gap-5 sm:pt-20">+ <h1 className="text-4xl font-semibold">Application submitted</h1>+ <p className="text-base text-neutral-700">+ Your application has been submitted for review.+ </p>+ </div>+ </div>+ </div>+ );+}diff --git a/apps/web/app/partners.dub.co/(apply)/apply/[programSlug]/header.tsx b/apps/web/app/partners.dub.co/(apply)/apply/[programSlug]/header.tsxindex 21835f9f39..83ec21d435 100644--- a/apps/web/app/partners.dub.co/(apply)/apply/[programSlug]/header.tsx+++ b/apps/web/app/partners.dub.co/(apply)/apply/[programSlug]/header.tsx@@ -3,11 +3,14 @@import { Button, useScroll, Wordmark } from "@dub/ui";import { cn } from "@dub/utils";import { Program } from "@prisma/client";+import Link from "next/link";export function Header({program,+ slug,}: {program: Pick<Program, "wordmark" | "logo">;+ slug: string;}) {const scrolled = useScroll(0);@@ -25,14 +28,16 @@ export function Header({)}/>- {program.wordmark || program.logo ? (- <img- className="h-7 max-w-32"- src={(program.wordmark ?? program.logo) as string}- />- ) : (- <Wordmark className="h-7" />- )}+ <Link href={`/apply/${slug}`}>+ {program.wordmark || program.logo ? (+ <img+ className="h-7 max-w-32"+ src={(program.wordmark ?? program.logo) as string}+ />+ ) : (+ <Wordmark className="h-7" />+ )}+ </Link><Buttontype="button"text="Apply"diff --git a/apps/web/app/partners.dub.co/(apply)/apply/[programSlug]/page.tsx b/apps/web/app/partners.dub.co/(apply)/apply/[programSlug]/page.tsxindex aeb470ca3d..e8764276f5 100644--- a/apps/web/app/partners.dub.co/(apply)/apply/[programSlug]/page.tsx+++ b/apps/web/app/partners.dub.co/(apply)/apply/[programSlug]/page.tsx@@ -61,7 +61,10 @@ export default async function ApplyPage({} as CSSProperties}>- <Header program={{ logo: program.logo, wordmark: program.wordmark }} />+ <Header+ program={{ logo: program.logo, wordmark: program.wordmark }}+ slug={programSlug}+ /><div className="p-6">{/* Hero section */}<div className="grid grid-cols-1 gap-5 sm:pt-20">diff --git a/apps/web/lib/actions/partners/create-program-application.ts b/apps/web/lib/actions/partners/create-program-application.tsnew file mode 100644index 0000000000..64539c61c6--- /dev/null+++ b/apps/web/lib/actions/partners/create-program-application.ts@@ -0,0 +1,73 @@+"use server";++import { getIP } from "@/lib/api/utils";+import { prisma } from "@/lib/prisma";+import { ratelimit } from "@/lib/upstash";+import { ProgramApplicationStatus } from "@prisma/client";+import z from "../../zod";+import { actionClient } from "../safe-action";++const createProgramApplicationSchema = z.object({+ programId: z.string(),+ name: z.string(),+ email: z.string(),+ website: z.string().optional(),+ plan: z.string(),+ comments: z.string().optional(),+});++// Onboard a new partner+export const createProgramApplicationAction = actionClient+ .schema(createProgramApplicationSchema)+ .action(async ({ parsedInput }) => {+ const { programId, ...data } = parsedInput;++ // Limit to 2 requests per minute per program per IP+ const { success } = await ratelimit(2, "1 m").limit(+ `create-program-application:${programId}:${getIP()}`,+ );++ if (!success)+ return {+ ok: false,+ message: "Too many requests. Please try again later.",+ };++ try {+ const program = await prisma.program.findUnique({+ where: { id: programId },+ });++ if (!program) return { ok: false, message: "Program not found." };++ const existingApplication = await prisma.programApplication.findFirst({+ where: {+ programId: program.id,+ email: data.email,+ status: {+ not: ProgramApplicationStatus.rejected,+ },+ },+ });++ if (existingApplication)+ return {+ ok: false,+ message: "You have already applied to this program.",+ };++ const application = await prisma.programApplication.create({+ data: {+ ...parsedInput,+ programId: program.id,+ },+ });++ return { ok: true, programApplicationId: application.id };+ } catch (e) {+ console.error(e);+ return {+ ok: false,+ };+ }+ });diff --git a/apps/web/prisma/schema/partner.prisma b/apps/web/prisma/schema/partner.prismaindex bb911846ab..2491880883 100644--- a/apps/web/prisma/schema/partner.prisma+++ b/apps/web/prisma/schema/partner.prisma@@ -26,6 +26,7 @@ model Partner {invites PartnerInvite[]payouts Payout[]sales Sale[]+ applications ProgramApplication[]}model PartnerUser {diff --git a/apps/web/prisma/schema/program.prisma b/apps/web/prisma/schema/program.prismaindex be5acb2d62..40bd783162 100644--- a/apps/web/prisma/schema/program.prisma+++ b/apps/web/prisma/schema/program.prisma@@ -46,11 +46,12 @@ model Program {createdAt DateTime @default(now())updatedAt DateTime @updatedAt- workspace Project @relation(fields: [workspaceId], references: [id])- partners ProgramEnrollment[]- payouts Payout[]- sales Sale[]- resources ProgramResource[]+ workspace Project @relation(fields: [workspaceId], references: [id])+ partners ProgramEnrollment[]+ payouts Payout[]+ sales Sale[]+ resources ProgramResource[]+ applications ProgramApplication[]@@index([workspaceId])}@@ -73,6 +74,32 @@ model ProgramEnrollment {@@index([linkId])}+enum ProgramApplicationStatus {+ pending+ approved+ rejected+}++model ProgramApplication {+ id String @id @default(cuid())+ programId String+ partnerId String?++ name String+ email String+ plan String+ website String?+ comments String?++ status ProgramApplicationStatus @default(pending)++ program Program @relation(fields: [programId], references: [id])+ partner Partner? @relation(fields: [partnerId], references: [id])++ @@index([programId])+ @@index([partnerId])+}+// TODO: Haven't finalized the resource table yet.model ProgramResource {id String @id @default(cuid())