Add transactions and awards to player page

This commit is contained in:
Peter 2023-09-17 20:05:15 -04:00
parent d56e52f20e
commit 05062e3b7d
3 changed files with 100 additions and 11 deletions

View File

@ -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<Award[]> {
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
}

View File

@ -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<Player> {
const response = await fetch(`${SITE_URL}/api/v3/players?season=${seasonNumber}&name=${playerName}`)
export async function fetchTransactionsForCurrentSeasonByPlayerName(playerName: string): Promise<Transaction[]> {
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
}

View File

@ -36,13 +36,13 @@
<div class="col-sm-12">
<h3>Summary</h3>
</div>
<!-- Batting Summary -->
<PlayerBattingSummaryTable v-if="isBatter" :current-season-batting="currentSeasonBatting"
:current-post-season-batting="currentPostSeasonBatting"
:regular-season-batting-stats="regularSeasonBattingStats" />
<PlayerPitchingSummaryTable v-else :current-season-pitching="currentSeasonPitching"
:current-post-season-pitching="currentPostSeasonPitching"
:regular-season-pitching-stats="regularSeasonPitchingStats" />
<!-- Injury rating + last 2 app -->
<div class="col-sm-4">
<div class="table-responsive-xl" style="max-width:20rem">
<table class="table table-sm table-striped">
@ -78,6 +78,64 @@
:post-season-batting-stats="postSeasonBattingStats" />
<PlayerCareerFieldingTable :regular-season-fielding-stats="regularSeasonFieldingStats"
:post-season-fielding-stats="postSeasonFieldingStats" />
<!-- Transactions and awards -->
<div v-if="transactions.length || awards.length" class="row" id="transactions-row">
<div v-if="transactions.length" class="col-sm-6">
<h3>Transactions</h3>
<div class="table-responsive-xl" style="max-width:35rem">
<table class="table table-sm table-striped" id="transactions">
<thead class="thead-dark">
<tr>
<th>Season</th>
<th>Week</th>
<th>Player</th>
<th>Old Team</th>
<th>New Team</th>
</tr>
</thead>
<tbody id="transactions-table">
<tr v-for="transaction in transactions">
<td>{{ transaction.season }}</td>
<td>{{ transaction.week }}</td>
<td>{{ transaction.player.name }}</td>
<td>{{ transaction.oldteam.sname }}</td>
<td>{{ transaction.newteam.sname }}</td>
</tr>
</tbody>
</table>
</div>
</div>
<div v-if="awards.length" class="col-sm-6" id="awards-row">
<h3>Awards</h3>
<div class="table-responsive-xl" style="max-width:40rem">
<table class="table table-sm table-striped" id="table-awards">
<thead class="thead-dark">
<tr>
<th>Season</th>
<th>Award</th>
<th>Recipient</th>
<th>Extras</th>
</tr>
</thead>
<tbody id="team-awards">
<tr v-for="award in awards">
<td>{{ award.season }}</td>
<td>{{ award.name }}</td>
<td>{{ award.player?.name }}</td>
<td>
<RouterLink v-if="award.team"
:to="{ name: 'team', params: { seasonNumber: award.team.season, teamAbbreviation: award.team.abbrev } }">
{{ award.team.sname }}
</RouterLink>
</td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
</div>
</div>
</template>
@ -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<Player | undefined> {
do {