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 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 23x 23x 23x 27x 27x 27x 9x 9x 9x 9x 9x 18x 18x 18x 18x 18x 18x 27x 27x 27x 27x 5x 27x 27x 27x 27x 19x 18x 18x 27x 1x 158x 158x 155x 23x 23x 23x 23x 158x 158x 4x 4x 4x 4x 158x 158x 12x 12x 158x 158x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 2x 2x 3x 1x 1x 1x 1x 158x 158x 3x 3x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 3x 158x 158x 2x 2x 2x 158x 158x 158x 158x 158x 158x 158x 158x 158x 158x 158x 158x 158x 158x 158x 1x 1x 7x 7x 7x 7x 7x 7x 7x | /**
* Bracket-follows composable.
*
* Singleton reactive state shared by every bracket FollowButton. Mirrors
* useTeamFollows: module-level reactive state, useBracketFollows() returns
* the same refs every call. A bracket is keyed by the tuple
* (tournament_id, tournament_group, age_group_id), joined into a string key.
*
* Backend endpoints (idempotent):
* GET /api/users/me/bracket-follows → { follows: [{ tournament_id, tournament_group, age_group_id, ... }] }
* POST /api/users/me/bracket-follows → 201 { ..., following: true }
* DELETE /api/users/me/bracket-follows?tournament_id=&tournament_group=&age_group_id= → 204
*/
import { reactive, computed } from 'vue';
import { useAuthStore } from '../stores/auth';
import { getApiBaseUrl } from '../config/api';
const state = reactive({
followedKeys: new Set(),
follows: [],
loaded: false,
loading: false,
error: null,
});
let inflightLoad = null;
// Stable string key for a bracket tuple.
function bracketKey(tournamentId, tournamentGroup, ageGroupId) {
return `${Number(tournamentId)}:${tournamentGroup}:${Number(ageGroupId)}`;
}
async function fetchFollows() {
const authStore = useAuthStore();
if (!authStore.isAuthenticated.value) {
state.followedKeys = new Set();
state.follows = [];
state.loaded = true;
return;
}
state.loading = true;
state.error = null;
try {
const data = await authStore.apiRequest(
`${getApiBaseUrl()}/api/users/me/bracket-follows`
);
const rows = data?.follows || [];
state.follows = rows;
state.followedKeys = new Set(
rows.map(r =>
bracketKey(r.tournament_id, r.tournament_group, r.age_group_id)
)
);
state.loaded = true;
} catch (err) {
state.error = err.message || String(err);
state.followedKeys = new Set();
state.follows = [];
} finally {
state.loading = false;
}
}
export function useBracketFollows() {
const authStore = useAuthStore();
const ensureLoaded = () => {
if (state.loaded || state.loading) return inflightLoad || Promise.resolve();
inflightLoad = fetchFollows().finally(() => {
inflightLoad = null;
});
return inflightLoad;
};
const refresh = () => {
inflightLoad = fetchFollows().finally(() => {
inflightLoad = null;
});
return inflightLoad;
};
const isFollowing = (tournamentId, tournamentGroup, ageGroupId) =>
state.followedKeys.has(
bracketKey(tournamentId, tournamentGroup, ageGroupId)
);
const follow = async (tournamentId, tournamentGroup, ageGroupId) => {
const key = bracketKey(tournamentId, tournamentGroup, ageGroupId);
if (state.followedKeys.has(key)) return { success: true };
// Optimistic
state.followedKeys.add(key);
state.error = null;
try {
await authStore.apiRequest(
`${getApiBaseUrl()}/api/users/me/bracket-follows`,
{
method: 'POST',
body: JSON.stringify({
tournament_id: Number(tournamentId),
tournament_group: tournamentGroup,
age_group_id: Number(ageGroupId),
}),
}
);
refresh();
return { success: true };
} catch (err) {
state.followedKeys.delete(key);
state.error = err.message || String(err);
return { success: false, error: state.error };
}
};
const unfollow = async (tournamentId, tournamentGroup, ageGroupId) => {
const key = bracketKey(tournamentId, tournamentGroup, ageGroupId);
if (!state.followedKeys.has(key)) return { success: true };
// Optimistic
state.followedKeys.delete(key);
state.error = null;
try {
const qs = new URLSearchParams({
tournament_id: String(Number(tournamentId)),
tournament_group: tournamentGroup,
age_group_id: String(Number(ageGroupId)),
});
await authStore.apiRequest(
`${getApiBaseUrl()}/api/users/me/bracket-follows?${qs.toString()}`,
{ method: 'DELETE' }
);
refresh();
return { success: true };
} catch (err) {
state.followedKeys.add(key);
state.error = err.message || String(err);
return { success: false, error: state.error };
}
};
const toggle = async (tournamentId, tournamentGroup, ageGroupId) => {
return isFollowing(tournamentId, tournamentGroup, ageGroupId)
? unfollow(tournamentId, tournamentGroup, ageGroupId)
: follow(tournamentId, tournamentGroup, ageGroupId);
};
return {
follows: computed(() => state.follows),
followedKeys: computed(() => state.followedKeys),
loaded: computed(() => state.loaded),
loading: computed(() => state.loading),
error: computed(() => state.error),
ensureLoaded,
refresh,
isFollowing,
follow,
unfollow,
toggle,
};
}
// Test-only: reset module-level singleton between tests.
export function _resetBracketFollowsForTest() {
state.followedKeys = new Set();
state.follows = [];
state.loaded = false;
state.loading = false;
state.error = null;
inflightLoad = null;
}
|