From 05062e3b7da74f0449eed57c6496c5ccab6cecee Mon Sep 17 00:00:00 2001 From: Peter Date: Sun, 17 Sep 2023 20:05:15 -0400 Subject: [PATCH] Add transactions and awards to player page --- src/services/awardsService.ts | 26 +++++++++++ src/services/transactionsService.ts | 16 +++---- src/views/PlayerView.vue | 69 ++++++++++++++++++++++++++++- 3 files changed, 100 insertions(+), 11 deletions(-) create mode 100644 src/services/awardsService.ts diff --git a/src/services/awardsService.ts b/src/services/awardsService.ts new file mode 100644 index 0000000..cd84c3f --- /dev/null +++ b/src/services/awardsService.ts @@ -0,0 +1,26 @@ +import type { Manager, Team } from './apiResponseTypes' +import type { Player } from './playersService' +import { SITE_URL } from './utilities' + +export interface Award { + id: number, + name: string, + season: number, + timing: string, + manager1: Manager | null, + manager2: Manager | null, + player: Player | null, + team: Team | null, + image: string +} + +export async function fetchAwardsByPlayerName(playerName: string): Promise { + const response = await fetch(`${SITE_URL}/api/v3/awards?player_name=${playerName}&timing=off-season`) + + const awardsResponse: { + count: number + awards: Award[] + } = await response.json() + + return awardsResponse.awards +} diff --git a/src/services/transactionsService.ts b/src/services/transactionsService.ts index 74202ea..d0363bb 100644 --- a/src/services/transactionsService.ts +++ b/src/services/transactionsService.ts @@ -1,6 +1,6 @@ import type { Team } from './apiResponseTypes' import type { Player } from './playersService' -import { SITE_URL } from './utilities' +import { CURRENT_SEASON, SITE_URL } from './utilities' export interface Transaction { id: number @@ -25,17 +25,13 @@ export async function fetchTransactionsByTeamAndWeek(seasonNumber: number, teamA return transactionsResponse.transactions } -export async function fetchPlayerByName(seasonNumber: number, playerName: string): Promise { - const response = await fetch(`${SITE_URL}/api/v3/players?season=${seasonNumber}&name=${playerName}`) +export async function fetchTransactionsForCurrentSeasonByPlayerName(playerName: string): Promise { + const response = await fetch(`${SITE_URL}/api/v3/transactions?season=${CURRENT_SEASON}&player_name=${playerName}`) - const playersResponse: { + const transactionsResponse: { count: number - players: Player[] + transactions: Transaction[] } = await response.json() - if (playersResponse.count !== 1) { - throw new Error('playersServices.fetchPlayerByName - Expected one player, return contained none or many') - } - - return playersResponse.players[0] + return transactionsResponse.transactions } diff --git a/src/views/PlayerView.vue b/src/views/PlayerView.vue index 0699d7e..44f662b 100644 --- a/src/views/PlayerView.vue +++ b/src/views/PlayerView.vue @@ -36,13 +36,13 @@

Summary

- +
@@ -78,6 +78,64 @@ :post-season-batting-stats="postSeasonBattingStats" /> + + +
+
+

Transactions

+
+
+ + + + + + + + + + + + + + + + + + +
SeasonWeekPlayerOld TeamNew Team
{{ transaction.season }}{{ transaction.week }}{{ transaction.player.name }}{{ transaction.oldteam.sname }}{{ transaction.newteam.sname }}
+
+
+ +
+

Awards

+
+ + + + + + + + + + + + + + + + + +
SeasonAwardRecipientExtras
{{ award.season }}{{ award.name }}{{ award.player?.name }} + + {{ award.team.sname }} + +
+
+
+ @@ -97,6 +155,8 @@ import PlayerBattingSummaryTable from '@/components/PlayerBattingSummaryTable.vu import PlayerPitchingSummaryTable from '@/components/PlayerPitchingSummaryTable.vue' import { fetchPitchingStatsBySeasonAndPlayerId, fetchPitchingStatsForLastFourGamesBySeasonAndPlayerId, type PitchingStat } from '@/services/pitchingStatsService' import { fetchFieldingStatsBySeasonAndPlayerId, fetchFieldingStatsForLastFourGamesBySeasonAndPlayerId, type FieldingStat } from '@/services/fieldingStatsService' +import { fetchTransactionsForCurrentSeasonByPlayerName, type Transaction } from '@/services/transactionsService' +import { fetchAwardsByPlayerName, type Award } from '@/services/awardsService' export default { name: "PlayerView", @@ -117,6 +177,10 @@ export default { regularSeasonFieldingStats: [] as FieldingStat[], postSeasonFieldingStats: [] as FieldingStat[], last4GamesFielding: [] as FieldingStat[], + + // Transactions and Awards + transactions: [] as Transaction[], + awards: [] as Award[] } }, components: { @@ -244,6 +308,9 @@ export default { this.postSeasonPitchingStats = (await Promise.all(playerSeasons.filter(isNotUndefined).map(player => fetchPitchingStatsBySeasonAndPlayerId(player!.season, player!.id, false)))).filter(isNotUndefined) this.regularSeasonFieldingStats = (await Promise.all(playerSeasons.filter(isNotUndefined).map(player => fetchFieldingStatsBySeasonAndPlayerId(player!.season, player!.id, true)))).flatMap(stat => stat).filter(isNotUndefined) this.postSeasonFieldingStats = (await Promise.all(playerSeasons.filter(isNotUndefined).map(player => fetchFieldingStatsBySeasonAndPlayerId(player!.season, player!.id, false)))).flatMap(stat => stat).filter(isNotUndefined) + + this.transactions = await fetchTransactionsForCurrentSeasonByPlayerName(this.player.name) + this.awards = await fetchAwardsByPlayerName(this.player.name) }, async tryFetchPlayerByNameForAnySeason(seasonNumber: number, playerName: string): Promise { do {