Extract group finding to method

This commit is contained in:
Daniel Schädler 2025-03-19 09:25:41 +01:00
parent 7a0bbccc19
commit e1cadf9108
1 changed files with 21 additions and 8 deletions

View File

@ -72,6 +72,21 @@ export function FavoriteBar({teams}) {
return lowestMatch; return lowestMatch;
} }
function findScrollToGroup(favoriteId) {
// Look for group elements that contain the favorite team's ID
const groupElements = document.querySelectorAll('[data-teams]');
const scrollToNotFound = null;
groupElements.forEach(groupEl => {
const teamIds = groupEl.getAttribute('data-teams').split(',').map(id => parseInt(id, 10));
if (teamIds.includes(favoriteId)) {
return groupEl;
}
});
return scrollToNotFound;
}
const scrollToFavorite = () => { const scrollToFavorite = () => {
if (!favorite) { if (!favorite) {
return; // Exit if there is no favorite team selected return; // Exit if there is no favorite team selected
@ -83,14 +98,12 @@ export function FavoriteBar({teams}) {
if (lowestMatch) { if (lowestMatch) {
scrollTo = lowestMatch; scrollTo = lowestMatch;
} else { } else {
// If no match is found, look for group elements that contain the favorite team's ID scrollTo = findScrollToGroup(favorite.id);
const groupElements = document.querySelectorAll('[data-teams]'); }
groupElements.forEach(groupEl => {
const teamIds = groupEl.getAttribute('data-teams').split(',').map(id => parseInt(id, 10)); if (!scrollTo) {
if (teamIds.includes(favorite.id)) { console.error('No match or group found for the favorite team');
scrollTo = groupEl; // Update the scroll target to the group element return;
}
});
} }
let scrollTimeout; let scrollTimeout;