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.

update getProgram

Steven Tey251edc0a0ee1
diff --git a/apps/web/app/(ee)/partners.dub.co/(apply)/[programSlug]/layout.tsx b/apps/web/app/(ee)/partners.dub.co/(apply)/[programSlug]/layout.tsx
index eb73d8346e..9f681c11c7 100644
--- a/apps/web/app/(ee)/partners.dub.co/(apply)/[programSlug]/layout.tsx
+++ b/apps/web/app/(ee)/partners.dub.co/(apply)/[programSlug]/layout.tsx
@@ -1,4 +1,5 @@
import { getProgram } from "@/lib/fetchers/get-program";
+import { getProgramApplicationRewardsAndDiscount } from "@/lib/partners/get-program-application-rewards";
import { formatRewardDescription } from "@/ui/partners/format-reward-description";
import { prisma } from "@dub/prisma";
import { Wordmark } from "@dub/ui";
@@ -15,23 +16,19 @@ export async function generateMetadata({
}) {
const program = await getProgram({
slug: programSlug,
- include: ["defaultRewards"],
+ include: ["allRewards", "allDiscounts"],
});
if (!program) {
notFound();
}
- if (!program.rewards || program.rewards.length === 0) {
- notFound();
- }
-
- const reward = program.rewards[0];
+ const { rewards } = getProgramApplicationRewardsAndDiscount(program);
return constructMetadata({
title: `${program.name} Affiliate Program`,
description: `Join the ${program.name} affiliate program and earn ${formatRewardDescription(
- { reward },
+ { reward: rewards[0] },
)} by referring ${program.name} to your friends and followers.`,
image: `${APP_DOMAIN}/api/og/program?slug=${program.slug}`,
});
diff --git a/apps/web/app/(ee)/partners.dub.co/(dashboard)/programs/[programSlug]/apply/page.tsx b/apps/web/app/(ee)/partners.dub.co/(dashboard)/programs/[programSlug]/apply/page.tsx
index 6d5a016c6c..2a8e3ab32c 100644
--- a/apps/web/app/(ee)/partners.dub.co/(dashboard)/programs/[programSlug]/apply/page.tsx
+++ b/apps/web/app/(ee)/partners.dub.co/(dashboard)/programs/[programSlug]/apply/page.tsx
@@ -31,13 +31,11 @@ export default async function ProgramDetailsPage({
<PageWidthWrapper className="mb-10 mt-4">
<BackLink href="/programs">Programs</BackLink>
<div className="mt-8 grid grid-cols-1 gap-x-16 gap-y-10 lg:grid-cols-[300px_minmax(0,600px)]">
- <div>
- <ProgramSidebar
- program={program}
- applicationRewards={rewards}
- applicationDiscount={discount}
- />
- </div>
+ <ProgramSidebar
+ program={program}
+ applicationRewards={rewards}
+ applicationDiscount={discount}
+ />
<div>
<div
className="relative"
diff --git a/apps/web/app/(ee)/partners.dub.co/(dashboard)/programs/[programSlug]/apply/program-sidebar.tsx b/apps/web/app/(ee)/partners.dub.co/(dashboard)/programs/[programSlug]/apply/program-sidebar.tsx
index 1315479b2b..1224f5edbe 100644
--- a/apps/web/app/(ee)/partners.dub.co/(dashboard)/programs/[programSlug]/apply/program-sidebar.tsx
+++ b/apps/web/app/(ee)/partners.dub.co/(dashboard)/programs/[programSlug]/apply/program-sidebar.tsx
@@ -28,6 +28,7 @@ export function ProgramSidebar({
const { programEnrollment, loading: isLoadingProgramEnrollment } =
useProgramEnrollment({
swrOpts: {
+ keepPreviousData: true,
shouldRetryOnError: (err) => err.status !== 404,
revalidateOnFocus: false,
},
diff --git a/apps/web/lib/fetchers/get-program.ts b/apps/web/lib/fetchers/get-program.ts
index 2d4081b45a..076b4234fb 100644
--- a/apps/web/lib/fetchers/get-program.ts
+++ b/apps/web/lib/fetchers/get-program.ts
@@ -8,20 +8,14 @@ export const getProgram = cache(
include,
}: {
slug: string;
- include?: (
- | "defaultRewards"
- | "allRewards"
- | "defaultDiscount"
- | "allDiscounts"
- )[];
+ include?: ("allRewards" | "allDiscounts")[];
}) => {
const program = await prisma.program.findUnique({
where: {
slug,
},
include: {
- ...((include?.includes("defaultRewards") ||
- include?.includes("allRewards")) && {
+ ...(include?.includes("allRewards") && {
rewards: include.includes("allRewards")
? true
: {
@@ -30,8 +24,7 @@ export const getProgram = cache(
},
},
}),
- ...((include?.includes("defaultDiscount") ||
- include?.includes("allDiscounts")) && {
+ ...(include?.includes("allDiscounts") && {
discounts: include.includes("allDiscounts")
? true
: {
@@ -47,7 +40,7 @@ export const getProgram = cache(
return null;
}
- if (include?.includes("defaultRewards")) {
+ if (include?.includes("allRewards")) {
program.rewards = sortRewardsByEventOrder(program.rewards);
}
diff --git a/apps/web/lib/partners/get-program-application-rewards.ts b/apps/web/lib/partners/get-program-application-rewards.ts
index 2d0a883aa3..f7530ea7ba 100644
--- a/apps/web/lib/partners/get-program-application-rewards.ts
+++ b/apps/web/lib/partners/get-program-application-rewards.ts
@@ -1,6 +1,7 @@
import { DiscountProps, RewardProps } from "@/lib/types";
import { Prisma } from "@prisma/client";
import { programLanderSchema } from "../zod/schemas/program-lander";
+import { sortRewardsByEventOrder } from "./sort-rewards-by-event-order";
// TODO: Add an alternative to this function that doesn't require fetching all rewards and discounts
@@ -20,7 +21,7 @@ export function getProgramApplicationRewardsAndDiscount({
landerData: Prisma.JsonValue;
}) {
const defaults = {
- rewards: rewards.filter((r) => r.default),
+ rewards: sortRewardsByEventOrder(rewards.filter((r) => r.default)),
discount: discounts.find((d) => d.default) || null,
};