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.

display the dicounts on the parter profile page

Kiran Ka4d12c5380a3
diff --git a/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/partners/[partnerId]/links/page-client.tsx b/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/partners/[partnerId]/links/page-client.tsx
index ca1f0e9e7c..1b6e0f6714 100644
--- a/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/partners/[partnerId]/links/page-client.tsx
+++ b/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/partners/[partnerId]/links/page-client.tsx
@@ -1,5 +1,6 @@
"use client";
+import useDiscountCodes from "@/lib/swr/use-discount-codes";
import usePartner from "@/lib/swr/use-partner";
import useWorkspace from "@/lib/swr/use-workspace";
import { EnrolledPartnerProps } from "@/lib/types";
@@ -15,7 +16,10 @@ export function ProgramPartnerLinksPageClient() {
const { partner, error } = usePartner({ partnerId });
return partner ? (
- <PartnerLinks partner={partner} />
+ <div className="space-y-8">
+ <PartnerLinks partner={partner} />
+ <PartnerDiscountCodes partner={partner} />
+ </div>
) : (
<div className="flex justify-center py-16">
{error ? (
@@ -37,11 +41,6 @@ const PartnerLinks = ({ partner }: { partner: EnrolledPartnerProps }) => {
partner,
});
- const { AddDiscountCodeModal, setShowAddDiscountCodeModal } =
- useAddDiscountCodeModal({
- partner,
- });
-
const table = useTable({
data: partner.links || [],
columns: [
@@ -134,24 +133,140 @@ const PartnerLinks = ({ partner }: { partner: EnrolledPartnerProps }) => {
return (
<>
- <AddPartnerLinkModal />
- <AddDiscountCodeModal />
<div className="flex items-end justify-between gap-4">
<h2 className="text-content-emphasis text-lg font-semibold">Links</h2>
- <div className="flex gap-2">
+ <Button
+ variant="secondary"
+ text="Create link"
+ className="h-8 w-fit rounded-lg px-3 py-2 font-medium"
+ onClick={() => setShowAddPartnerLinkModal(true)}
+ />
+ </div>
+ <div className="mt-4">
+ <Table {...table} />
+ </div>
+ <AddPartnerLinkModal />
+ </>
+ );
+};
+
+const PartnerDiscountCodes = ({
+ partner,
+}: {
+ partner: EnrolledPartnerProps;
+}) => {
+ const { slug } = useWorkspace();
+ const { discountCodes, loading, error } = useDiscountCodes({
+ partnerId: partner.id || null,
+ });
+
+ const { AddDiscountCodeModal, setShowAddDiscountCodeModal } =
+ useAddDiscountCodeModal({
+ partner,
+ });
+
+ const table = useTable({
+ data: discountCodes || [],
+ columns: [
+ {
+ id: "shortLink",
+ header: "Link",
+ cell: ({ row }) => {
+ const link = partner.links?.find((l) => l.id === row.original.linkId);
+ return link ? (
+ <div className="flex items-center gap-3">
+ <Link
+ href={`/${slug}/links/${link.domain}/${link.key}`}
+ target="_blank"
+ className="cursor-alias font-medium text-black decoration-dotted hover:underline"
+ >
+ {getPrettyUrl(link.shortLink)}
+ </Link>
+ <CopyButton value={link.shortLink} className="p-0.5" />
+ </div>
+ ) : (
+ <span className="text-neutral-500">Link not found</span>
+ );
+ },
+ },
+ {
+ id: "code",
+ header: "Discount code",
+ cell: ({ row }) => (
+ <div className="flex items-center gap-3">
+ <span className="font-mono text-sm">{row.original.code}</span>
+ <CopyButton value={row.original.code} className="p-0.5" />
+ </div>
+ ),
+ },
+ ],
+ resourceName: (p) => `discount code${p ? "s" : ""}`,
+ thClassName: (id) =>
+ cn(id === "total" && "[&>div]:justify-end", "border-l-0"),
+ tdClassName: (id) => cn(id === "total" && "text-right", "border-l-0"),
+ className: "[&_tr:last-child>td]:border-b-transparent",
+ scrollWrapperClassName: "min-h-[40px]",
+ } as any);
+
+ if (loading) {
+ return (
+ <>
+ <AddDiscountCodeModal />
+ <div className="flex items-end justify-between gap-4">
+ <h2 className="text-content-emphasis text-lg font-semibold">
+ Discount codes
+ </h2>
<Button
variant="secondary"
- text="Create discount code"
+ text="Create code"
className="h-8 w-fit rounded-lg px-3 py-2 font-medium"
onClick={() => setShowAddDiscountCodeModal(true)}
/>
+ </div>
+ <div className="mt-4 flex justify-center py-16">
+ <LoadingSpinner />
+ </div>
+ </>
+ );
+ }
+
+ if (error) {
+ return (
+ <>
+ <AddDiscountCodeModal />
+ <div className="flex items-end justify-between gap-4">
+ <h2 className="text-content-emphasis text-lg font-semibold">
+ Discount codes
+ </h2>
<Button
variant="secondary"
- text="Create link"
+ text="Create code"
className="h-8 w-fit rounded-lg px-3 py-2 font-medium"
- onClick={() => setShowAddPartnerLinkModal(true)}
+ onClick={() => setShowAddDiscountCodeModal(true)}
/>
</div>
+ <div className="mt-4 flex justify-center py-16">
+ <span className="text-content-subtle text-sm">
+ Failed to load discount codes
+ </span>
+ </div>
+ </>
+ );
+ }
+
+ return (
+ <>
+ <AddDiscountCodeModal />
+ <div className="flex items-end justify-between gap-4">
+ <h2 className="text-content-emphasis text-lg font-semibold">
+ Discount codes
+ </h2>
+ <Button
+ variant="secondary"
+ text="Create code"
+ className="h-8 w-fit rounded-lg px-3 py-2 font-medium"
+ onClick={() => setShowAddDiscountCodeModal(true)}
+ />
</div>
<div className="mt-4">
<Table {...table} />
diff --git a/apps/web/lib/swr/use-discount-codes.ts b/apps/web/lib/swr/use-discount-codes.ts
new file mode 100644
index 0000000000..06d14dd80c
--- /dev/null
+++ b/apps/web/lib/swr/use-discount-codes.ts
@@ -0,0 +1,30 @@
+import { fetcher } from "@dub/utils";
+import useSWR from "swr";
+import { DiscountCodeProps } from "../types";
+import useWorkspace from "./use-workspace";
+
+export default function useDiscountCodes({
+ partnerId,
+ enabled = true,
+}: {
+ partnerId: string | null;
+ enabled?: boolean;
+}) {
+ const { id: workspaceId } = useWorkspace();
+
+ const { data: discountCodes, error } = useSWR<DiscountCodeProps[]>(
+ enabled && workspaceId && partnerId
+ ? `/api/discount-codes?partnerId=${partnerId}&workspaceId=${workspaceId}`
+ : null,
+ fetcher,
+ {
+ dedupingInterval: 60000,
+ },
+ );
+
+ return {
+ discountCodes,
+ loading: !discountCodes && !error,
+ error,
+ };
+}
diff --git a/apps/web/lib/types.ts b/apps/web/lib/types.ts
index 0d2a6bc518..ac2fdd099f 100644
--- a/apps/web/lib/types.ts
+++ b/apps/web/lib/types.ts
@@ -44,7 +44,7 @@ import {
CustomerSchema,
} from "./zod/schemas/customers";
import { dashboardSchema } from "./zod/schemas/dashboard";
-import { DiscountSchema } from "./zod/schemas/discount";
+import { DiscountSchema, DiscountCodeSchema } from "./zod/schemas/discount";
import { FolderSchema } from "./zod/schemas/folders";
import {
additionalPartnerLinkSchema,
@@ -440,6 +440,8 @@ export type EnrolledPartnerProps = z.infer<typeof EnrolledPartnerSchema>;
export type DiscountProps = z.infer<typeof DiscountSchema>;
+export type DiscountCodeProps = z.infer<typeof DiscountCodeSchema>;
+
export type ProgramProps = z.infer<typeof ProgramSchema>;
export type ProgramLanderData = z.infer<typeof programLanderSchema>;
diff --git a/apps/web/ui/partners/add-discount-code-modal.tsx b/apps/web/ui/partners/add-discount-code-modal.tsx
index 98eef01f8f..b75be7e5cf 100644
--- a/apps/web/ui/partners/add-discount-code-modal.tsx
+++ b/apps/web/ui/partners/add-discount-code-modal.tsx
@@ -3,16 +3,16 @@ import { useApiMutation } from "@/lib/swr/use-api-mutation";
import { EnrolledPartnerProps } from "@/lib/types";
import { createDiscountCodeSchema } from "@/lib/zod/schemas/discount";
import {
+ ArrowTurnLeft,
Button,
Combobox,
ComboboxOption,
- InfoTooltip,
Modal,
- SimpleTooltipContent,
useCopyToClipboard,
useMediaQuery,
} from "@dub/ui";
import { cn } from "@dub/utils";
+import { Tag } from "lucide-react";
import { useCallback, useMemo, useRef, useState } from "react";
import { useForm } from "react-hook-form";
import { toast } from "sonner";
@@ -114,27 +114,26 @@ const AddDiscountCodeModal = ({
htmlFor="code"
className="block text-sm font-medium text-neutral-700"
>
- Discount code
+ Discount code (optional)
</label>
- <InfoTooltip
- content={
- <SimpleTooltipContent
- title="Discount codes cannot be edited after creation"
- cta="Learn more."
- href="https://dub.co/help/article/discount-codes"
- />
- }
- />
</div>
- <input
- {...register("code")}
- type="text"
- id="code"
- autoFocus={!isMobile}
- className="block w-full rounded-md border-neutral-300 text-neutral-900 placeholder-neutral-400 focus:border-neutral-500 focus:outline-none focus:ring-neutral-500 sm:text-sm"
- placeholder="Enter discount code (optional)"
- />
+ <div className="relative">
+ <div className="absolute inset-y-0 left-0 flex items-center pl-3">
+ <Tag className="text-content-default h-4 w-4" />
+ </div>
+ <input
+ {...register("code")}
+ type="text"
+ id="code"
+ autoFocus={!isMobile}
+ className="block w-full rounded-md border-[1.5px] border-neutral-300 pl-10 text-neutral-900 placeholder-neutral-400 focus:border-neutral-500 focus:outline-none focus:ring-neutral-500 sm:text-sm"
+ placeholder="CODE"
+ />
+ </div>
+ <p className="text-xs text-neutral-500">
+ Discount codes cannot be edited after creation
+ </p>
</div>
<div className="flex flex-col gap-2">
@@ -145,15 +144,6 @@ const AddDiscountCodeModal = ({
>
Referral link
</label>
- <InfoTooltip
- content={
- <SimpleTooltipContent
- title="Select the partner link to associate with this discount code."
- cta="Learn more."
- href="https://dub.co/help/article/discount-codes"
- />
- }
- />
</div>
<Combobox
@@ -174,8 +164,8 @@ const AddDiscountCodeModal = ({
}}
options={linkOptions}
caret={true}
- placeholder="Select link..."
- searchPlaceholder="Search links..."
+ placeholder="Select referral link"
+ searchPlaceholder="Search"
buttonProps={{
className: cn(
"w-full h-10 justify-start px-3",
@@ -196,8 +186,15 @@ const AddDiscountCodeModal = ({
<div className="flex items-center justify-end border-t border-neutral-200 bg-neutral-50 p-4">
<Button
type="submit"
- text="Create discount code"
- className="h-8 w-fit px-3"
+ text={
+ <span className="flex items-center gap-2">
+ Create discount code
+ <div className="rounded border border-white/20 p-1">
+ <ArrowTurnLeft className="size-3.5" />
+ </div>
+ </span>
+ }
+ className="h-8 w-fit pl-2.5 pr-1.5"
loading={isSubmitting}
disabled={!linkId}
/>