Skip to content

Commit

Permalink
Merge pull request #43 from unmonk/v0.2
Browse files Browse the repository at this point in the history
schedules bug fix, compressed og image
  • Loading branch information
unmonk authored Nov 26, 2024
2 parents bed1971 + 418458c commit 0dec9c5
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 66 deletions.
121 changes: 66 additions & 55 deletions components/admin/schedule-run-button.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,18 @@ import { Loader2Icon } from "lucide-react";
import { Button } from "../ui/button";
import { useState } from "react";
import { api } from "@/convex/_generated/api";
import { ScrollArea } from "@radix-ui/react-scroll-area";
import { useQuery, useAction } from "convex/react";
import { ScrollArea } from "../ui/scroll-area";
import { useAction } from "convex/react";
import { toast } from "sonner";
import { Dialog, DialogContent, DialogHeader, DialogTitle } from "../ui/dialog";
import {
Table,
TableBody,
TableCell,
TableHead,
TableHeader,
TableRow,
} from "../ui/table";

export default function ScheduleRunButton() {
const runSchedules = useAction(api.schedules.schedules);
Expand All @@ -30,12 +39,14 @@ export default function ScheduleRunButton() {
>
>({});
const [loading, setLoading] = useState(false);
const [isOpen, setIsOpen] = useState(false);

const handleRunSchedules = async () => {
try {
setLoading(true);
const response = await runSchedules();
setScheduleResponse(response);
setIsOpen(true);
toast.success("Schedules run successfully");
} catch (error) {
toast.error("Error running schedules");
Expand All @@ -45,7 +56,7 @@ export default function ScheduleRunButton() {
};

return (
<div className="flex gap-2">
<>
<Button
onClick={handleRunSchedules}
disabled={loading}
Expand All @@ -60,58 +71,58 @@ export default function ScheduleRunButton() {
)}
</Button>

{Object.keys(scheduleResponse).length > 0 && (
<div className="space-y-4">
<h2 className="text-xl font-semibold">Schedule Response</h2>
<ScrollArea className="h-[500px]">
{Object.keys(scheduleResponse).map((league) => (
<div key={league}>
<h3 className="text-lg font-semibold">{league}</h3>
{scheduleResponse[league].error && (
<p className="text-red-500">
{scheduleResponse[league].error}
</p>
)}
<div className="grid grid-cols-2 md:grid-cols-3 lg:grid-cols-5 gap-4 mt-2 mb-4">
<div className="p-3 bg-muted rounded-lg">
<div className="text-sm text-muted-foreground">
ESPN Games
</div>
<div className="text-lg font-medium">
{scheduleResponse[league].gamesOnSchedule}
</div>
</div>
<div className="p-3 bg-muted rounded-lg">
<div className="text-sm text-muted-foreground"></div>
<div className="text-lg font-medium">
Existing Matchups
<span className="font-bold">
{scheduleResponse[league].existingMatchups}
</span>
</div>
</div>
<div className="p-3 bg-muted rounded-lg">
<div className="text-sm text-muted-foreground">
Updated Info
</div>
<div className="text-lg font-medium">
{scheduleResponse[league].matchupsUpdated}
</div>
</div>
<div className="p-3 bg-muted rounded-lg">
<div className="text-sm text-muted-foreground">
New Matchups
</div>
<div className="text-lg font-medium">
{scheduleResponse[league].scoreMatchupsCreated}
</div>
</div>
</div>
</div>
))}
<Dialog open={isOpen} onOpenChange={setIsOpen}>
<DialogContent className="max-w-5xl">
<DialogHeader>
<DialogTitle>Schedule Results</DialogTitle>
</DialogHeader>

<ScrollArea className="h-[500px] w-full">
<Table>
<TableHeader>
<TableRow>
<TableHead>League</TableHead>
<TableHead className="text-right">ESPN Games</TableHead>
<TableHead className="text-right">
Existing Matchups
</TableHead>
<TableHead className="text-right">Updated</TableHead>
<TableHead className="text-right">New Matchups</TableHead>
<TableHead>Status</TableHead>
</TableRow>
</TableHeader>
<TableBody>
{Object.entries(scheduleResponse).map(([league, data]) => (
<TableRow key={league}>
<TableCell className="font-medium">{league}</TableCell>
<TableCell className="text-right">
{data.gamesOnSchedule}
</TableCell>
<TableCell className="text-right">
{data.existingMatchups}
</TableCell>
<TableCell className="text-right">
{data.matchupsUpdated}
</TableCell>
<TableCell className="text-right">
{data.scoreMatchupsCreated}
</TableCell>
<TableCell>
{data.error ? (
<span className="text-red-500 text-sm">
{data.error}
</span>
) : (
<span className="text-green-500 text-sm">Success</span>
)}
</TableCell>
</TableRow>
))}
</TableBody>
</Table>
</ScrollArea>
</div>
)}
</div>
</DialogContent>
</Dialog>
</>
);
}
17 changes: 7 additions & 10 deletions convex/scheduleprocessors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ export async function processSchedule(
// Iterate through each day in the schedule data
for (const day in scheduleData) {
const games = scheduleData[day].games;

// Skip if no games exist for this day
if (!games) continue;

Expand All @@ -139,7 +140,6 @@ export async function processSchedule(
// Increment total games counter
leagueResponse.gamesOnSchedule++;
const gameId = game.id;

// Handle existing matchups for this game
if (existingMatchups[gameId]) {
// Increment existing matchups counter
Expand All @@ -165,6 +165,7 @@ export async function processSchedule(
game: game.shortName,
result: result.result,
});
leagueResponse.scoreMatchupsCreated += result.matchupsCreated || 0;
}
}

Expand All @@ -185,7 +186,6 @@ export async function processGame(
result: validationResult.result,
};
}

// Process existing matchup
if (existingMatchups[game.id]?.length > 0) {
//check if needs to update
Expand Down Expand Up @@ -262,8 +262,8 @@ function hasMatchupChanged(matchup: Doc<"matchups">, game: Game) {
function isGameValid(game: Game) {
const competitionStatus = game.competitions[0].status?.type?.name;
const competitors = game.competitions[0].competitors;
const home = competitors.find((c) => c.id === game.competitions[0].venue?.id);
const away = competitors.find((c) => c.id !== game.competitions[0].venue?.id);
const home = competitors.find((c) => c.homeAway === "home");
const away = competitors.find((c) => c.homeAway === "away");

if (!competitionStatus)
return {
Expand All @@ -280,10 +280,7 @@ function isGameValid(game: Game) {
valid: false,
result: "Not enough competitors",
};
if (
game.competitions[0].competitors[0].team.name === "TBD" ||
game.competitions[0].competitors[1].team.name === "TBD"
)
if (home?.team.name === "TBD" || away?.team.name === "TBD")
return {
valid: false,
result: "TBD team",
Expand Down Expand Up @@ -323,8 +320,8 @@ async function createNewMatchupByType(
if (matchupType === "SCORE") {
const competition = game.competitions[0];
const competitors = competition.competitors;
const home = competitors.find((c) => c.id === competition.venue?.id);
const away = competitors.find((c) => c.id !== competition.venue?.id);
const home = competitors.find((c) => c.homeAway === "home");
const away = competitors.find((c) => c.homeAway === "away");
if (!home || !away || !competition) return;

if (matchupType === "SCORE") {
Expand Down
3 changes: 2 additions & 1 deletion convex/schedules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,8 @@ export function getScheduleEndpoints(league: League) {
const tomorrowString = tomorrow.toISOString().split("T")[0].replace(/-/g, "");
const theDayAfterTomorrowString = theDayAfterTomorrow
.toISOString()
.split("T")[0];
.split("T")[0]
.replace(/-/g, "");
const dates = [
`${yesterdayString}`,
`${todayString}`,
Expand Down
Binary file modified public/images/og-image.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 0dec9c5

Please sign in to comment.