Skip to content

Commit 99fa92f

Browse files
committed
feat: implement consistent fuel ordering in popups
- Add getOrderedFuelEntries method to FuelCategorizer for consistent ordering - Update API endpoints to display fuels in standard order: Unleaded, Diesel, Premium - Apply consistent ordering across mapbox endpoint, station details, and cache manager - Improve user experience with predictable fuel price layout
1 parent 19f03cd commit 99fa92f

3 files changed

Lines changed: 48 additions & 7 deletions

File tree

src/cache-manager.ts

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
/// <reference path="../worker-configuration.d.ts" />
22
import { BrandStandardizer } from './brand-standardizer'
33
import { PopupGenerator } from './popup-generator'
4-
import { CACHE_TTL, MAP_CONFIG } from './constants'
4+
import { FuelCategorizer } from './fuel-categorizer'
5+
import { CACHE_TTL, MAP_CONFIG, PRICE_THRESHOLDS } from './constants'
56

67
export interface CacheConfig {
78
defaultTtl: number;
@@ -223,9 +224,26 @@ export class CacheManager {
223224
const lat = stn.location.latitude;
224225

225226
if (lng >= bounds.west && lng <= bounds.east && lat >= bounds.south && lat <= bounds.north) {
226-
let prices: any = [];
227+
// Process fuel prices with consistent ordering
228+
let numericPrices: number[] = [];
229+
let fuelPrices: { [key: string]: number } = {};
230+
227231
for (let fuel of Object.keys(stn.prices)) {
228-
prices.push(`${fuel}: £${stn.prices[fuel]}`);
232+
let price = stn.prices[fuel];
233+
if (typeof price === 'number') {
234+
const priceInPounds = price > PRICE_THRESHOLDS.PENCE_CONVERSION ? price / 100 : price;
235+
numericPrices.push(priceInPounds);
236+
fuelPrices[fuel] = priceInPounds;
237+
}
238+
}
239+
240+
// Group fuels by category and create display strings in consistent order
241+
const groupedFuels = FuelCategorizer.groupFuelsByCategory(fuelPrices);
242+
const orderedFuels = FuelCategorizer.getOrderedFuelEntries(groupedFuels);
243+
let prices: any = [];
244+
for (const [category, data] of orderedFuels) {
245+
const displayText = FuelCategorizer.formatFuelDisplay(category, data.price, data.originalType);
246+
prices.push(displayText);
229247
}
230248

231249
features.push({

src/fuel-categorizer.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,4 +99,25 @@ export class FuelCategorizer {
9999
static getAllCategories(): FuelCategory[] {
100100
return [...this.FUEL_CATEGORIES];
101101
}
102+
103+
static getOrderedFuelEntries(groupedFuels: { [key: string]: { price: number, originalType: string } }): Array<[string, { price: number, originalType: string }]> {
104+
const orderedEntries: Array<[string, { price: number, originalType: string }]> = [];
105+
106+
// First, add fuels in the standard order (unleaded, diesel, premium)
107+
for (const category of this.FUEL_CATEGORIES) {
108+
if (groupedFuels[category.name]) {
109+
orderedEntries.push([category.name, groupedFuels[category.name]]);
110+
}
111+
}
112+
113+
// Then add any other fuel types not in the standard categories
114+
for (const [fuelType, data] of Object.entries(groupedFuels)) {
115+
const isStandardCategory = this.FUEL_CATEGORIES.some(cat => cat.name === fuelType);
116+
if (!isStandardCategory) {
117+
orderedEntries.push([fuelType, data]);
118+
}
119+
}
120+
121+
return orderedEntries;
122+
}
102123
}

src/index.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -218,9 +218,10 @@ router.get('/api/data.mapbox', async (request, env, _context) => {
218218
}
219219
}
220220

221-
// Group fuels by category and create display strings
221+
// Group fuels by category and create display strings in consistent order
222222
const groupedFuels = FuelCategorizer.groupFuelsByCategory(fuelPrices);
223-
for (const [category, data] of Object.entries(groupedFuels)) {
223+
const orderedFuels = FuelCategorizer.getOrderedFuelEntries(groupedFuels);
224+
for (const [category, data] of orderedFuels) {
224225
const displayText = FuelCategorizer.formatFuelDisplay(category, data.price, data.originalType);
225226
prices.push(displayText);
226227
}
@@ -416,9 +417,10 @@ router.get('/api/station/:stationId', async (request, env, _context) => {
416417
}
417418
}
418419

419-
// Group fuels by category and create display strings
420+
// Group fuels by category and create display strings in consistent order
420421
const groupedFuels = FuelCategorizer.groupFuelsByCategory(fuelPrices);
421-
for (const [category, data] of Object.entries(groupedFuels)) {
422+
const orderedFuels = FuelCategorizer.getOrderedFuelEntries(groupedFuels);
423+
for (const [category, data] of orderedFuels) {
422424
const displayText = FuelCategorizer.formatFuelDisplay(category, data.price, data.originalType);
423425
prices.push(displayText);
424426
}

0 commit comments

Comments
 (0)