Skip to content

Commit 8fcb4fd

Browse files
committed
bugfix(pagination): fix error at get total pages
1 parent cd8b3eb commit 8fcb4fd

File tree

2 files changed

+59
-43
lines changed

2 files changed

+59
-43
lines changed

components/hackathons/Hackathons.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,6 @@ export default function Hackathons({
7676
const isHackathonCreator = session?.user?.custom_attributes.includes("hackathonCreator") || session?.user?.custom_attributes.includes("team1-admin");
7777

7878
const router = useRouter();
79-
const pageSize = 4;
8079

8180
const [pastHackathons, setPastHackathons] = useState<HackathonHeader[]>(
8281
initialPastHackathons
@@ -90,6 +89,7 @@ export default function Hackathons({
9089

9190
const [filters, setFilters] = useState<HackathonsFilters>(initialFilters);
9291
const [searchQuery, setSearchQuery] = useState<string>("");
92+
const [pageSize, setPageSize] = useState<number>(filters.recordsByPage ?? 4);
9393
const [totalPages, setTotalPages] = useState<number>(
9494
Math.ceil(totalPastHackathons / pageSize)
9595
);
@@ -346,7 +346,7 @@ export default function Hackathons({
346346
length: totalPages > 7 ? 7 : totalPages,
347347
},
348348
(_, i) =>
349-
currentPage +
349+
1 +
350350
i -
351351
(currentPage > 3
352352
? totalPages - currentPage > 3
@@ -386,7 +386,7 @@ export default function Hackathons({
386386
onValueChange={(value: string) =>
387387
handleFilterChange("recordsByPage", value)
388388
}
389-
value={String(filters.recordsByPage ?? 4)}
389+
value={String(pageSize) ?? 4}
390390
>
391391
<SelectTrigger className="border border-zinc-300 dark:border-zinc-800">
392392
<SelectValue placeholder="Select track" />

server/services/hackathons.ts

Lines changed: 56 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -67,29 +67,31 @@ export class ValidationError extends Error {
6767
}
6868
}
6969

70-
export async function getHackathonLite(hackathon: any): Promise<HackathonHeader> {
70+
export async function getHackathonLite(
71+
hackathon: any
72+
): Promise<HackathonHeader> {
7173
// Get user information if created_by exists
7274
if (hackathon.created_by) {
7375
try {
7476
const user = await getUserById(hackathon.created_by);
75-
hackathon.created_by_name = user?.name || user?.email || 'Unknown User';
77+
hackathon.created_by_name = user?.name || user?.email || "Unknown User";
7678
} catch (error) {
77-
console.error('Error fetching user info:', error);
78-
hackathon.created_by_name = 'Unknown User';
79+
console.error("Error fetching user info:", error);
80+
hackathon.created_by_name = "Unknown User";
7981
}
8082
}
8183

8284
// Get user information if updated_by exists
8385
if (hackathon.updated_by) {
8486
try {
8587
const user = await getUserById(hackathon.updated_by);
86-
hackathon.updated_by_name = user?.name || user?.email || 'Unknown User';
88+
hackathon.updated_by_name = user?.name || user?.email || "Unknown User";
8789
} catch (error) {
88-
console.error('Error fetching updated_by user info:', error);
89-
hackathon.updated_by_name = 'Unknown User';
90+
console.error("Error fetching updated_by user info:", error);
91+
hackathon.updated_by_name = "Unknown User";
9092
}
9193
}
92-
94+
9395
return hackathon;
9496
}
9597

@@ -141,43 +143,40 @@ export async function getFilteredHackathons(options: GetHackathonsOptions) {
141143
const offset = (page - 1) * pageSize;
142144

143145
let filters: any = {};
144-
146+
145147
// Build all conditions
146148
const conditions: any[] = [];
147-
149+
148150
if (options.location) {
149151
if (options.location == "InPerson") {
150152
conditions.push({ NOT: { location: "Online" } });
151153
} else {
152154
conditions.push({ location: options.location });
153155
}
154156
}
155-
157+
156158
if (options.created_by) {
157159
// Show hackathons where user is either creator OR updater
158160
conditions.push({
159161
OR: [
160162
{ created_by: options.created_by },
161-
{ updated_by: options.created_by }
162-
]
163+
{ updated_by: options.created_by },
164+
],
163165
});
164166
}
165-
167+
166168
if (options.date) {
167169
conditions.push({ date: options.date });
168170
}
169-
171+
170172
// Filter by visibility: only show public hackathons unless include_private is true
171173
// Treat null/undefined as public for backwards compatibility
172174
if (!options.include_private) {
173175
conditions.push({
174-
OR: [
175-
{ is_public: true },
176-
{ is_public: null },
177-
]
176+
OR: [{ is_public: true }, { is_public: null }],
178177
});
179178
}
180-
179+
181180
if (options.search) {
182181
const searchWords = options.search.split(/\s+/);
183182
let searchFilters: any[] = [];
@@ -215,7 +214,7 @@ export async function getFilteredHackathons(options: GetHackathonsOptions) {
215214

216215
conditions.push({ OR: searchFilters });
217216
}
218-
217+
219218
if (options.status) {
220219
switch (options.status) {
221220
case "ENDED":
@@ -232,15 +231,16 @@ export async function getFilteredHackathons(options: GetHackathonsOptions) {
232231
break;
233232
}
234233
}
235-
234+
236235
// Combine all conditions with AND
237236
if (conditions.length === 1) {
238237
filters = conditions[0];
239238
} else if (conditions.length > 1) {
240239
filters = { AND: conditions };
241240
}
242-
241+
243242
console.log("Filters: ", filters);
243+
const hackathonCount = await prisma.hackathon.count({ where: filters });
244244

245245
const hackathonList = await prisma.hackathon.findMany({
246246
where: filters,
@@ -265,7 +265,7 @@ export async function getFilteredHackathons(options: GetHackathonsOptions) {
265265
),
266266
} as HackathonHeader)
267267
),
268-
total: hackathonsLite.length,
268+
total: hackathonCount,
269269
page,
270270
pageSize,
271271
};
@@ -329,8 +329,10 @@ export async function updateHackathon(
329329
userId?: string
330330
): Promise<HackathonHeader> {
331331
// Skip validation if we're only updating is_public field
332-
const isOnlyPublicUpdate = Object.keys(hackathonData).length === 1 && hackathonData.hasOwnProperty('is_public');
333-
332+
const isOnlyPublicUpdate =
333+
Object.keys(hackathonData).length === 1 &&
334+
hackathonData.hasOwnProperty("is_public");
335+
334336
if (!isOnlyPublicUpdate) {
335337
const errors = validateHackathon(hackathonData);
336338
console.log(errors);
@@ -360,10 +362,11 @@ export async function updateHackathon(
360362
}
361363
// Build update data object with only provided fields
362364
const updateData: any = {};
363-
365+
364366
if (hackathonData.id !== undefined) updateData.id = hackathonData.id;
365367
if (hackathonData.title !== undefined) updateData.title = hackathonData.title;
366-
if (hackathonData.description !== undefined) updateData.description = hackathonData.description;
368+
if (hackathonData.description !== undefined)
369+
updateData.description = hackathonData.description;
367370
if (hackathonData.start_date !== undefined) {
368371
updateData.start_date = getDateWithTimezone(
369372
hackathonData.start_date,
@@ -376,22 +379,35 @@ export async function updateHackathon(
376379
hackathonData.timezone ?? existingHackathon.timezone
377380
);
378381
}
379-
if (hackathonData.location !== undefined) updateData.location = hackathonData.location;
380-
if (hackathonData.total_prizes !== undefined) updateData.total_prizes = hackathonData.total_prizes;
382+
if (hackathonData.location !== undefined)
383+
updateData.location = hackathonData.location;
384+
if (hackathonData.total_prizes !== undefined)
385+
updateData.total_prizes = hackathonData.total_prizes;
381386
if (hackathonData.tags !== undefined) updateData.tags = hackathonData.tags;
382-
if (hackathonData.timezone !== undefined) updateData.timezone = hackathonData.timezone;
387+
if (hackathonData.timezone !== undefined)
388+
updateData.timezone = hackathonData.timezone;
383389
if (hackathonData.icon !== undefined) updateData.icon = hackathonData.icon;
384-
if (hackathonData.banner !== undefined) updateData.banner = hackathonData.banner;
385-
if (hackathonData.small_banner !== undefined) updateData.small_banner = hackathonData.small_banner;
386-
if (hackathonData.participants !== undefined) updateData.participants = hackathonData.participants;
387-
if (hackathonData.top_most !== undefined) updateData.top_most = hackathonData.top_most;
388-
if (hackathonData.organizers !== undefined) updateData.organizers = hackathonData.organizers;
389-
if (hackathonData.custom_link !== undefined) updateData.custom_link = hackathonData.custom_link;
390-
if (hackathonData.created_by !== undefined) updateData.created_by = hackathonData.created_by;
391-
if (hackathonData.is_public !== undefined) updateData.is_public = hackathonData.is_public;
390+
if (hackathonData.banner !== undefined)
391+
updateData.banner = hackathonData.banner;
392+
if (hackathonData.small_banner !== undefined)
393+
updateData.small_banner = hackathonData.small_banner;
394+
if (hackathonData.participants !== undefined)
395+
updateData.participants = hackathonData.participants;
396+
if (hackathonData.top_most !== undefined)
397+
updateData.top_most = hackathonData.top_most;
398+
if (hackathonData.organizers !== undefined)
399+
updateData.organizers = hackathonData.organizers;
400+
if (hackathonData.custom_link !== undefined)
401+
updateData.custom_link = hackathonData.custom_link;
402+
if (hackathonData.created_by !== undefined)
403+
updateData.created_by = hackathonData.created_by;
404+
if (hackathonData.is_public !== undefined)
405+
updateData.is_public = hackathonData.is_public;
392406
if (userId) updateData.updated_by = userId;
393407
if (hackathonData.content !== undefined) {
394-
const content = { ...hackathonData.content } as unknown as Prisma.JsonObject;
408+
const content = {
409+
...hackathonData.content,
410+
} as unknown as Prisma.JsonObject;
395411
updateData.content = content;
396412
}
397413

0 commit comments

Comments
 (0)