The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.
TypeScript clickhousemysqlnextjsopen-sourceplanetscaleprismatailwindcsstinybirdtypescriptupstashvercel
Add profile settings
Tim Wilsone1d732a33583
diff --git a/apps/web/app/partners.dub.co/(dashboard)/[partnerId]/settings/page-client.tsx b/apps/web/app/partners.dub.co/(dashboard)/[partnerId]/settings/page-client.tsxnew file mode 100644index 0000000000..cbf2199f99--- /dev/null+++ b/apps/web/app/partners.dub.co/(dashboard)/[partnerId]/settings/page-client.tsx@@ -0,0 +1,188 @@+"use client";++import { updatePartnerProfile } from "@/lib/actions/partners/update-partner-profile";+import usePartnerProfile from "@/lib/swr/use-partner-profile";+import { PartnerProps } from "@/lib/types";+import {+ Button,+ buttonVariants,+ FileUpload,+ LoadingSpinner,+ MaxWidthWrapper,+} from "@dub/ui";+import { cn, DICEBEAR_AVATAR_URL } from "@dub/utils";+import { useParams } from "next/navigation";+import { PropsWithChildren } from "react";+import { Controller, useForm } from "react-hook-form";+import ReactTextareaAutosize from "react-textarea-autosize";+import { toast } from "sonner";++export function ProfileSettingsPageClient() {+ const { partner, error } = usePartnerProfile();++ return (+ <MaxWidthWrapper>+ <div className="max-w-screen-md rounded-lg border border-neutral-200 bg-white">+ <div className="border-b border-neutral-200 p-6">+ <h2 className="text-xl font-medium text-neutral-800">About you</h2>+ </div>+ {partner ? (+ <ProfileForm partner={partner} />+ ) : (+ <div className="flex h-32 w-full items-center justify-center">+ {error ? (+ <span className="text-sm text-neutral-500">+ Failed to load profile data+ </span>+ ) : (+ <LoadingSpinner />+ )}+ </div>+ )}+ </div>+ </MaxWidthWrapper>+ );+}++function ProfileForm({ partner }: { partner: PartnerProps }) {+ const { partnerId } = useParams() as { partnerId: string };+ const {+ register,+ control,+ handleSubmit,+ setError,+ formState: { errors, isSubmitting, isSubmitSuccessful },+ } = useForm<{+ name: string;+ logo: string | null;+ description: string | null;+ }>({+ defaultValues: {+ name: partner.name,+ logo: partner.logo,+ description: partner.bio,+ },+ });++ return (+ <form+ onSubmit={handleSubmit(async (data) => {+ try {+ const result = await updatePartnerProfile({ ...data, partnerId });+ if (!result?.data?.ok) throw new Error("Failed to save profile");+ toast.success("Profile updated successfully");+ } catch (error) {+ console.error(error);+ setError("root.serverError", {+ message: "Failed to save profile. Please try again.",+ });+ toast.error("Failed to save profile. Please try again.");+ }+ })}+ >+ <div className="px-5">+ <div className="grid grid-cols-2 items-center">+ <FormRow>+ <label className="contents">+ <span className="text-sm font-medium text-gray-800">Logo</span>+ <div className="flex items-center gap-5">+ <Controller+ control={control}+ name="logo"+ render={({ field }) => (+ <FileUpload+ accept="images"+ className="mt-2 size-14 rounded-full border border-gray-300"+ iconClassName="w-5 h-5"+ previewClassName="size-14 rounded-full"+ variant="plain"+ imageSrc={+ field.value || `${DICEBEAR_AVATAR_URL}${partner?.name}`+ }+ readFile+ onChange={({ src }) => field.onChange(src)}+ content={null}+ maxFileSizeMB={2}+ />+ )}+ />+ <div>+ <div+ className={cn(+ buttonVariants({ variant: "secondary" }),+ "flex h-7 w-fit cursor-pointer items-center rounded-md border px-2 text-xs",+ )}+ >+ Upload image+ </div>+ <p className="mt-1.5 text-xs text-gray-500">+ Recommended size: 160x160px+ </p>+ </div>+ </div>+ </label>+ </FormRow>++ <FormRow>+ <label className="contents">+ <span className="text-sm font-medium text-gray-800">Name</span>+ <div>+ <input+ type="text"+ className={cn(+ "mt-2 block w-full rounded-md focus:outline-none sm:text-sm",+ errors.name+ ? "border-red-300 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."+ {...register("name", {+ required: true,+ })}+ />+ </div>+ </label>+ </FormRow>++ <FormRow>+ <label className="contents">+ <span className="text-sm font-medium text-gray-800">+ Description+ </span>+ <div>+ <ReactTextareaAutosize+ className={cn(+ "mt-2 block w-full rounded-md focus:outline-none sm:text-sm",+ errors.name+ ? "border-red-300 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="Tell us about your business"+ minRows={3}+ maxRows={10}+ {...register("description")}+ />+ </div>+ </label>+ </FormRow>+ </div>+ </div>+ <div className="flex justify-end rounded-b-lg border-t border-neutral-200 bg-neutral-100 px-5 py-3.5">+ <Button+ type="submit"+ text="Save changes"+ className="h-8 w-fit px-2.5"+ loading={isSubmitting}+ />+ </div>+ </form>+ );+}++function FormRow({ children }: PropsWithChildren) {+ return (+ <div className="blah contents [&:not(:last-child)>label>*]:border-b [&>label>*]:flex [&>label>*]:h-full [&>label>*]:items-center [&>label>*]:border-neutral-200 [&>label>*]:py-5">+ {children}+ </div>+ );+}diff --git a/apps/web/app/partners.dub.co/(dashboard)/[partnerId]/settings/page.tsx b/apps/web/app/partners.dub.co/(dashboard)/[partnerId]/settings/page.tsxindex f378c9382b..a0469aec19 100644--- a/apps/web/app/partners.dub.co/(dashboard)/[partnerId]/settings/page.tsx+++ b/apps/web/app/partners.dub.co/(dashboard)/[partnerId]/settings/page.tsx@@ -1,5 +1,13 @@import { PageContent } from "@/ui/layout/page-content";+import { ProfileSettingsPageClient } from "./page-client";export default function ProfileSettingsPage() {- return <PageContent title="Profile"></PageContent>;+ return (+ <PageContent+ title="Profile"+ description="This partner profile is shared with program owners when you apply to their programs."+ >+ <ProfileSettingsPageClient />+ </PageContent>+ );}diff --git a/apps/web/lib/actions/partners/onboard-partner.ts b/apps/web/lib/actions/partners/onboard-partner.tsindex 2fad6aaaad..c74b671b6b 100644--- a/apps/web/lib/actions/partners/onboard-partner.ts+++ b/apps/web/lib/actions/partners/onboard-partner.ts@@ -15,7 +15,7 @@ const onboardPartnerSchema = z.object({description: z.string().nullable(),});-// Update the notification preference for a user in a workspace+// Onboard a new partnerexport const onboardPartner = authUserActionClient.schema(onboardPartnerSchema).action(async ({ ctx, parsedInput }) => {diff --git a/apps/web/lib/actions/partners/update-partner-profile.ts b/apps/web/lib/actions/partners/update-partner-profile.tsnew file mode 100644index 0000000000..852493bfae--- /dev/null+++ b/apps/web/lib/actions/partners/update-partner-profile.ts@@ -0,0 +1,68 @@+"use server";++import { getPartnerOrThrow } from "@/lib/api/partners/get-partner-or-throw";+import { userIsInBeta } from "@/lib/edge-config";+import { prisma } from "@/lib/prisma";+import { storage } from "@/lib/storage";+import { nanoid } from "nanoid";+import z from "../../zod";+import { authUserActionClient } from "../safe-action";++const onboardPartnerSchema = z.object({+ partnerId: z.string(),+ name: z.string(),+ logo: z.string().nullable(),+ description: z.string().nullable(),+});++// Update a partner profile+export const updatePartnerProfile = authUserActionClient+ .schema(onboardPartnerSchema)+ .action(async ({ ctx, parsedInput }) => {+ const { user } = ctx;++ const { partner } = await getPartnerOrThrow({+ partnerId: parsedInput.partnerId,+ userId: user.id,+ });++ const partnersPortalEnabled = await userIsInBeta(+ user.email,+ "partnersPortal",+ );+ if (!partnersPortalEnabled) {+ return {+ ok: false,+ error: "Partners portal feature flag disabled.",+ };+ }++ const { name, logo, description } = parsedInput;++ try {+ let logoUrl: string | null = null;+ if (logo)+ logoUrl = (+ await storage.upload(+ `logos/partners/${partner.id}_${nanoid(7)}`,+ logo,+ )+ ).url;++ await prisma.partner.update({+ where: { id: partner.id },+ data: {+ name,+ bio: description,+ logo: logoUrl,+ },+ });++ return { ok: true };+ } catch (e) {+ console.error(e);+ return {+ ok: false,+ };+ }+ });diff --git a/apps/web/lib/api/partners/get-partner-or-throw.ts b/apps/web/lib/api/partners/get-partner-or-throw.tsnew file mode 100644index 0000000000..b978556320--- /dev/null+++ b/apps/web/lib/api/partners/get-partner-or-throw.ts@@ -0,0 +1,26 @@+import { prisma } from "@/lib/prisma";+import { DubApiError } from "../errors";++export async function getPartnerOrThrow({+ partnerId,+ userId,+}: {+ partnerId: string;+ userId: string;+}) {+ const partner = await prisma.partner.findUnique({+ where: {+ id: partnerId,+ users: { some: { userId } },+ },+ });++ if (!partner) {+ throw new DubApiError({+ code: "not_found",+ message: "Partner not found.",+ });+ }++ return { partner };+}diff --git a/apps/web/ui/layout/page-content/index.tsx b/apps/web/ui/layout/page-content/index.tsxindex 02c3615852..ba16e07af8 100644--- a/apps/web/ui/layout/page-content/index.tsx+++ b/apps/web/ui/layout/page-content/index.tsx@@ -7,20 +7,36 @@ import { NavButton } from "./nav-button";export function PageContent({title,+ description,children,-}: PropsWithChildren<{ title?: ReactNode }>) {+}: PropsWithChildren<{ title?: ReactNode; description?: ReactNode }>) {const hasTitle = title !== undefined;+ const hasDescription = description !== undefined;return (<div className="bg-neutral-100 md:bg-white">- <MaxWidthWrapper className={cn("mt-3", hasTitle && "md:mt-6 md:py-3")}>+ <MaxWidthWrapper+ className={cn(+ "mt-3",+ (hasTitle || hasDescription) && "md:mt-6 md:py-3",+ )}+ ><div className="flex items-center justify-between gap-4"><div className="flex items-center gap-4"><NavButton />- {hasTitle && (- <h1 className="text-xl font-semibold leading-7 text-neutral-900 md:text-2xl">- {title}- </h1>+ {(hasTitle || hasDescription) && (+ <div>+ {hasTitle && (+ <h1 className="text-xl font-semibold leading-7 text-neutral-900 md:text-2xl">+ {title}+ </h1>+ )}+ {hasDescription && (+ <p className="mt-1 hidden text-base text-neutral-500 md:block">+ {description}+ </p>+ )}+ </div>)}</div><div className="flex items-center gap-4 md:hidden">@@ -30,6 +46,13 @@ export function PageContent({</div></MaxWidthWrapper><div className="bg-white pt-2.5 max-md:mt-3 max-md:rounded-t-[16px]">+ {hasDescription && (+ <MaxWidthWrapper className="">+ <p className="mb-3 mt-1 text-base text-neutral-500 md:hidden">+ {description}+ </p>+ </MaxWidthWrapper>+ )}{children}</div></div>