The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.
TypeScript clickhousemysqlnextjsopen-sourceplanetscaleprismatailwindcsstinybirdtypescriptupstashvercel
update the profile type
Kiran K7fa9db60cd4a
diff --git a/apps/web/app/partners.dub.co/(dashboard)/settings/page-client.tsx b/apps/web/app/partners.dub.co/(dashboard)/settings/page-client.tsxindex 4fb7269b2e..ea6d92cdf5 100644--- a/apps/web/app/partners.dub.co/(dashboard)/settings/page-client.tsx+++ b/apps/web/app/partners.dub.co/(dashboard)/settings/page-client.tsx@@ -12,9 +12,12 @@ import {FileUpload,LoadingSpinner,MaxWidthWrapper,+ ToggleGroup,useEnterSubmit,} from "@dub/ui";import { cn, DICEBEAR_AVATAR_URL } from "@dub/utils";+import { PartnerProfileType } from "@prisma/client";+import { AnimatePresence, LayoutGroup, motion } from "framer-motion";import { useAction } from "next-safe-action/hooks";import { PropsWithChildren, useRef } from "react";import { Controller, useForm } from "react-hook-form";@@ -90,28 +93,35 @@ function ProfileForm({ partner }: { partner: PartnerProps }) {control,handleSubmit,setError,+ watch,+ setValue,formState: { errors, isSubmitting },} = useForm<{name: string;image: string | null;description: string | null;country: string;+ profileType: PartnerProfileType;+ companyName: string | null;}>({defaultValues: {name: partner.name,image: partner.image,description: partner.description ?? null,country: partner.country ?? "",+ profileType: partner.profileType ?? "individual",+ companyName: partner.companyName ?? null,},});+ const { profileType } = watch();+const { payoutsCount } = usePartnerPayoutsCount<PayoutsCount[]>({groupBy: "status",});- const processingPayoutsCount = payoutsCount?.find(- (payout) => payout.status === "processing",- )?.count;+ const processingPayoutsCount =+ payoutsCount?.find((payout) => payout.status === "processing")?.count ?? 0;const formRef = useRef<HTMLFormElement>(null);const { handleKeyDown } = useEnterSubmit(formRef);@@ -220,11 +230,7 @@ function ProfileForm({ partner }: { partner: PartnerProps }) {value={field.value || ""}onChange={field.onChange}error={errors.country ? true : false}- disabled={- processingPayoutsCount && processingPayoutsCount > 0- ? true- : false- }+ disabled={processingPayoutsCount > 0}/>)}/>@@ -255,23 +261,88 @@ function ProfileForm({ partner }: { partner: PartnerProps }) {</label></FormRow>- {partner.profileType === "company" && (- <FormRow>- <label className="contents">- <span className="text-sm font-medium text-neutral-800">- Legal company name- </span>- <div>- <input- type="text"- className="mt-2 block w-full rounded-md border-neutral-300 text-neutral-900 read-only:bg-neutral-100 read-only:text-neutral-500 sm:text-sm"- readOnly- defaultValue={partner.companyName ?? ""}- />- </div>- </label>- </FormRow>- )}+ <FormRow>+ <label className="contents">+ <span className="text-sm font-medium text-neutral-800">+ Profile Type+ </span>+ <div className="w-full">+ <LayoutGroup>+ <div className="w-full">+ <ToggleGroup+ options={[+ {+ value: "individual",+ label: "Individual",+ },+ {+ value: "company",+ label: "Company",+ },+ ]}+ selected={profileType}+ selectAction={(option: "individual" | "company") => {+ if (processingPayoutsCount === 0) {+ setValue("profileType", option);+ }+ }}+ className={cn(+ "flex w-full items-center gap-0.5 rounded-lg border-neutral-300 bg-neutral-100 p-0.5",+ processingPayoutsCount > 0 && "cursor-not-allowed",+ )}+ optionClassName={cn(+ "h-9 flex items-center justify-center rounded-lg flex-1",+ processingPayoutsCount > 0 && "pointer-events-none",+ )}+ indicatorClassName="bg-white"+ />+ </div>+ </LayoutGroup>+ </div>+ </label>+ </FormRow>++ <AnimatePresence mode="popLayout">+ {profileType === "company" && (+ <motion.div+ layout+ initial={{ opacity: 0, height: 0 }}+ animate={{ opacity: 1, height: "auto" }}+ exit={{ opacity: 0, height: 0 }}+ transition={{+ type: "spring",+ stiffness: 300,+ damping: 30,+ opacity: { duration: 0.2 },+ layout: { duration: 0.3, type: "spring" },+ }}+ className="contents"+ >+ <FormRow>+ <label className="contents">+ <span className="text-sm font-medium text-neutral-800">+ Legal company name+ </span>+ <div>+ <input+ type="text"+ className={cn(+ "mt-2 block w-full rounded-md focus:outline-none sm:text-sm",+ errors.companyName+ ? "border-red-300 pr-10 text-red-900 placeholder-red-300 focus:border-red-500 focus:ring-red-500"+ : "border-neutral-300 text-neutral-900 placeholder-neutral-400 focus:border-neutral-500 focus:ring-neutral-500",+ )}+ disabled={processingPayoutsCount > 0}+ {...register("companyName", {+ required: profileType === "company",+ })}+ />+ </div>+ </label>+ </FormRow>+ </motion.div>+ )}+ </AnimatePresence></div></div><div className="flex justify-end rounded-b-lg border-t border-neutral-200 bg-neutral-100 px-5 py-3.5">diff --git a/apps/web/lib/actions/partners/update-partner-profile.ts b/apps/web/lib/actions/partners/update-partner-profile.tsindex 253767769d..a74570a74c 100644--- a/apps/web/lib/actions/partners/update-partner-profile.ts+++ b/apps/web/lib/actions/partners/update-partner-profile.ts@@ -3,29 +3,60 @@import { storage } from "@/lib/storage";import { prisma } from "@dub/prisma";import { COUNTRIES, nanoid } from "@dub/utils";+import { PartnerProfileType } from "@prisma/client";import { stripe } from "../../stripe";import z from "../../zod";import { authPartnerActionClient } from "../safe-action";-const updatePartnerProfileSchema = z.object({- name: z.string(),- image: z.string().nullable(),- description: z.string().nullable(),- country: z.enum(Object.keys(COUNTRIES) as [string, ...string[]]).nullable(),-});+const updatePartnerProfileSchema = z+ .object({+ name: z.string(),+ image: z.string().nullable(),+ description: z.string().nullable(),+ country: z.enum(Object.keys(COUNTRIES) as [string, ...string[]]).nullable(),+ profileType: z.nativeEnum(PartnerProfileType),+ companyName: z.string().nullable(),+ })+ .refine(+ (data) => {+ if (data.profileType === "company") {+ return !!data.companyName;+ }++ return true;+ },+ {+ message: "Legal company name is required.",+ path: ["companyName"],+ },+ )+ .transform((data) => ({+ ...data,+ companyName: data.profileType === "individual" ? null : data.companyName,+ }));// Update a partner profileexport const updatePartnerProfileAction = authPartnerActionClient.schema(updatePartnerProfileSchema).action(async ({ ctx, parsedInput }) => {const { partner } = ctx;- const { name, image, description, country } = parsedInput;+ const { name, image, description, country, profileType, companyName } =+ parsedInput;++ const countryChanged =+ partner.country?.toLowerCase() !== country?.toLowerCase();++ const profileTypeChanged =+ partner.profileType.toLowerCase() !== profileType.toLowerCase();++ const companyNameChanged =+ partner.companyName?.toLowerCase() !== companyName?.toLowerCase();++ console.log({ countryChanged, profileTypeChanged, companyNameChanged });- if (- partner.country &&- partner.country.toLowerCase() !== country?.toLowerCase()- ) {- // Partner should be able to update their country if they don't have any payouts with status `processing`+ if (countryChanged || profileTypeChanged || companyNameChanged) {+ // Partner should be able to update their country, profile type, or company name+ // if they don't have any payouts with status `processing`const pendingPayoutsCount = await prisma.payout.count({where: {partnerId: partner.id,@@ -74,6 +105,8 @@ export const updatePartnerProfileAction = authPartnerActionClientdescription,image: imageUrl,country,+ profileType,+ companyName,},});});