Skip to content

Commit 503a1b5

Browse files
committed
fix the partners selection and search
1 parent eab71ed commit 503a1b5

File tree

4 files changed

+30
-10
lines changed

4 files changed

+30
-10
lines changed

apps/web/app/api/programs/[programId]/rewards/partners/route.ts

+7-2
Original file line numberDiff line numberDiff line change
@@ -33,15 +33,20 @@ export const GET = withWorkspace(
3333
partner: {
3434
select: {
3535
id: true,
36+
name: true,
37+
image: true,
38+
email: true,
3639
},
3740
},
3841
},
3942
},
4043
},
4144
});
4245

43-
return NextResponse.json(
44-
partners.map(({ programEnrollment: { partner } }) => partner.id),
46+
const flatPartners = partners.map(
47+
({ programEnrollment: { partner } }) => partner,
4548
);
49+
50+
return NextResponse.json(flatPartners);
4651
},
4752
);

apps/web/lib/swr/use-reward-partners.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { fetcher } from "@dub/utils";
22
import { useParams } from "next/navigation";
33
import useSWR from "swr";
44
import { z } from "zod";
5+
import { EnrolledPartnerProps } from "../types";
56
import { rewardPartnersQuerySchema } from "../zod/schemas/rewards";
67
import useWorkspace from "./use-workspace";
78

@@ -17,7 +18,7 @@ export default function useRewardPartners({
1718
const { programId } = useParams();
1819
const { id: workspaceId } = useWorkspace();
1920

20-
const { data, error, isLoading } = useSWR<string[]>(
21+
const { data, error, isLoading } = useSWR<EnrolledPartnerProps[]>(
2122
enabled && workspaceId && programId
2223
? `/api/programs/${programId}/rewards/partners?${new URLSearchParams({
2324
workspaceId,

apps/web/ui/partners/add-edit-reward-sheet.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ function RewardSheetContent({ setIsOpen, event, reward }: RewardSheetProps) {
136136

137137
useEffect(() => {
138138
if (rewardPartners && rewardPartners.length > 0) {
139-
setValue("partnerIds", rewardPartners);
139+
setValue("partnerIds", rewardPartners.map(partner => partner.id));
140140
}
141141
}, [rewardPartners, setValue]);
142142

apps/web/ui/partners/reward-partners-table.tsx

+20-6
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,19 @@ export function RewardPartnersTable({
2121
const [search, setSearch] = useState("");
2222
const [debouncedSearch] = useDebounce(search, 500);
2323

24-
const { data: partners } = usePartners({
24+
// Get all partners for the table
25+
const { data: allPartners } = usePartners({});
26+
27+
// Get filtered partners for the combobox
28+
const { data: searchPartners } = usePartners({
2529
query: {
2630
search: debouncedSearch,
2731
},
2832
});
2933

3034
const options = useMemo(
3135
() =>
32-
partners?.map((partner) => ({
36+
searchPartners?.map((partner) => ({
3337
icon: (
3438
<img
3539
alt={partner.name}
@@ -40,7 +44,7 @@ export function RewardPartnersTable({
4044
value: partner.id,
4145
label: partner.name,
4246
})),
43-
[partners],
47+
[searchPartners],
4448
);
4549

4650
const selectedPartnersOptions = useMemo(
@@ -54,21 +58,30 @@ export function RewardPartnersTable({
5458
const selectedPartners = useMemo(
5559
() =>
5660
partnerIds
57-
.map((id) => partners?.find((partner) => partner.id === id))
61+
.map((id) => allPartners?.find((partner) => partner.id === id))
5862
.filter((p): p is NonNullable<typeof p> => p != null)
5963
.map((partner) => ({
6064
id: partner.id,
6165
name: partner.name,
6266
email: partner.email,
6367
image: partner.image,
6468
})),
65-
[partnerIds, partners],
69+
[partnerIds, allPartners],
6670
);
6771

6872
const handlePartnerSelection = (
6973
selectedOptions: typeof selectedPartnersOptions,
7074
) => {
71-
const newSelectedIds = selectedOptions.map(({ value }) => value);
75+
// Get all currently selected IDs
76+
const currentIds = new Set(partnerIds);
77+
78+
// Add new selections
79+
selectedOptions.forEach(({ value }) => {
80+
currentIds.add(value);
81+
});
82+
83+
// Convert back to array
84+
const newSelectedIds = Array.from(currentIds);
7285
setPartners(newSelectedIds);
7386
};
7487

@@ -115,6 +128,7 @@ export function RewardPartnersTable({
115128
resourceName: (p) => `eligible partner${p ? "s" : ""}`,
116129
getRowId: (row: EnrolledPartnerProps) => row.id,
117130
loading,
131+
rowCount: selectedPartners.length,
118132
});
119133

120134
return (

0 commit comments

Comments
 (0)