Premier League Predictor: FastAPI & PostgreSQL — Chapter 10, Exercise 2 ==================================================== TASK Explain exactly how seedUsedTeams resolves the gap Chapter 4 flagged about usedTeamIds resetting on page reload, and what real request it makes to do so. SOLUTION Chapter 4's own honest gap was that usedTeamIds started completely empty every time the page loaded, with no way to know about fixtures that had already been entered in a previous visit — reloading the page mid-way through entering a gameweek would re-enable every team button, even ones already fixtured, purely because the in-memory Set had no persistence across a reload. seedUsedTeams fixes this by never trusting the in-memory set as the starting point at all. Every time the selection-changed event fires — which happens both on the very first page load (via initSelectors's own call to loadGameweeks, which calls selectGameweek) and on any later gameweek switch — seedUsedTeams makes a real request: GET /api/gameweeks/${gameweekId}/fixtures This is Chapter 4's own existing list_fixtures route, returning every fixture already stored in the database for that specific gameweek — the real, current, server-side state, not anything remembered in the browser. seedUsedTeams then rebuilds usedTeamIds completely from scratch by reading each returned fixture's home_team_id and away_team_id, and immediately calls disableUsedButtons() (the same function Chapter 4 already wrote) to gray out every team that request just confirmed is genuinely already used. Because this runs on every load and every gameweek switch — not just once — a page reload no longer matters: the very next thing that happens is a fresh, accurate fetch of the real fixture data, so usedTeamIds is correct again within one request regardless of whether the page was freshly loaded, switched from a different gameweek, or switched back to a gameweek visited earlier in the same session. WHY THIS WORKS AS AN ANSWER ---------------------------- It restates the precise original gap from Chapter 4, names the exact real endpoint seedUsedTeams calls to fix it, and explains why running that fetch on every selection change (not just once) is what actually makes the fix reliable across reloads and gameweek switches alike.