All files / src/components PostMatchEditor.vue

73.87% Statements 164/222
50% Branches 1/2
7.14% Functions 1/14
73.87% Lines 164/222

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 2601x 1x 1x 1x 1x 1x 1x 1x   1x 1x 1x   1x   1x 1x 1x   1x   1x 1x 1x 1x 1x   1x   1x 1x 1x 1x 1x 1x 1x 1x 1x           1x 1x 1x 1x 1x 1x 1x           1x 1x 1x   1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x   1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x       1x 1x 1x 1x   1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 3x 3x 3x 3x   3x 3x 3x 3x 3x   3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x   3x 3x                     3x 3x                               3x 3x               3x               3x             3x             3x               3x               3x       3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 1x 1x    
<template>
  <div class="mt-3" data-testid="post-match-section">
    <!-- Toggle button -->
    <button
      @click="toggleSection"
      data-testid="post-match-toggle"
      class="w-full flex items-center gap-2 px-3 py-2 bg-slate-700 hover:bg-slate-600 text-white text-sm font-semibold rounded-lg transition-colors"
      :class="{ 'bg-slate-600': showSection }"
    >
      <span class="text-base font-bold w-5 text-center">{{
        showSection ? '-' : '+'
      }}</span>
      Match Stats
    </button>
 
    <div v-if="showSection" class="mt-2 bg-slate-800 rounded-lg p-3">
      <!-- Loading state -->
      <div v-if="dataLoading" class="text-slate-400 text-sm text-center py-4">
        Loading match stats...
      </div>
 
      <!-- No roster message -->
      <div
        v-else-if="dataLoaded && !homeRoster.length && !awayRoster.length"
        class="text-slate-400 text-sm text-center py-4"
      >
        No roster available for either team.
      </div>
 
      <!-- Content -->
      <template v-else-if="dataLoaded">
        <!-- Team tabs -->
        <div class="flex gap-2 mb-3">
          <button
            @click="activeTab = 'home'"
            data-testid="post-match-tab-home"
            class="flex-1 py-2 px-3 text-sm font-medium rounded-lg border-2 transition-colors truncate"
            :class="
              activeTab === 'home'
                ? 'border-blue-500 bg-blue-500/20 text-white'
                : 'border-slate-600 text-slate-400 hover:border-slate-500'
            "
          >
            {{ match.home_team_name }}
          </button>
          <button
            @click="activeTab = 'away'"
            data-testid="post-match-tab-away"
            class="flex-1 py-2 px-3 text-sm font-medium rounded-lg border-2 transition-colors truncate"
            :class="
              activeTab === 'away'
                ? 'border-blue-500 bg-blue-500/20 text-white'
                : 'border-slate-600 text-slate-400 hover:border-slate-500'
            "
          >
            {{ match.away_team_name }}
          </button>
        </div>
 
        <!-- Home team panel -->
        <div v-if="activeTab === 'home'">
          <TeamStatsPanel
            :team-id="match.home_team_id"
            :team-name="match.home_team_name"
            :roster="homeRoster"
            :events="teamEvents(match.home_team_id)"
            :player-stats="homeStats"
            :can-edit="canEditTeam(match.home_team_id)"
            :saving="saving"
            :error="error"
            @add-goal="handleAddGoal"
            @remove-goal="handleRemoveGoal"
            @add-substitution="handleAddSubstitution"
            @remove-substitution="handleRemoveSubstitution"
            @add-card="handleAddCard"
            @remove-card="handleRemoveCard"
            @save-stats="handleSaveStats"
          />
        </div>
 
        <!-- Away team panel -->
        <div v-if="activeTab === 'away'">
          <TeamStatsPanel
            :team-id="match.away_team_id"
            :team-name="match.away_team_name"
            :roster="awayRoster"
            :events="teamEvents(match.away_team_id)"
            :player-stats="awayStats"
            :can-edit="canEditTeam(match.away_team_id)"
            :saving="saving"
            :error="error"
            @add-goal="handleAddGoal"
            @remove-goal="handleRemoveGoal"
            @add-substitution="handleAddSubstitution"
            @remove-substitution="handleRemoveSubstitution"
            @add-card="handleAddCard"
            @remove-card="handleRemoveCard"
            @save-stats="handleSaveStats"
          />
        </div>
      </template>
    </div>
  </div>
