From 971ae3f12e9036c0836de82705aff0ee4ec08829 Mon Sep 17 00:00:00 2001 From: Peter Date: Fri, 15 Nov 2024 16:43:02 -0600 Subject: [PATCH] Add sorting to pitching table --- src/components/TeamPitchingTable.vue | 122 ++++++++++++++++++++------- 1 file changed, 90 insertions(+), 32 deletions(-) diff --git a/src/components/TeamPitchingTable.vue b/src/components/TeamPitchingTable.vue index 412a8d9..30bcc31 100644 --- a/src/components/TeamPitchingTable.vue +++ b/src/components/TeamPitchingTable.vue @@ -6,35 +6,35 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + @@ -118,6 +118,14 @@ import { aggregatePitchingStats, fetchPitchingStatsBySeasonAndTeamId, type PitchingStat } from '@/services/pitchingStatsService' import { outsToInnings, winPercentage, hitsPer9, hrsPer9 } from '@/services/utilities' +interface ExtendedPitchingStat extends PitchingStat { + playerName: string + winPct: string + irsPct: string + hPer9: string + hrPer9: string +} + export default { name: 'TeamPitchingTable', props: { @@ -127,7 +135,9 @@ export default { }, data() { return { - pitchingStats: [] as PitchingStat[] + pitchingStats: [] as ExtendedPitchingStat[], + sortKey: 'ip' as keyof ExtendedPitchingStat, + sortOrder: -1 } }, computed: { @@ -144,14 +154,47 @@ export default { }, watch: { teamId(newValue, oldValue) { - if (newValue !== oldValue) + if (newValue !== oldValue) { + this.pitchingStats = [] + this.sortKey = 'ip' + this.sortOrder = -1 this.fetchData() + } } }, methods: { async fetchData(): Promise { const unsortedPitchingStats: PitchingStat[] = await fetchPitchingStatsBySeasonAndTeamId(this.seasonNumber, this.teamId, this.isRegularSeason) - this.pitchingStats = unsortedPitchingStats.sort((s1, s2) => s2.outs - s1.outs) + this.pitchingStats = unsortedPitchingStats + .map(s => ( + { + ...s, + playerName: s.player.name, + winPct: this.winPercentage(s), + irsPct: this.formatIRSPercentage(s), + hPer9: this.hitsPer9(s), + hrPer9: this.hrsPer9(s) + })) + .sort((s1, s2) => s2.ip - s1.ip) + }, + sortBy(stat: keyof ExtendedPitchingStat): void { + this.setKey(stat) + + this.pitchingStats.sort((s1, s2) => s2[stat] < s1[stat] ? this.sortOrder : -1 * this.sortOrder) + }, + setKey(stat: keyof ExtendedPitchingStat): void { + if (this.sortKey === stat) { + // if key currently selected, flip sort order + this.sortOrder *= -1 + } else { + this.sortKey = stat + this.sortOrder = stat === 'playerName' ? 1 : -1 + } + }, + getArrow(stat: keyof ExtendedPitchingStat): string { + if (this.sortKey !== stat) return 'faux-arrow' + + return this.sortOrder > 0 ? 'up' : 'down' }, outsToInnings(stat: PitchingStat): string { return outsToInnings(stat) @@ -173,3 +216,18 @@ export default { } } + +
PlayerWLW-L%ERAGGSSVHDBSVIPHRERHRBBSOHBPBKWPIRIRSIRS%WHIPH/9HR/9BB/9SO/9SO/BBPlayerWLW-L%ERAGGSSVHDBSVIPHRERHRBBSOHBPBKWPIRIRSIRS%WHIPH/9HR/9BB/9SO/9SO/BB