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.

add RequestPartnerInviteForm

Kiran Kc849e53cbe5f
diff --git a/apps/web/app/api/referrals/invite/route.ts b/apps/web/app/api/referrals/invite/route.ts
index 1b3fbf3ec4..2c48617913 100644
--- a/apps/web/app/api/referrals/invite/route.ts
+++ b/apps/web/app/api/referrals/invite/route.ts
@@ -8,23 +8,41 @@ import { sendEmail } from "emails";
import PartnerInvite from "emails/partner-invite";
import { NextResponse } from "next/server";
+// GET /api/referrals/invite - check if a partner is already invited to a program
+export const GET = withAuth(async ({ link, program }) => {
+ const [programEnrollment, programInvite] = await Promise.all([
+ prisma.programEnrollment.findFirst({
+ where: {
+ programId: program.id,
+ linkId: link.id,
+ },
+ }),
+
+ prisma.programInvite.findFirst({
+ where: {
+ programId: program.id,
+ linkId: link.id,
+ },
+ }),
+ ]);
+
+ return NextResponse.json({
+ programEnrollment,
+ programInvite,
+ });
+});
+
// POST /api/referrals/invite - invite a partner to dub partners
export const POST = withAuth(async ({ req, link, program, workspace }) => {
- const { email } = requestPartnerInviteSchema.parse(
- await parseRequestBody(req),
- );
-
// TODO:
// We're repating the same logic in the invite-partner action.
// We should move it to a shared location if it makes sense.
- const [
- programEnrollment,
- programInvite,
- linkInProgramEnrollment,
- linkInProgramInvite,
- tags,
- ] = await Promise.all([
+ const { email } = requestPartnerInviteSchema.parse(
+ await parseRequestBody(req),
+ );
+
+ const [programEnrollment, programInvite, tags] = await Promise.all([
prisma.programEnrollment.findFirst({
where: {
programId: program.id,
@@ -49,18 +67,6 @@ export const POST = withAuth(async ({ req, link, program, workspace }) => {
},
}),
- prisma.programEnrollment.findUnique({
- where: {
- linkId: link.id,
- },
- }),
-
- prisma.programInvite.findUnique({
- where: {
- linkId: link.id,
- },
- }),
-
prisma.tag.findMany({
where: {
links: {
@@ -73,18 +79,14 @@ export const POST = withAuth(async ({ req, link, program, workspace }) => {
]);
if (programEnrollment) {
- throw new Error(`Partner ${email} already enrolled in this program.`);
+ throw new Error("You're already enrolled in this program.");
}
if (programInvite) {
- throw new Error(`Partner ${email} already invited to this program.`);
- }
-
- if (linkInProgramEnrollment || linkInProgramInvite) {
- throw new Error("Link is already associated with another partner.");
+ throw new Error("You've already been invited to this program.");
}
- const [result, _] = await Promise.all([
+ const [programInvited] = await Promise.all([
prisma.programInvite.create({
data: {
id: createId({ prefix: "pgi_" }),
@@ -110,18 +112,20 @@ export const POST = withAuth(async ({ req, link, program, workspace }) => {
workspace_id: workspace.id,
deleted: false,
}),
- ]);
- await sendEmail({
- subject: `${program.name} invited you to join Dub Partners`,
- email,
- react: PartnerInvite({
+ sendEmail({
+ subject: `${program.name} invited you to join Dub Partners`,
email,
- appName: `${process.env.NEXT_PUBLIC_APP_NAME}`,
- programName: program.name,
- programLogo: program.logo,
+ react: PartnerInvite({
+ email,
+ appName: `${process.env.NEXT_PUBLIC_APP_NAME}`,
+ program: {
+ name: program.name,
+ logo: program.logo,
+ },
+ }),
}),
- });
+ ]);
- return NextResponse.json(result);
+ return NextResponse.json(programInvited);
});
diff --git a/apps/web/app/app.dub.co/embed/page-client.tsx b/apps/web/app/app.dub.co/embed/page-client.tsx
index ab2e38a30e..6cbfc932e5 100644
--- a/apps/web/app/app.dub.co/embed/page-client.tsx
+++ b/apps/web/app/app.dub.co/embed/page-client.tsx
@@ -16,8 +16,11 @@ import {
Wordmark,
} from "@dub/ui";
import { Copy, MoneyBill2 } from "@dub/ui/src/icons";
-import { getPrettyUrl } from "@dub/utils";
+import { fetcher, getPrettyUrl } from "@dub/utils";
+import { ProgramEnrollment, ProgramInvite } from "@prisma/client";
import { useContext } from "react";
+import useSWR from "swr";
+import { RequestPartnerInviteForm } from "./request-partner-invite-form";
import { SaleTable } from "./sale-table";
import useReferralAnalytics from "./use-referral-analytics";
import { useReferralLink } from "./use-referral-link";
@@ -39,6 +42,13 @@ export function ReferralsEmbedPageClient() {
interval?: IntervalOptions;
};
+ const { data, isLoading } = useSWR<{
+ programEnrollment: ProgramEnrollment;
+ programInvite: ProgramInvite;
+ }>(`/api/referrals/invite`, fetcher);
+
+ const hasEnrolled = data?.programEnrollment;
+ const hasInvited = data?.programInvite;
const color = program?.brandColor || "#8B5CF6";
return (
@@ -121,6 +131,11 @@ export function ReferralsEmbedPageClient() {
</ProgramOverviewContext.Provider>
</div>
+ {/* TODO: UI needs tweaking */}
+ {!isLoading && !hasEnrolled && !hasInvited && (
+ <RequestPartnerInviteForm />
+ )}
+
<div className="flex items-center justify-center">
<a
href="https://d.to/conversions"
diff --git a/apps/web/app/app.dub.co/embed/request-partner-invite-form.tsx b/apps/web/app/app.dub.co/embed/request-partner-invite-form.tsx
new file mode 100644
index 0000000000..d6ba09bf6c
--- /dev/null
+++ b/apps/web/app/app.dub.co/embed/request-partner-invite-form.tsx
@@ -0,0 +1,61 @@
+import { requestPartnerInviteSchema } from "@/lib/dots/schemas";
+import z from "@/lib/zod";
+import { Button } from "@dub/ui";
+import { zodResolver } from "@hookform/resolvers/zod";
+import { useForm } from "react-hook-form";
+import { toast } from "sonner";
+import { mutate } from "swr";
+
+type RequestPartnerInvite = z.infer<typeof requestPartnerInviteSchema>;
+
+export const RequestPartnerInviteForm = () => {
+ const {
+ register,
+ handleSubmit,
+ formState: { isSubmitting },
+ } = useForm<RequestPartnerInvite>({
+ resolver: zodResolver(requestPartnerInviteSchema),
+ });
+
+ const onSubmit = async (data: RequestPartnerInvite) => {
+ const response = await fetch("/api/referrals/invite", {
+ method: "POST",
+ body: JSON.stringify(data),
+ });
+
+ if (!response.ok) {
+ const { error } = await response.json();
+ toast.error(error.message);
+ return;
+ }
+
+ mutate("/api/referrals/invite");
+ toast.success(
+ "Invite request sent successfully! Check your email for the invite link.",
+ );
+ };
+
+ return (
+ <form
+ onSubmit={handleSubmit(onSubmit)}
+ className="mt-4 rounded-lg border border-neutral-300 p-5"
+ >
+ <div className="flex gap-2">
+ <input
+ {...register("email")}
+ className="flex-grow rounded-md border-gray-300 text-gray-900 placeholder-gray-400 focus:border-gray-500 focus:outline-none focus:ring-gray-500 sm:text-sm"
+ autoFocus
+ required
+ placeholder="Enter your email"
+ />
+
+ <Button
+ type="submit"
+ text="Request invite"
+ className="h-10 w-fit"
+ loading={isSubmitting}
+ />
+ </div>
+ </form>
+ );
+};
diff --git a/apps/web/app/app.dub.co/embed/request-partner-invite-modal.tsx b/apps/web/app/app.dub.co/embed/request-partner-invite-modal.tsx
deleted file mode 100644
index a34ba96ece..0000000000
--- a/apps/web/app/app.dub.co/embed/request-partner-invite-modal.tsx
+++ /dev/null
@@ -1,135 +0,0 @@
-import { requestPartnerInviteSchema } from "@/lib/dots/schemas";
-import z from "@/lib/zod";
-import { Button, Modal } from "@dub/ui";
-import { zodResolver } from "@hookform/resolvers/zod";
-import { useCallback, useMemo, useState } from "react";
-import { useForm } from "react-hook-form";
-import { toast } from "sonner";
-
-type RequestPartnerInvite = z.infer<typeof requestPartnerInviteSchema>;
-
-interface RequestPartnerInviteProps {
- showModal: boolean;
- setShowModal: (showModal: boolean) => void;
-}
-
-interface RequestPartnerInviteFormProps {
- closeModal: () => void;
-}
-
-const RequestPartnerInvite = ({
- showModal,
- setShowModal,
-}: RequestPartnerInviteProps) => {
- return (
- <Modal
- showModal={showModal}
- setShowModal={setShowModal}
- drawerRootProps={{ repositionInputs: false }}
- >
- <h3 className="border-b border-gray-200 px-4 py-4 text-lg font-medium sm:px-6">
- Request invite to Dub Partners for receiving payouts
- </h3>
- <div className="scrollbar-hide mt-6 max-h-[calc(100dvh-200px)] overflow-auto overflow-y-scroll">
- <RequestPartnerInviteForm closeModal={() => setShowModal(false)} />
- </div>
- </Modal>
- );
-};
-
-const RequestPartnerInviteForm = ({
- closeModal,
-}: RequestPartnerInviteFormProps) => {
- const {
- register,
- handleSubmit,
- formState: { isValid, isSubmitting },
- } = useForm<RequestPartnerInvite>({
- resolver: zodResolver(requestPartnerInviteSchema),
- });
-
- const onSubmit = async (data: RequestPartnerInvite) => {
- const response = await fetch("/api/referrals/invite", {
- method: "POST",
- body: JSON.stringify(data),
- });
-
- if (response.ok) {
- toast.success("Invite request sent successfully!");
- closeModal();
- }
-
- const { error } = await response.json();
- toast.error(error.message);
- };
-
- return (
- <form onSubmit={handleSubmit(onSubmit)}>
- <div className="flex flex-col gap-y-6 px-4 text-left sm:px-6">
- <div className="flex flex-col gap-3">
- <div>
- <label
- htmlFor="accountNumber"
- className="flex items-center space-x-2"
- >
- <h2 className="text-sm font-medium text-gray-900">
- Account number
- </h2>
- </label>
- <div className="relative mt-2 rounded-md shadow-sm">
- <input
- {...register("email")}
- className="block w-full rounded-md border-gray-300 text-gray-900 placeholder-gray-400 focus:border-gray-500 focus:outline-none focus:ring-gray-500 sm:text-sm"
- placeholder="000000000"
- required
- autoFocus
- autoComplete="off"
- />
- </div>
- </div>
- </div>
- </div>
-
- <div className="mt-8 flex justify-end gap-2 border-t border-gray-200 px-4 py-4 sm:px-6">
- <Button
- type="button"
- variant="secondary"
- text="Cancel"
- className="h-9 w-fit"
- disabled={isSubmitting}
- onClick={closeModal}
- />
-
- <Button
- type="submit"
- text="Request invite"
- className="h-9 w-fit"
- disabled={!isValid}
- loading={isSubmitting}
- />
- </div>
- </form>
- );
-};
-
-export function useRequestPartnerInviteModal() {
- const [showRequestPartnerInviteModal, setShowRequestPartnerInviteModal] =
- useState(false);
-
- const RequestPartnerInviteModal = useCallback(() => {
- return (
- <RequestPartnerInvite
- showModal={showRequestPartnerInviteModal}
- setShowModal={setShowRequestPartnerInviteModal}
- />
- );
- }, [showRequestPartnerInviteModal, setShowRequestPartnerInviteModal]);
-
- return useMemo(
- () => ({
- setShowRequestPartnerInviteModal,
- RequestPartnerInviteModal,
- }),
- [setShowRequestPartnerInviteModal, RequestPartnerInviteModal],
- );
-}