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 country combobox in create customer modal

Steven Tey5f025ba9c58b
diff --git a/apps/web/app/(ee)/api/customers/route.ts b/apps/web/app/(ee)/api/customers/route.ts
index ecdeb52892..97d7987a74 100644
--- a/apps/web/app/(ee)/api/customers/route.ts
+++ b/apps/web/app/(ee)/api/customers/route.ts
@@ -139,7 +139,7 @@ export const GET = withWorkspace(
// POST /api/customers – Create a customer
export const POST = withWorkspace(
async ({ req, workspace }) => {
- const { email, name, avatar, externalId, stripeCustomerId } =
+ const { email, name, avatar, externalId, stripeCustomerId, country } =
createCustomerBodySchema.parse(await parseRequestBody(req));
const customerId = createId({ prefix: "cus_" });
@@ -158,6 +158,7 @@ export const POST = withWorkspace(
avatar: finalCustomerAvatar,
externalId,
stripeCustomerId,
+ country,
projectId: workspace.id,
projectConnectId: workspace.stripeConnectId,
},
diff --git a/apps/web/lib/actions/partners/create-manual-commission.ts b/apps/web/lib/actions/partners/create-manual-commission.ts
index 600e613d1c..01c5afdda5 100644
--- a/apps/web/lib/actions/partners/create-manual-commission.ts
+++ b/apps/web/lib/actions/partners/create-manual-commission.ts
@@ -325,7 +325,10 @@ export const createManualCommissionAction = authActionClient
linkId: link.id,
clickId: clickEventData.click_id,
clickedAt: new Date(clickEventData.timestamp),
- country: clickEventData.country,
+ country:
+ clickEventData.country === "Unknown"
+ ? null
+ : clickEventData.country,
...(recordSaleEvents && {
sales: totalSales,
saleAmount: totalSaleAmount,
@@ -356,8 +359,8 @@ export const createManualCommissionAction = authActionClient
url: link.url,
ip: "127.0.0.1",
continent: customer.country
- ? COUNTRIES_TO_CONTINENTS[customer.country.toUpperCase()] || "NA"
- : "NA",
+ ? COUNTRIES_TO_CONTINENTS[customer.country.toUpperCase()] || ""
+ : "",
});
tbEventsToRecord.push(recordClickZod(generatedClickEvent));
@@ -444,7 +447,6 @@ export const createManualCommissionAction = authActionClient
linkId: link.id,
clickId: clickId,
clickedAt: new Date(clickTimestamp),
- country: generatedClickEvent.country,
...(saleAmount && {
sales: {
increment: 1,
diff --git a/apps/web/lib/zod/schemas/customers.ts b/apps/web/lib/zod/schemas/customers.ts
index c0203a0358..a32190af12 100644
--- a/apps/web/lib/zod/schemas/customers.ts
+++ b/apps/web/lib/zod/schemas/customers.ts
@@ -79,30 +79,35 @@ export const getCustomersCountQuerySchema = getCustomersQuerySchema
.extend({ groupBy: z.enum(["country", "linkId"]).optional() });
export const createCustomerBodySchema = z.object({
- email: z
- .string()
- .email()
- .nullish()
- .describe("Email of the customer in the client's app."),
+ email: z.string().email().nullish().describe("The customer's email address."),
name: z
.string()
.nullish()
.describe(
- "Name of the customer in the client's app. If not provided, a random name will be generated.",
+ "The customer's name. If not provided, the email address will be used, and if email is not provided, a random name will be generated.",
),
avatar: z
.string()
.url()
.nullish()
- .describe("Avatar URL of the customer in the client's app."),
+ .describe(
+ "The customer's avatar URL. If not provided, a random avatar will be generated.",
+ ),
externalId: z
.string()
- .describe("Unique identifier for the customer in the client's app."),
+ .describe(
+ "The customer's unique identifier your database. This is useful for associating subsequent conversion events from Dub's API to your internal systems.",
+ ),
stripeCustomerId: z
.string()
.nullish()
.describe(
- "The customer's Stripe customer ID. Useful for attribution recurring sale events to the partner who referred the customer.",
+ "The customer's Stripe customer ID. This is useful for attributing recurring sale events to the partner who referred the customer.",
+ ),
+ country: z
+ .string()
+ .describe(
+ "The customer's country in ISO 3166-1 alpha-2 format. Updating this field will only affect the customer's country in Dub's system (and has no effect on existing conversion events).",
),
});
diff --git a/apps/web/ui/modals/add-customer-modal.tsx b/apps/web/ui/modals/add-customer-modal.tsx
index 91e0661880..c1cc59c834 100644
--- a/apps/web/ui/modals/add-customer-modal.tsx
+++ b/apps/web/ui/modals/add-customer-modal.tsx
@@ -1,6 +1,7 @@
import useWorkspace from "@/lib/swr/use-workspace";
import { CustomerProps } from "@/lib/types";
import { createCustomerBodySchema } from "@/lib/zod/schemas/customers";
+import { CountryCombobox } from "@/ui/partners/country-combobox";
import { Button, Modal, useMediaQuery } from "@dub/ui";
import { useCallback, useEffect, useMemo, useRef, useState } from "react";
import { useForm } from "react-hook-form";
@@ -31,13 +32,15 @@ export const AddCustomerModal = ({
handleSubmit,
watch,
reset,
- formState: { isSubmitting },
+ setValue,
+ formState: { isSubmitting, errors },
} = useForm<FormData>({
defaultValues: {
name: null,
email: null,
externalId: "",
stripeCustomerId: null,
+ country: "US",
},
});
@@ -51,6 +54,7 @@ export const AddCustomerModal = ({
email: null,
externalId: "",
stripeCustomerId: null,
+ country: "US",
});
}
prevShowModal.current = showModal;
@@ -87,6 +91,7 @@ export const AddCustomerModal = ({
};
const externalId = watch("externalId");
+ const country = watch("country");
return (
<Modal showModal={showModal} setShowModal={setShowModal}>
@@ -168,6 +173,24 @@ export const AddCustomerModal = ({
The customer's Stripe customer ID (optional)
</p>
</div>
+
+ <div>
+ <label className="text-sm font-normal text-neutral-500">
+ Country <span className="text-red-500">*</span>
+ </label>
+ <CountryCombobox
+ value={country || "US"}
+ onChange={(value) =>
+ setValue("country", value, { shouldValidate: true })
+ }
+ error={!!errors.country}
+ className="mt-2"
+ />
+ <input
+ type="hidden"
+ {...register("country", { required: true })}
+ />
+ </div>
</div>
<div className="flex items-center justify-end border-t border-neutral-200 px-4 py-4 sm:px-6">
@@ -185,7 +208,7 @@ export const AddCustomerModal = ({
text="Create customer"
className="h-9 w-fit"
loading={isSubmitting}
- disabled={!externalId}
+ disabled={!externalId || !country}
/>
</div>
</div>