Skip to content

Commit

Permalink
Add pick the winner to daily prizes
Browse files Browse the repository at this point in the history
  • Loading branch information
nalves599 committed Feb 18, 2025
1 parent ff6ff62 commit c257640
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 7 deletions.
54 changes: 49 additions & 5 deletions src/app/(authenticated)/prizes/DailyPrizesTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,25 @@
import EventDayButton from "@/components/EventDayButton";
import GridList from "@/components/GridList";
import List from "@/components/List";
import { PrizeTile } from "@/components/prize";
import { getEventFullDate } from "@/utils/utils";
import { useEffect, useMemo, useState } from "react";
import { PrizeParticipant, PrizeTile } from "@/components/prize";
import { AchievementService } from "@/services/AchievementService";
import { getEventFullDate, isMember } from "@/utils/utils";
import { useEffect, useMemo, useRef, useState } from "react";

interface DailyPrizesTable {
user?: User;
cannonToken: string;
prizes: Prize[];
}

export default function DailyPrizesTable({ prizes }: DailyPrizesTable) {
export default function DailyPrizesTable({
user,
cannonToken,
prizes,
}: DailyPrizesTable) {
const [showingDay, setShowingDay] = useState<string | null>(null);
const [dailyParticipants, setDailyParticipants] =
useState<PrizeParticipant[]>();

const prizesByDay = useMemo(() => {
const sortedPrizes = prizes.sort((a, b) => a.name.localeCompare(b.name));
Expand All @@ -38,6 +47,35 @@ export default function DailyPrizesTable({ prizes }: DailyPrizesTable) {
setShowingDay(sortedDays.find((d) => d === today) || sortedDays[0]);
}, [sortedDays]);

useEffect(() => {
if (showingDay) getDailyPrizeParticipants(showingDay);
}, [showingDay]);

async function getDailyPrizeParticipants(day: string) {
const achievements = await AchievementService.getAchievements();
const startDate = new Date(day);
const endDate = new Date(`${day}T23:59:59Z`);
const entries = achievements
?.filter(
(a) =>
startDate <= new Date(a.validity?.from ?? "") &&
endDate >= new Date(a.validity?.to ?? ""),
)
.reduce(
(acc, a) => {
a.users?.forEach((u) => {
acc[u] = (acc[u] ?? 0) + a.value;
});
return acc;
},
{} as Record<string, number>,
);
setDailyParticipants(
entries &&
Object.keys(entries).map((u) => ({ userId: u, entries: entries[u] })),
);
}

return (
<>
<GridList>
Expand All @@ -55,7 +93,13 @@ export default function DailyPrizesTable({ prizes }: DailyPrizesTable) {
.map((d) => (
<List key={d} description={getEventFullDate(d)}>
{prizesByDay[d].map((p) => (
<PrizeTile key={p.id} prize={p} />
<PrizeTile
key={p.id}
participants={dailyParticipants}
prize={p}
cannonToken={cannonToken}
pickWinner={!!user && isMember(user.role)}
/>
))}
</List>
))}
Expand Down
6 changes: 5 additions & 1 deletion src/app/(authenticated)/prizes/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,11 @@ export default async function Prizes() {
Visit SINFO and get the chance to win
</span>
</div>
<DailyPrizesTable prizes={dailyPrizes} />
<DailyPrizesTable
prizes={dailyPrizes}
cannonToken={session!.cannonToken}
user={user || undefined}
/>
</div>

{/* Sessions Prizes */}
Expand Down
2 changes: 1 addition & 1 deletion src/components/GridCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export default function GridCard({
/>
)}
{label && (
<span className="m-auto bg-sinfo-primary text-white rounded-md px-2 py-1 uppercase">
<span className="m-auto bg-sinfo-secondary font-bold text-white rounded-md px-2 py-1 uppercase">
{label}
</span>
)}
Expand Down

0 comments on commit c257640

Please sign in to comment.