Extract group finding to method
This commit is contained in:
parent
7a0bbccc19
commit
e1cadf9108
|
|
@ -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 (teamIds.includes(favorite.id)) {
|
|
||||||
scrollTo = groupEl; // Update the scroll target to the group element
|
|
||||||
}
|
}
|
||||||
});
|
|
||||||
|
if (!scrollTo) {
|
||||||
|
console.error('No match or group found for the favorite team');
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
let scrollTimeout;
|
let scrollTimeout;
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue