Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions apps/frontend/src/components/VenuePrices.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,35 @@ import { formatPrice, formatPriceLimitation } from '#/lib/format-price'
import { useUserTimeZone } from '#/lib/use-user-time-zone'
import type { VenuePriceSighting } from '#/types/venue'

const SIZE_VOLUME_ML: Partial<Record<string, number>> = {
PINT: 568,
HALF_LITER: 500,
QUARTER_LITER: 250,
CAN: 440,
OTHER: 0,
}

function isLimited(sighting: VenuePriceSighting) {
return sighting.Limitations.length > 0
}

function volumeMl(sighting: VenuePriceSighting) {
return sighting.sizeMl ?? SIZE_VOLUME_ML[sighting.size] ?? 0
}

function sortPrices(prices: VenuePriceSighting[]) {
return [...prices].sort((a, b) => {
const volumeDiff = volumeMl(b) - volumeMl(a)
if (volumeDiff !== 0) return volumeDiff

const aLimited = isLimited(a)
const bLimited = isLimited(b)

if (aLimited !== bLimited) return aLimited ? 1 : -1

const priceDiff = a.price - b.price
if (priceDiff !== 0) return priceDiff

return b.timeSighted.localeCompare(a.timeSighted)
})
}
Expand Down
58 changes: 58 additions & 0 deletions apps/frontend/src/components/__tests__/VenuePrices.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,64 @@ describe('VenuePrices', () => {
expect(screen.getByText('Happy hour 15:00–17:00')).toBeInTheDocument()
})

it('sorts by volume, then regular before limited, then price', () => {
render(
<VenuePrices
prices={[
makePriceSighting({
price: 60,
size: 'HALF_LITER',
sizeMl: null,
}),
makePriceSighting({
price: 75,
size: 'PINT',
sizeMl: null,
Limitations: ['MEMBERSHIP'],
}),
makePriceSighting({
price: 89,
size: 'PINT',
sizeMl: null,
}),
makePriceSighting({
price: 65,
size: 'PINT',
sizeMl: null,
Limitations: ['TIME'],
timeStart: '1970-01-01T15:00:00.000Z',
timeEnd: '1970-01-01T17:00:00.000Z',
}),
makePriceSighting({
price: 35,
size: 'QUARTER_LITER',
sizeMl: null,
}),
makePriceSighting({
price: 55,
size: 'CAN',
sizeMl: null,
}),
makePriceSighting({
price: 45,
size: 'OTHER',
sizeMl: 330,
}),
]}
/>,
)

const rows = screen.getAllByRole('listitem').map((li) => li.textContent)

expect(rows[0]).toContain('NOK 89 · pint')
expect(rows[1]).toContain('NOK 65 · pint')
expect(rows[2]).toContain('NOK 75 · pint')
expect(rows[3]).toContain('NOK 60 · half liter')
expect(rows[4]).toContain('NOK 55 · can')
expect(rows[5]).toContain('NOK 45 · 330 ml')
expect(rows[6]).toContain('NOK 35 · quarter liter')
})

it('shows an empty state when no prices exist', () => {
render(<VenuePrices prices={[]} />)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export function ReportPriceDialog({
trigger={
<Button>
<span aria-hidden="true" className="text-xs font-bold">
kr
+
</span>
Report price
</Button>
Expand Down