Skip to content

Fix #631: respect user and group default currency when creating expenses#647

Merged
krokosik merged 4 commits into
oss-apps:mainfrom
phof:fix/default-currency
May 18, 2026
Merged

Fix #631: respect user and group default currency when creating expenses#647
krokosik merged 4 commits into
oss-apps:mainfrom
phof:fix/default-currency

Conversation

@phof
Copy link
Copy Markdown
Contributor

@phof phof commented May 17, 2026

Summary

Closes #631.

The expense form always defaulted to USD regardless of the user's or group's configured default currency. The defaults were fetched from the backend but never applied to the form's currency field.

Changes

  • src/pages/add.tsx:
    • For non-group expenses, initializes the currency from `user.defaultCurrency` in the `setCurrentUser` effect.
    • For group expenses, initializes the currency from `group.defaultCurrency` (falling back to the user's default) in the group initialization effect.
    • The `setCurrentUser` effect is gated on `router.isReady` so that the first render — before Next.js populates `router.query` — does not falsely treat a group page as a non-group page and apply the wrong default.
    • Moved the `useRouter` / `router.query` destructuring above the `setCurrentUser` effect so `groupId` is in scope.
  • `src/store/addStore.ts`: `resetState` now restores the user's default currency on unmount, so navigating away and back lands on the correct default instead of stale state.

Test plan

  • Open `/add` as a user whose default currency is set (e.g. JPY). Currency picker shows JPY.
  • Open `/add?groupId=N` for a group whose default currency is EUR. Currency picker shows EUR.
  • Open `/add?groupId=N` for a group with no default currency, where the user default is JPY. Picker shows JPY.
  • Manually change currency to USD on the add page, navigate away, navigate back. Picker resets to the user's default.
  • Close the window and reopen. Picker still shows the user's default (not USD).
  • Edit an existing expense: picker still shows the expense's original currency (unchanged behaviour).

Disclaimer

Parts of this change were drafted with the help of Claude Opus 4.7.

Closes oss-apps#631

The expense form always defaulted to USD regardless of the user's or
group's configured default currency. The user/group defaults were
fetched but never applied to the form's currency field.

Changes:
- src/pages/add.tsx: set currency from user.defaultCurrency for
  non-group expenses, and from group.defaultCurrency (falling back to
  the user's) when entering a group context. The setCurrentUser effect
  is gated on router.isReady so the first render (before Next.js
  populates router.query) does not falsely treat a group page as a
  non-group page and apply the wrong default.
- src/store/addStore.ts: resetState now restores the user's default
  currency on unmount so navigating away and back lands on the correct
  default instead of stale state.

Disclaimer: parts of this change were drafted with the help of
Claude Opus 4.7.
Copy link
Copy Markdown
Collaborator

@krokosik krokosik left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The fix is appropriate but not quite there yet. It omits the core of the regression, which is the missing functionality of defaulting to last used currency. It is kept in currency field in the currentUser object. The correct preference chain is:

  1. currentUser.currency
  2. group.defaultCurrency
  3. currentUser.defaultCurrency

Comment thread src/store/addStore.ts Outdated
Address review feedback on oss-apps#647: prefer the user's last-used currency
(User.currency) before falling back to the group default and the user's
defaultCurrency setting. Also drop the currency override in resetState
so that resetting the form keeps the last-used currency.

Preference chain when initializing the add-expense page:
  1. currentUser.currency (last used)
  2. group.defaultCurrency (when in a group context)
  3. currentUser.defaultCurrency

Disclaimer: parts of this change were drafted with the help of Claude Opus 4.7.
phof added a commit to phof/split-pro that referenced this pull request May 17, 2026
Address review feedback on oss-apps#647: prefer the user's last-used currency
(User.currency) before falling back to the group default and the user's
defaultCurrency setting. Also drop the currency override in resetState
so that resetting the form keeps the last-used currency.

Preference chain when initializing the add-expense page:
  1. currentUser.currency (last used)
  2. group.defaultCurrency (when in a group context)
  3. currentUser.defaultCurrency

Disclaimer: parts of this change were drafted with the help of Claude Opus 4.7.
phof added 2 commits May 17, 2026 17:10
When picking a currency, also update currentUser.currency in the
add-expense store so the preference chain on the next /add mount sees
the just-picked value without waiting for the async next-auth session
update to settle. On mount, the AddPage effect also preserves the
stored currency for the same user id instead of overwriting it with
potentially stale session data.

Disclaimer: parts of this change were drafted with the help of Claude Opus 4.7.
Replace the previous remount-time currency preservation with a simpler
fix: in the expense submit handler, await the next-auth session update
(which writes the just-picked currency back into the session) BEFORE
navigating away. This guarantees that any subsequent /add mount reads
the up-to-date currentUser.currency without local-store overrides.

Reverts the in-store overwrite from 49b9778 so currentUser in the
add-expense store keeps mirroring the session user as-is.

Disclaimer: parts of this change were drafted with the help of Claude Opus 4.7.
@phof
Copy link
Copy Markdown
Contributor Author

phof commented May 17, 2026

@krokosik Thank you, see if the latest changes make sense now

@phof phof requested a review from krokosik May 18, 2026 14:32
@krokosik krokosik merged commit 6dc90b4 into oss-apps:main May 18, 2026
1 check passed
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.

[Bug] Unable to set default currency

2 participants