</template>
 
<script>
import { ref, computed } from 'vue';
import { usePostMatchStats } from '../composables/usePostMatchStats';
import { useMatchLineup } from '../composables/useMatchLineup';
import TeamStatsPanel from './post-match/TeamStatsPanel.vue';
 
export default {
  name: 'PostMatchEditor',
  components: {
    TeamStatsPanel,
  },
  props: {
    matchId: { type: [Number, String], required: true },
    match: { type: Object, required: true },
    events: { type: Array, default: () => [] },
  },
  emits: ['events-changed'],
  setup(props, { emit }) {
    const showSection = ref(false);
    const activeTab = ref('home');
    const dataLoaded = ref(false);
    const dataLoading = ref(false);
 
    // Use lineup composable to get rosters
    const { homeRoster, awayRoster, fetchTeamRosters } = useMatchLineup(
      props.matchId,
      computed(() => props.match)
    );
 
    // Use post-match stats composable
    const {
      homeStats,
      awayStats,
      saving,
      error,
      canEditTeam,
      fetchAllStats,
      addGoal,
      removeGoal,
      addSubstitution,
      removeSubstitution,
      addCard,
      removeCard,
      savePlayerStats,
    } = usePostMatchStats(
      props.matchId,
      computed(() => props.match)
    );
 
    // Filter events by team
    function teamEvents(teamId) {
      return props.events.filter(
        e =>
          e.team_id === teamId &&
          !e.is_deleted &&
          ['goal', 'substitution', 'red_card', 'yellow_card'].includes(
            e.event_type
          )
      );
    }
 
    // Toggle section and lazy-load data
    async function toggleSection() {
      showSection.value = !showSection.value;
 
      if (showSection.value && !dataLoaded.value) {
        dataLoading.value = true;
        try {
          await Promise.all([fetchTeamRosters(), fetchAllStats()]);
          dataLoaded.value = true;
        } catch (err) {
          console.error('Failed to load post-match data:', err);
        } finally {
          dataLoading.value = false;
        }
      }
    }
 
    // Event handlers
    async function handleAddGoal(goalData) {
      const result = await addGoal(goalData);
      if (result.success) {
        emit('events-changed');
        await fetchAllStats();
      }
    }
 
    async function handleRemoveGoal(eventId) {
      const result = await removeGoal(eventId);
      if (result.success) {
        emit('events-changed');
        await fetchAllStats();
      }
    }
 
    async function handleAddSubstitution(subData) {
      const result = await addSubstitution(subData);
      if (result.success) {
        emit('events-changed');
      }
    }
 
    async function handleRemoveSubstitution(eventId) {
      const result = await removeSubstitution(eventId);
      if (result.success) {
        emit('events-changed');
      }
    }
 
    async function handleAddCard(cardData) {
      const result = await addCard(cardData);
      if (result.success) {
        emit('events-changed');
        await fetchAllStats();
      }
    }
 
    async function handleRemoveCard(eventId) {
      const result = await removeCard(eventId);
      if (result.success) {
        emit('events-changed');
        await fetchAllStats();
      }
    }
 
    async function handleSaveStats(teamId, entries) {
      await savePlayerStats(teamId, entries);
    }
 
    return {
      showSection,
      activeTab,
      dataLoaded,
      dataLoading,
      homeRoster,
      awayRoster,
      homeStats,
      awayStats,
      saving,
      error,
      canEditTeam,
      teamEvents,
      toggleSection,
      handleAddGoal,
      handleRemoveGoal,
      handleAddSubstitution,
      handleRemoveSubstitution,
      handleAddCard,
      handleRemoveCard,
      handleSaveStats,
    };
  },
};
</script>