mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-10-18 21:16:47 +00:00
fix(budget): use Math.trunc for user colour index, complete settlement fixtures
The `| 0` truncation in colorForUserId/colorFor existed to coerce an undefined user_id to 0, but the settlement API always returns user_id — only the two BudgetPanel test fixtures omitted it. Add user_id to the fixtures, switch both helpers to Math.trunc (with a Number.isFinite guard in colorForUserId preserving the old undefined->0 behaviour), and drop nothing else: TravelerPicker only ever receives required numbers.
This commit is contained in:
@@ -75,7 +75,8 @@ export function splitColorFor(userId: number, order: number) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export function colorForUserId(userId: number) {
|
export function colorForUserId(userId: number) {
|
||||||
return SPLIT_COLORS[((userId | 0) - 1 + SPLIT_COLORS.length * 1000) % SPLIT_COLORS.length]
|
const id = Number.isFinite(userId) ? Math.trunc(userId) : 0
|
||||||
|
return SPLIT_COLORS[(id - 1 + SPLIT_COLORS.length * 1000) % SPLIT_COLORS.length]
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -371,7 +371,7 @@ describe('BudgetPanel', () => {
|
|||||||
{ user_id: 2, username: 'bob', balance: 10, avatar_url: null },
|
{ user_id: 2, username: 'bob', balance: 10, avatar_url: null },
|
||||||
],
|
],
|
||||||
flows: [
|
flows: [
|
||||||
{ from: { username: 'alice', avatar_url: null }, to: { username: 'bob', avatar_url: null }, amount: 10 },
|
{ from: { user_id: 1, username: 'alice', avatar_url: null }, to: { user_id: 2, username: 'bob', avatar_url: null }, amount: 10 },
|
||||||
],
|
],
|
||||||
})
|
})
|
||||||
)
|
)
|
||||||
@@ -465,7 +465,7 @@ describe('BudgetPanel', () => {
|
|||||||
{ user_id: 1, username: 'alice', avatar_url: '/uploads/avatars/alice.jpg', balance: -30 },
|
{ user_id: 1, username: 'alice', avatar_url: '/uploads/avatars/alice.jpg', balance: -30 },
|
||||||
{ user_id: 2, username: 'bob', avatar_url: null, balance: 30 },
|
{ user_id: 2, username: 'bob', avatar_url: null, balance: 30 },
|
||||||
],
|
],
|
||||||
flows: [{ from: { username: 'alice', avatar_url: '/uploads/avatars/alice.jpg' }, to: { username: 'bob', avatar_url: null }, amount: 30 }]
|
flows: [{ from: { user_id: 1, username: 'alice', avatar_url: '/uploads/avatars/alice.jpg' }, to: { user_id: 2, username: 'bob', avatar_url: null }, amount: 30 }]
|
||||||
})
|
})
|
||||||
),
|
),
|
||||||
http.get('/api/trips/1/budget/per-person', () => HttpResponse.json({ summary: [] })),
|
http.get('/api/trips/1/budget/per-person', () => HttpResponse.json({ summary: [] })),
|
||||||
|
|||||||
@@ -9,7 +9,7 @@ import { useTranslation } from '../../i18n'
|
|||||||
// picker, the card and the cost-split chips (mirrors BudgetPanel.helpers).
|
// picker, the card and the cost-split chips (mirrors BudgetPanel.helpers).
|
||||||
function colorFor(userId: number) {
|
function colorFor(userId: number) {
|
||||||
const n = SPLIT_COLORS.length
|
const n = SPLIT_COLORS.length
|
||||||
return SPLIT_COLORS[((((userId | 0) - 1) % n) + n) % n]
|
return SPLIT_COLORS[(((Math.trunc(userId) - 1) % n) + n) % n]
|
||||||
}
|
}
|
||||||
|
|
||||||
function Avatar({ userId, username, avatarUrl, size = 22, dim = false }: {
|
function Avatar({ userId, username, avatarUrl, size = 22, dim = false }: {
|
||||||
|
|||||||
Reference in New Issue
Block a user