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.

refresh the links after creating/updating

Kiran Kb59ff62fe305
diff --git a/apps/web/app/api/embed/referrals/links/route.ts b/apps/web/app/api/embed/referrals/links/route.ts
index a163fc0036..34d1a24466 100644
--- a/apps/web/app/api/embed/referrals/links/route.ts
+++ b/apps/web/app/api/embed/referrals/links/route.ts
@@ -8,6 +8,13 @@ import { ReferralsEmbedLinkSchema } from "@/lib/zod/schemas/referrals-embed";
import { getApexDomain } from "@dub/utils";
import { NextResponse } from "next/server";
+// GET /api/embed/referrals/links – get links for a partner
+export const GET = withReferralsEmbedToken(async ({ links }) => {
+ const partnerLinks = ReferralsEmbedLinkSchema.array().parse(links);
+
+ return NextResponse.json(partnerLinks);
+});
+
// POST /api/embed/referrals/links – create links for a partner
export const POST = withReferralsEmbedToken(
async ({ req, programEnrollment, program, links }) => {
diff --git a/apps/web/app/app.dub.co/embed/referrals/add-edit-link.tsx b/apps/web/app/app.dub.co/embed/referrals/add-edit-link.tsx
index 9e57fae6e5..734e7682df 100644
--- a/apps/web/app/app.dub.co/embed/referrals/add-edit-link.tsx
+++ b/apps/web/app/app.dub.co/embed/referrals/add-edit-link.tsx
@@ -82,7 +82,7 @@ export function ReferralsEmbedCreateUpdateLink({
return;
}
- await mutateSuffix("/links");
+ await mutateSuffix("api/embed/referrals/links");
onCancel();
} finally {
setIsSubmitting(false);
diff --git a/apps/web/app/app.dub.co/embed/referrals/links-list.tsx b/apps/web/app/app.dub.co/embed/referrals/links-list.tsx
index 7deb380523..4045e4938a 100644
--- a/apps/web/app/app.dub.co/embed/referrals/links-list.tsx
+++ b/apps/web/app/app.dub.co/embed/referrals/links-list.tsx
@@ -2,8 +2,10 @@ import { PARTNER_LINKS_LIMIT } from "@/lib/embed/constants";
import { AnimatedEmptyState } from "@/ui/shared/animated-empty-state";
import { Button, CopyButton, Table, Users, useTable } from "@dub/ui";
import { Pen2, Plus2 } from "@dub/ui/icons";
-import { getPrettyUrl, TAB_ITEM_ANIMATION_SETTINGS } from "@dub/utils";
+import { fetcher, getPrettyUrl, TAB_ITEM_ANIMATION_SETTINGS } from "@dub/utils";
import { motion } from "framer-motion";
+import { useEffect, useState } from "react";
+import useSWR from "swr";
import { ReferralsEmbedLink } from "./types";
interface Props {
@@ -17,10 +19,23 @@ export function ReferralsEmbedLinksList({
onCreateLink,
onEditLink,
}: Props) {
- const linksLimitReached = links.length >= PARTNER_LINKS_LIMIT;
+ const [partnerLinks, setPartnerLinks] = useState<ReferralsEmbedLink[]>(links);
+
+ const { data: refreshedLinks } = useSWR<ReferralsEmbedLink[]>(
+ "/api/embed/referrals/links",
+ fetcher,
+ );
+
+ useEffect(() => {
+ if (refreshedLinks) {
+ setPartnerLinks(refreshedLinks);
+ }
+ }, [refreshedLinks]);
+
+ const linksLimitReached = partnerLinks.length >= PARTNER_LINKS_LIMIT;
const { table, ...tableProps } = useTable({
- data: links,
+ data: partnerLinks,
columns: [
{
id: "link",
@@ -67,7 +82,11 @@ export function ReferralsEmbedLinksList({
icon={<Plus2 className="text-white" />}
onClick={onCreateLink}
disabled={linksLimitReached}
- disabledTooltip={`You have reached the limit of ${PARTNER_LINKS_LIMIT} referral links.`}
+ disabledTooltip={
+ linksLimitReached
+ ? `You have reached the limit of ${PARTNER_LINKS_LIMIT} referral links.`
+ : undefined
+ }
/>
),
minSize: 60,