All files / src/utils defaultAgeGroup.js

100% Statements 27/27
91.66% Branches 11/12
100% Functions 1/1
100% Lines 27/27

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 281x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 81x 81x 81x 156x 11x 11x 156x 70x 81x 81x  
/**
 * Which age group a filtered view opens on when this browser has no
 * remembered pick (SB-1286).
 *
 * Precedence, first one that exists in `ageGroups` wins:
 *   1. the account's saved preference (profile.preferences.default_age_group_id)
 *   2. the age group of the viewer's current-season team (SB-599)
 *   3. U14, the anonymous fallback
 *
 * The remembered pick (useFilterMemory, SB-1112) sits above all three and is
 * applied by the view itself — last pick wins.
 *
 * Preferences live in jsonb with no foreign key, so a saved id can name an age
 * group that no longer exists. That falls through to the next rung rather than
 * selecting something the dropdown cannot show.
 */
export const pickDefaultAgeGroupId = ({ preferred, team, ageGroups }) => {
  const list = ageGroups || [];
  const known = new Set(list.map(ag => Number(ag.id)));
  for (const id of [preferred, team]) {
    if (id !== null && id !== undefined && known.has(Number(id))) {
      return Number(id);
    }
  }
  const u14 = list.find(ag => ag.name === 'U14');
  return u14 ? u14.id : null;
};