Skip to content

fix: don't hang the calendar when the official body weight category is missing - #1311

Closed
hirak214 wants to merge 1 commit into
wger-project:masterfrom
hirak214:fix/calendar-missing-bodyweight-category
Closed

hirak214 wants to merge 1 commit into
wger-project:masterfrom
hirak214:fix/calendar-missing-bodyweight-category

Conversation

@hirak214

@hirak214 hirak214 commented Sep 19, 2026 •

Copy link
Copy Markdown

Proposed Changes

  • getBodyWeightCategory resolves null on an empty response instead of throwing (null, not undefined, since it resolves a react-query query). A genuine request failure still rejects through axios.
  • useBodyWeightQuery returns an empty history when there is no official body weight category, so its consumers (calendar, dashboard weight card, BMI calculator, plan weight chart) read as empty rather than failing.
  • The body weight screen renders its existing empty state (the add-weight FAB creates the category) instead of dereferencing a null category.

Related Issue(s)

Please check that the PR fulfills these requirements

  • Tests for the changes have been added (for bug fixes / features)

Bug

Opening the Calendar leaves the day-detail panel stuck on its loading spinner forever, even though the workout, measurement and nutrition data all load fine.

getBodyWeightCategory (api/bodyWeight.ts) throws No official body weight category found when the category query is empty. CalendarComponent gates its whole detail panel behind isSuccess of every query, so that throw keeps the panel from ever rendering.

Why the category can be missing

The comment there assumes "The server guarantees that every user has exactly one", but the server creates it lazily and the creating post_save signal is @disable_for_loaddata — so users loaded from a fixture never get one. The most common case is the default admin account shipped with the server: log in as it and open the calendar before recording a weight, and you hit the spinner. Imported users and deleted categories land in the same state.

Tests

  • api/bodyWeight.test.ts — empty response resolves to null, not a throw.
  • queries/bodyWeight.test.tsx — a missing category yields an empty history and does not fetch entries.
  • CalendarComponent.test.tsx — the detail panel still renders when the category is missing.

npm run typecheck and npm run lint are clean; the touched suites pass.

Reproduce

Fresh server, log in as the default admin (or any fixture-created user that never recorded a body weight), open the Calendar → detail panel spins forever. With this change it renders normally.

…ory is missing

The React calendar reads body weight through `getBodyWeightCategory`, which
threw "No official body weight category found" on an empty response. The
calendar gates its whole detail panel behind `isSuccess` of every query, so
that throw left the panel stuck on its loading spinner even though the workout,
measurement and nutrition reads all succeeded.

The official body weight category is created lazily (a `@disable_for_loaddata`
post_save signal, the profile, the legacy weight endpoint, the CSV import), so
it is not guaranteed to exist: accounts loaded from a fixture -- most notably
the default `admin` account shipped with the server -- start without one and
hit this on first visit to the calendar.

Treat a missing category as "no body weight recorded yet" instead of an error:

- getBodyWeightCategory resolves `null` on an empty response (null, not
  undefined, since it resolves a react-query query). A real request failure
  still rejects through axios.
- useBodyWeightQuery returns an empty history when there is no category, so its
  consumers (calendar, dashboard card, BMI calculator, weight chart) read as
  empty rather than failing.
- The body weight screen renders its empty state (the add-weight FAB creates
  the category) instead of dereferencing a null category.

Adds coverage: the api resolves null on empty, the query yields an empty
history without fetching entries, and the calendar renders its detail panel
when the category is missing.
@hirak214
hirak214 force-pushed the fix/calendar-missing-bodyweight-category branch from ee96292 to c379620 Compare September 19, 2026 09:51
rolandgeider added a commit to wger-project/wger that referenced this pull request Sep 26, 2026
@rolandgeider

Copy link
Copy Markdown
Member

Hi and thanks for the PR!

You are right that it's possible to create users without this category, but I think this is the wrong place to handle this case (we would need special code for the flutter app as well, etc.). I have fixed the bootstrap command so that we can be sure this doesn't happen and can continue to assume the category always exists

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants