All files / src/composables useMatchLineup.js

96.85% Statements 123/127
87.5% Branches 21/24
100% Functions 6/6
96.85% Lines 123/127

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 1521x 1x 1x 1x 1x 1x           64x 64x 64x   1x 48x   48x 48x 48x   48x 48x 48x 48x   48x 48x 48x 48x 48x 32x 32x   31x 32x   30x 30x 30x 30x 30x 30x 30x 30x 30x   32x 32x   32x 32x 32x 32x 32x 1x 1x 1x 32x   48x 48x 48x 48x 58x 58x 58x 58x 58x 58x       58x   48x 48x 48x 48x 30x 30x   29x 29x   29x 29x 29x 29x 29x   29x 29x 30x   30x 29x 29x 30x   48x 48x 48x 48x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x   2x 2x 2x 2x 1x 1x 1x 1x 2x   2x 3x 1x 1x 1x 3x   48x 48x 48x 48x   48x 48x 48x 48x   48x 48x 48x 48x 48x 48x 48x  
/**
 * Match Lineup Composable
 *
 * Shared logic for fetching and saving team rosters and lineups.
 * Used by both useLiveMatch (live matches) and MatchDetailView (pre-match).
 */
 
import { ref, isRef } from 'vue';
import { useAuthStore } from '../stores/auth';
import { getApiBaseUrl } from '../config/api';
 
function unwrapMatchData(matchData) {
  return isRef(matchData) ? matchData.value : matchData;
}
 
export function useMatchLineup(matchId, matchData) {
  const authStore = useAuthStore();
 
  // Roster state
  const homeRoster = ref([]);
  const awayRoster = ref([]);
 
  // Lineup state
  const homeLineup = ref(null);
  const awayLineup = ref(null);
  const lineupLoading = ref(false);
 
  /**
   * Fetch rosters for both teams in the match.
   * Returns { home: [], away: [] } for compatibility with LiveAdminControls.
   */
  async function fetchTeamRosters() {
    const match = unwrapMatchData(matchData);
    if (!match) return { home: [], away: [] };
 
    const { home_team_id, away_team_id, season_id } = match;
    if (!season_id) return { home: [], away: [] };
 
    try {
      const [homeResponse, awayResponse] = await Promise.all([
        authStore.apiRequest(
          `${getApiBaseUrl()}/api/teams/${home_team_id}/roster?season_id=${season_id}`
        ),
        authStore.apiRequest(
          `${getApiBaseUrl()}/api/teams/${away_team_id}/roster?season_id=${season_id}`
        ),
      ]);
 
      homeRoster.value = homeResponse?.roster || [];
      awayRoster.value = awayResponse?.roster || [];
 
      return {
        home: homeRoster.value,
        away: awayRoster.value,
      };
    } catch (err) {
      console.error('Error fetching rosters:', err);
      return { home: [], away: [] };
    }
  }
 
  /**
   * Fetch lineup for a single team.
   */
  async function fetchLineup(teamId) {
    try {
      const response = await authStore.apiRequest(
        `${getApiBaseUrl()}/api/matches/${matchId}/lineup/${teamId}`
      );
      return response;
    } catch (err) {
      console.error('Error fetching lineup:', err);
      return null;
    }
  }
 
  /**
   * Fetch lineups for both teams, updating homeLineup/awayLineup refs.
   */
  async function fetchLineups() {
    const match = unwrapMatchData(matchData);
    if (!match) return;
 
    const { home_team_id, away_team_id } = match;
    lineupLoading.value = true;
 
    try {
      const [homeResponse, awayResponse] = await Promise.all([
        fetchLineup(home_team_id),
        fetchLineup(away_team_id),
      ]);
 
      homeLineup.value = homeResponse;
      awayLineup.value = awayResponse;
    } catch (err) {
      console.error('Error fetching lineups:', err);
    } finally {
      lineupLoading.value = false;
    }
  }
 
  /**
   * Save lineup for a team. Updates the corresponding local ref.
   */
  async function saveLineup(teamId, formationName, positions) {
    try {
      const response = await authStore.apiRequest(
        `${getApiBaseUrl()}/api/matches/${matchId}/lineup/${teamId}`,
        {
          method: 'PUT',
          body: JSON.stringify({
            formation_name: formationName,
            positions,
          }),
        }
      );
 
      // Update local state
      const match = unwrapMatchData(matchData);
      if (match) {
        if (teamId === match.home_team_id) {
          homeLineup.value = response;
        } else if (teamId === match.away_team_id) {
          awayLineup.value = response;
        }
      }
 
      return { success: true, lineup: response };
    } catch (err) {
      console.error('Error saving lineup:', err);
      return { success: false, error: err.message };
    }
  }
 
  return {
    // Roster state
    homeRoster,
    awayRoster,
 
    // Lineup state
    homeLineup,
    awayLineup,
    lineupLoading,
 
    // Methods
    fetchTeamRosters,
    fetchLineup,
    fetchLineups,
    saveLineup,
  };
}