Repository navigation
Expand file tree
/
Copy pathuseHackathons.js
More file actions
87 lines (79 loc) · 3.06 KB
/
Copy pathuseHackathons.js
File metadata and controls
87 lines (79 loc) · 3.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
import { useQuery } from "@tanstack/react-query";
import { HackathonAPI } from "../api/hackathon.api.js";
// Backend paginates (default limit 12, max not confirmed). We pull a high
// limit per bucket to approximate the old "fetch everything" behaviour.
// Revisit with real pagination if any bucket regularly exceeds this.
const FETCH_LIMIT = 100;
// phases[] replaced the old flat startDate/endDate/submissionEndDate fields.
// Derive an overall window from all phases (earliest start -> latest end),
// matching the same logic the backend's lifecycleStatus virtual uses.
const derivePhaseWindow = (phases) => {
if (!phases || phases.length === 0) return { start: null, end: null };
const starts = phases.map((p) => new Date(p.startDate).getTime());
const ends = phases.map((p) => new Date(p.endDate).getTime());
return {
start: new Date(Math.min(...starts)),
end: new Date(Math.max(...ends)),
};
};
// Submission countdown should target the SUBMISSION phase's endDate, not
// the overall window end. Falls back to the overall end if there's no
// SUBMISSION phase.
const deriveSubmissionEnd = (phases, fallbackEnd) => {
const submissionPhases = (phases || []).filter(
(p) => p.phaseType === "SUBMISSION"
);
if (submissionPhases.length === 0) return fallbackEnd;
return new Date(
Math.max(...submissionPhases.map((p) => new Date(p.endDate).getTime()))
);
};
const formatDate = (d) =>
d ? d.toLocaleDateString("en-IN", { day: "numeric", month: "short" }) : "";
// Only adds derived convenience fields on top of the backend object.
// Every native backend field name (lifecycleStatus, numParticipants,
// techStacks, image.url, phases, etc.) is left untouched — no aliasing.
const mapHackathon = (h) => {
const { start, end } = derivePhaseWindow(h.phases);
const submissionEnd = deriveSubmissionEnd(h.phases, end);
const totalPrize = (h.prizes || []).reduce(
(sum, p) => sum + (p.amount || 0),
0
);
return {
...h,
overallStart: start,
overallEnd: end,
submissionEnd,
totalPrize,
formattedDate:
start && end ? `${formatDate(start)} – ${formatDate(end)}` : "TBA",
};
};
export const useHackathons = () => {
return useQuery({
queryKey: ["hackathons"],
queryFn: async () => {
// Backend filters by lifecycleStatus server-side via ?status=.
// We do NOT recompute lifecycle client-side.
const [activeRes, upcomingRes, completedRes] = await Promise.all([
HackathonAPI.getHackathons({ status: "ACTIVE", limit: FETCH_LIMIT }),
HackathonAPI.getHackathons({
status: "UPCOMING",
limit: FETCH_LIMIT,
}),
HackathonAPI.getHackathons({
status: "COMPLETED",
limit: FETCH_LIMIT,
}),
]);
return {
active: (activeRes.data.data || []).map(mapHackathon),
upcoming: (upcomingRes.data.data || []).map(mapHackathon),
completed: (completedRes.data.data || []).map(mapHackathon),
};
},
staleTime: 30 * 1000, // 30 seconds — Redis already absorbs repeat load
refetchOnWindowFocus: true,
});
};