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.

Allow partners to update country

Kiran K203120b1d7f9
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.tsx
index 30d0c0bcf0..b3c53b3ba1 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
@@ -3,6 +3,7 @@
import { updatePartnerProfileAction } from "@/lib/actions/partners/update-partner-profile";
import usePartnerProfile from "@/lib/swr/use-partner-profile";
import { PartnerProps } from "@/lib/types";
+import { CountryCombobox } from "@/ui/partners/country-combobox";
import { OnlinePresenceForm } from "@/ui/partners/online-presence-form";
import {
Button,
@@ -12,7 +13,7 @@ import {
MaxWidthWrapper,
useEnterSubmit,
} from "@dub/ui";
-import { cn, COUNTRIES, DICEBEAR_AVATAR_URL } from "@dub/utils";
+import { cn, DICEBEAR_AVATAR_URL } from "@dub/utils";
import { useAction } from "next-safe-action/hooks";
import { PropsWithChildren, useRef } from "react";
import { Controller, useForm } from "react-hook-form";
@@ -93,11 +94,13 @@ function ProfileForm({ partner }: { partner: PartnerProps }) {
name: string;
image: string | null;
description: string | null;
+ country: string;
}>({
defaultValues: {
name: partner.name,
image: partner.image,
description: partner.description ?? null,
+ country: partner.country ?? "",
},
});
@@ -199,13 +202,17 @@ function ProfileForm({ partner }: { partner: PartnerProps }) {
Country
</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 focus:border-neutral-500 focus:outline-none focus:ring-neutral-500 sm:text-sm"
- readOnly
- defaultValue={
- partner.country ? COUNTRIES[partner.country] : ""
- }
+ <Controller
+ control={control}
+ name="country"
+ rules={{ required: true }}
+ render={({ field }) => (
+ <CountryCombobox
+ value={field.value || ""}
+ onChange={field.onChange}
+ error={errors.country ? true : false}
+ />
+ )}
/>
</div>
</label>
diff --git a/apps/web/app/partners.dub.co/(onboarding)/onboarding/onboarding-form.tsx b/apps/web/app/partners.dub.co/(onboarding)/onboarding/onboarding-form.tsx
index 0447ff032a..79d2d030f2 100644
--- a/apps/web/app/partners.dub.co/(onboarding)/onboarding/onboarding-form.tsx
+++ b/apps/web/app/partners.dub.co/(onboarding)/onboarding/onboarding-form.tsx
@@ -2,23 +2,22 @@
import { onboardPartnerAction } from "@/lib/actions/partners/onboard-partner";
import { onboardPartnerSchema } from "@/lib/zod/schemas/partners";
+import { CountryCombobox } from "@/ui/partners/country-combobox";
import { Partner } from "@dub/prisma/client";
import {
Button,
buttonVariants,
- Combobox,
FileUpload,
ToggleGroup,
useEnterSubmit,
useMediaQuery,
} from "@dub/ui";
-import { COUNTRIES } from "@dub/utils";
import { cn } from "@dub/utils/src/functions";
import { AnimatePresence, LayoutGroup, motion } from "framer-motion";
import { useSession } from "next-auth/react";
import { useAction } from "next-safe-action/hooks";
import { useRouter } from "next/navigation";
-import { useEffect, useMemo, useRef } from "react";
+import { useEffect, useRef } from "react";
import { Controller, useForm } from "react-hook-form";
import ReactTextareaAutosize from "react-textarea-autosize";
import { toast } from "sonner";
@@ -281,66 +280,3 @@ export function OnboardingForm({
</form>
);
}
-
-function CountryCombobox({
- value,
- onChange,
- disabled,
- error,
-}: {
- value: string;
- onChange: (value: string) => void;
- disabled?: boolean;
- error?: boolean;
-}) {
- const options = useMemo(
- () =>
- Object.entries(COUNTRIES).map(([key, value]) => ({
- icon: (
- <img
- alt={value}
- src={`https://hatscripts.github.io/circle-flags/flags/${key.toLowerCase()}.svg`}
- className="mr-1.5 size-4"
- />
- ),
- value: key,
- label: value,
- })),
- [],
- );
-
- return (
- <Combobox
- selected={options.find((o) => o.value === value) ?? null}
- setSelected={(option) => {
- if (!option) return;
- onChange(option.value);
- }}
- options={options}
- icon={
- value ? (
- <img
- alt={COUNTRIES[value]}
- src={`https://hatscripts.github.io/circle-flags/flags/${value.toLowerCase()}.svg`}
- className="mr-0.5 size-4"
- />
- ) : undefined
- }
- caret={true}
- placeholder="Select country"
- searchPlaceholder="Search countries..."
- matchTriggerWidth
- buttonProps={{
- className: cn(
- "mt-2 w-full justify-start border-neutral-300 px-3",
- "data-[state=open]:ring-1 data-[state=open]:ring-neutral-500 data-[state=open]:border-neutral-500",
- "focus:ring-1 focus:ring-neutral-500 focus:border-neutral-500 transition-none",
- !value && "text-neutral-400",
- disabled && "cursor-not-allowed",
- error && "border-red-500 ring-red-500 ring-1",
- ),
- disabled,
- }}
- />
- );
-}
diff --git a/apps/web/lib/actions/partners/update-partner-profile.ts b/apps/web/lib/actions/partners/update-partner-profile.ts
index bf55e8ee08..d34b77e7d7 100644
--- a/apps/web/lib/actions/partners/update-partner-profile.ts
+++ b/apps/web/lib/actions/partners/update-partner-profile.ts
@@ -2,7 +2,8 @@
import { storage } from "@/lib/storage";
import { prisma } from "@dub/prisma";
-import { nanoid } from "@dub/utils";
+import { COUNTRIES, nanoid } from "@dub/utils";
+import { stripe } from "../../stripe";
import z from "../../zod";
import { authPartnerActionClient } from "../safe-action";
@@ -10,6 +11,7 @@ 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(),
});
// Update a partner profile
@@ -17,7 +19,42 @@ export const updatePartnerProfileAction = authPartnerActionClient
.schema(updatePartnerProfileSchema)
.action(async ({ ctx, parsedInput }) => {
const { partner } = ctx;
- const { name, image, description } = parsedInput;
+ const { name, image, description, country } = parsedInput;
+
+ 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`
+ const pendingPayoutsCount = await prisma.payout.count({
+ where: {
+ partnerId: partner.id,
+ status: "processing",
+ },
+ });
+
+ if (pendingPayoutsCount > 0) {
+ throw new Error(
+ "Unable to change country while you have pending payouts.",
+ );
+ }
+
+ if (partner.stripeConnectId) {
+ const response = await stripe.accounts.del(partner.stripeConnectId);
+
+ if (response.deleted) {
+ await prisma.partner.update({
+ where: {
+ id: partner.id,
+ },
+ data: {
+ stripeConnectId: null,
+ payoutsEnabledAt: null,
+ },
+ });
+ }
+ }
+ }
const imageUrl = image
? (
@@ -29,11 +66,14 @@ export const updatePartnerProfileAction = authPartnerActionClient
: null;
await prisma.partner.update({
- where: { id: partner.id },
+ where: {
+ id: partner.id,
+ },
data: {
name,
description,
image: imageUrl,
+ country,
},
});
});
diff --git a/apps/web/ui/partners/country-combobox.tsx b/apps/web/ui/partners/country-combobox.tsx
new file mode 100644
index 0000000000..8009f4c86f
--- /dev/null
+++ b/apps/web/ui/partners/country-combobox.tsx
@@ -0,0 +1,66 @@
+import { Combobox } from "@dub/ui";
+import { cn, COUNTRIES } from "@dub/utils";
+import { useMemo } from "react";
+
+export function CountryCombobox({
+ value,
+ onChange,
+ disabled,
+ error,
+}: {
+ value: string;
+ onChange: (value: string) => void;
+ disabled?: boolean;
+ error?: boolean;
+}) {
+ const options = useMemo(
+ () =>
+ Object.entries(COUNTRIES).map(([key, value]) => ({
+ icon: (
+ <img
+ alt={value}
+ src={`https://hatscripts.github.io/circle-flags/flags/${key.toLowerCase()}.svg`}
+ className="mr-1.5 size-4"
+ />
+ ),
+ value: key,
+ label: value,
+ })),
+ [],
+ );
+
+ return (
+ <Combobox
+ selected={options.find((o) => o.value === value) ?? null}
+ setSelected={(option) => {
+ if (!option) return;
+ onChange(option.value);
+ }}
+ options={options}
+ icon={
+ value ? (
+ <img
+ alt={COUNTRIES[value]}
+ src={`https://hatscripts.github.io/circle-flags/flags/${value.toLowerCase()}.svg`}
+ className="mr-0.5 size-4"
+ />
+ ) : undefined
+ }
+ caret={true}
+ placeholder="Select country"
+ searchPlaceholder="Search countries..."
+ matchTriggerWidth
+ buttonProps={{
+ className: cn(
+ "mt-2 w-full justify-start border-neutral-300 px-3",
+ "data-[state=open]:ring-1 data-[state=open]:ring-neutral-500 data-[state=open]:border-neutral-500",
+ "focus:ring-1 focus:ring-neutral-500 focus:border-neutral-500 transition-none",
+ !value && "text-neutral-400",
+ disabled && "cursor-not-allowed",
+ error && "border-red-500 ring-red-500 ring-1",
+ ),
+ disabled,
+ }}
+ />
+ );
+}