diff --git a/app/teams/[slug]/page.tsx b/app/teams/[slug]/page.tsx index c25dc50..cb4fab9 100644 --- a/app/teams/[slug]/page.tsx +++ b/app/teams/[slug]/page.tsx @@ -56,18 +56,16 @@ export default function TeamPage({ params }: { params: Promise<{ slug: string }> skip: !team?.id, }) - // Load all scorers to filter by team const { data: scorerData } = useQuery(gql` - query TeamScorers { - topScorers(limit: 200) { + query TeamScorers($teamId: Int!) { + topScorers(teamId: $teamId, limit: 30) { playerName goals penalties ownGoals tournaments team { id name iso2 } } } - `) + `, { variables: { teamId: team?.id ?? 0 }, skip: !team?.id }) - const allScorers = scorerData?.topScorers ?? [] - const teamScorers = team ? allScorers.filter((s: { team?: { id: number } | null }) => s.team?.id === team.id).slice(0, 15) : [] + const teamScorers = scorerData?.topScorers ?? [] const teamMatches: MatchRow[] = matchesData?.matches ?? [] // Group matches by year for the history display diff --git a/lib/graphql/resolvers/index.ts b/lib/graphql/resolvers/index.ts index 902b6f9..be0f372 100644 --- a/lib/graphql/resolvers/index.ts +++ b/lib/graphql/resolvers/index.ts @@ -152,11 +152,13 @@ export const resolvers = { return found ? teamWithSlug(found) : null } catch (e) { if (isMissingTable(e)) return null; throw e } }, - async topScorers(_: unknown, { year, limit = 20 }: { year?: number; limit?: number }) { + async topScorers(_: unknown, { year, limit = 20, teamId }: { year?: number; limit?: number; teamId?: number }) { try { - const conditions = year - ? sql`AND m.tournament_year = ${year} AND m.is_quali_playoff = false` - : sql`AND m.is_quali_playoff = false` + const conditions = sql` + ${year ? sql`AND m.tournament_year = ${year}` : sql``} + ${teamId ? sql`AND g.team_id = ${teamId}` : sql``} + AND m.is_quali_playoff = false + ` const rows = await db.execute(sql` SELECT g.player_name, diff --git a/lib/graphql/schema.ts b/lib/graphql/schema.ts index 3794cd9..d0c9c13 100644 --- a/lib/graphql/schema.ts +++ b/lib/graphql/schema.ts @@ -170,7 +170,7 @@ export const typeDefs = /* GraphQL */ ` teams: [Team!]! team(slug: String!): Team - topScorers(year: Int, limit: Int): [ScorerEntry!]! + topScorers(year: Int, limit: Int, teamId: Int): [ScorerEntry!]! player(name: String!): ScorerEntry hatTricks(year: Int): [HatTrick!]!