Skip to content

Commit 6dc90b4

Browse files
authored
Fix #631: respect user and group default currency when creating expenses (#647)
* fix: respect user and group default currency when creating expenses Closes #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. * fix: apply full currency preference chain in add expense Address review feedback on #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. * fix: keep last-used currency in store across remounts 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. * fix: await session refresh before navigating after expense submit 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.
1 parent 9ed899a commit 6dc90b4

2 files changed

Lines changed: 24 additions & 14 deletions

File tree

src/components/AddExpense/AddExpensePage.tsx

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -172,17 +172,15 @@ export const AddOrEditExpensePage: React.FC<{
172172
navPromise = async () => router.back();
173173
}
174174

175-
navPromise()
175+
update((session: any) => ({
176+
...session,
177+
user: {
178+
...(session?.user ?? {}),
179+
currency,
180+
},
181+
}))
182+
.then(() => navPromise())
176183
.then(() => resetState())
177-
.then(() =>
178-
update((session: any) => ({
179-
...session,
180-
user: {
181-
...(session?.user ?? {}),
182-
currency,
183-
},
184-
})),
185-
)
186184
.catch(console.error);
187185
}
188186
}

src/pages/add.tsx

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,9 @@ const AddPage: NextPageWithUser<{
5050
const { setMaxUploadFileSizeMB } = useAppStore((s) => s.actions);
5151
setMaxUploadFileSizeMB(maxUploadFileSizeMB);
5252

53+
const router = useRouter();
54+
const { friendId, groupId, expenseId } = router.query;
55+
5356
useEffect(() => {
5457
setCurrentUser({
5558
...user,
@@ -61,10 +64,13 @@ const AddPage: NextPageWithUser<{
6164
obapiProviderId: user.obapiProviderId ?? null,
6265
bankingId: user.bankingId ?? null,
6366
});
64-
}, [setCurrentUser, user]);
65-
66-
const router = useRouter();
67-
const { friendId, groupId, expenseId } = router.query;
67+
if (router.isReady && !groupId) {
68+
const preferredCurrency = user.currency ?? user.defaultCurrency;
69+
if (preferredCurrency) {
70+
setCurrency(parseCurrencyCode(preferredCurrency));
71+
}
72+
}
73+
}, [setCurrentUser, setCurrency, groupId, router.isReady, user]);
6874

6975
const _groupId = parseInt(groupId as string);
7076
const _friendId = parseInt(friendId as string);
@@ -117,6 +123,11 @@ const AddPage: NextPageWithUser<{
117123
.map((gu) => gu.user)
118124
.filter((groupUser) => groupUser.id !== currentUser.id),
119125
]);
126+
const preferredCurrency =
127+
currentUser.currency ?? groupQuery.data.defaultCurrency ?? currentUser.defaultCurrency;
128+
if (preferredCurrency) {
129+
setCurrency(parseCurrencyCode(preferredCurrency));
130+
}
120131
const parsedDefaultSplit = deserializeDefaultSplit(groupQuery.data.defaultSplit);
121132
if (parsedDefaultSplit) {
122133
applySplitPreset(parsedDefaultSplit.splitType, parsedDefaultSplit.shares);
@@ -131,6 +142,7 @@ const AddPage: NextPageWithUser<{
131142
currentUser,
132143
setGroup,
133144
setParticipants,
145+
setCurrency,
134146
applySplitPreset,
135147
]);
136148

0 commit comments

Comments
 (0)