feat: expand station cards with detailed information and add live statistics endpoint
- Add `/stats/live` endpoint returning station count and latest price timestamp with 5-minute cache - Transform StationCard into expandable component with click/keyboard interaction showing full details - Display brand label, badges (24h/Supermarket/Motorway), fuel types, amenities, opening hours, and price delta vs average - Add brand filter dropdown to StationList with dynamic brand extraction from results - Calculate and display price comparison against filtered stations average - Redesign map markers to simpler price display; move directions link to popup alongside station details - Add "locate-me" button to SearchBar for geolocation trigger - Show "Live" indicator with station count and last-update time on homepage hero - Remove standalone directions link from marker HTML; consolidate in popup with click propagation handling - Persist `avgPence` calculation across StationList and pass to cards for delta display - Add `@iconify-json/lucide` dev dependency and register collection on app mount - Stop click propagation on card action buttons (directions, remove)
This commit is contained in:
@@ -63,20 +63,17 @@ function buildDirectionsUrl(station, origin) {
|
||||
return base
|
||||
}
|
||||
|
||||
function buildMarkerHtml(station, index, colour, borderColour, origin) {
|
||||
function buildMarkerHtml(station, index, colour, borderColour) {
|
||||
const isFirst = index === 0
|
||||
const w = isFirst ? 46 : 40
|
||||
const h = isFirst ? 20 : 18
|
||||
const fontSize = isFirst ? 11 : 10
|
||||
const iconSize = isFirst ? 11 : 10
|
||||
const star = isFirst
|
||||
? `<span style="margin-right:2px;color:#facc15;font-size:10px;line-height:1;">★</span>`
|
||||
: ''
|
||||
|
||||
const directionsUrl = escHtml(buildDirectionsUrl(station, origin))
|
||||
const navSvg = `<svg xmlns="http://www.w3.org/2000/svg" width="${iconSize}" height="${iconSize}" viewBox="0 0 24 24" fill="currentColor" stroke="currentColor" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round"><polygon points="3 11 22 2 13 21 11 13 3 11"/></svg>`
|
||||
|
||||
return `<div style="display:inline-flex;align-items:center;height:${h}px;padding:0 4px 0 6px;background:${colour};color:#fff;font-weight:700;font-size:${fontSize}px;line-height:1;letter-spacing:-0.2px;border-radius:10px;border:1.5px solid ${borderColour};box-shadow:0 1px 3px rgba(0,0,0,0.25);white-space:nowrap;gap:3px;">
|
||||
${star}<span>${Number(station.price).toFixed(1)}</span><a data-directions href="${directionsUrl}" target="_blank" rel="noopener" aria-label="Directions" style="display:inline-flex;align-items:center;justify-content:center;width:${h - 6}px;height:${h - 6}px;margin-left:1px;border-radius:50%;background:rgba(255,255,255,0.22);color:#fff;text-decoration:none;">${navSvg}</a>
|
||||
return `<div style="display:inline-flex;align-items:center;justify-content:center;width:${w}px;height:${h}px;padding:0 5px;background:${colour};color:#fff;font-weight:700;font-size:${fontSize}px;line-height:1;letter-spacing:-0.2px;border-radius:10px;border:1.5px solid ${borderColour};box-shadow:0 1px 3px rgba(0,0,0,0.25);white-space:nowrap;">
|
||||
${star}${Number(station.price).toFixed(1)}
|
||||
</div>`
|
||||
}
|
||||
|
||||
@@ -197,24 +194,31 @@ function renderMarkers() {
|
||||
? '<span style="display:inline-block;background:#84cc16;color:#fff;font-size:10px;padding:1px 5px;border-radius:3px;margin-left:4px;">Supermarket</span>'
|
||||
: ''
|
||||
|
||||
const directionsUrl = escHtml(buildDirectionsUrl(station, props.origin))
|
||||
|
||||
const popup = `
|
||||
<div style="min-width:160px">
|
||||
<strong style="font-size:13px">${escHtml(station.name)}</strong>${supermarketTag}<br>
|
||||
<span style="font-size:20px;font-weight:700;color:${escHtml(colour)}">${Number(station.price).toFixed(1)}p</span><br>
|
||||
<div style="min-width:180px">
|
||||
<div style="display:flex;align-items:flex-start;justify-content:space-between;gap:8px;">
|
||||
<div style="min-width:0;flex:1;">
|
||||
<strong style="font-size:13px">${escHtml(station.name)}</strong>${supermarketTag}
|
||||
</div>
|
||||
<a data-directions href="${directionsUrl}" target="_blank" rel="noopener" aria-label="Directions" style="display:inline-flex;align-items:center;justify-content:center;width:28px;height:28px;flex-shrink:0;border-radius:8px;;color:black;text-decoration:none;"><iconify-icon icon="lucide:navigation" style="font-size:16px;"></iconify-icon></a>
|
||||
</div>
|
||||
<div style="margin-top:4px;"><span style="font-size:20px;font-weight:700;color:${escHtml(colour)}">${Number(station.price).toFixed(1)}p</span></div>
|
||||
<span style="font-size:12px;color:#6b7280">${escHtml(miles)} miles away</span><br>
|
||||
<span style="font-size:11px;color:#9ca3af">${escHtml(station.address)}, ${escHtml(station.postcode)}</span>
|
||||
</div>
|
||||
`
|
||||
|
||||
const isFirst = index === 0
|
||||
const w = isFirst ? 65 : 56
|
||||
const w = isFirst ? 46 : 40
|
||||
const h = isFirst ? 20 : 18
|
||||
|
||||
const icon = L.divIcon({
|
||||
className: '',
|
||||
iconSize: [w, h],
|
||||
iconAnchor: [w / 2, h / 2],
|
||||
html: buildMarkerHtml(station, index, colour, borderColour, props.origin),
|
||||
html: buildMarkerHtml(station, index, colour, borderColour),
|
||||
})
|
||||
|
||||
const marker = L.marker([station.lat, station.lng], {icon}).bindPopup(popup)
|
||||
@@ -224,6 +228,11 @@ function renderMarkers() {
|
||||
mapInstance.setView([station.lat, station.lng], target, {animate: true})
|
||||
})
|
||||
|
||||
marker.on('popupopen', (e) => {
|
||||
const link = e.popup.getElement()?.querySelector('a[data-directions]')
|
||||
if (link) L.DomEvent.disableClickPropagation(link)
|
||||
})
|
||||
|
||||
markersLayer.addLayer(marker)
|
||||
bounds.push([station.lat, station.lng])
|
||||
})
|
||||
|
||||
@@ -13,8 +13,8 @@
|
||||
type="button"
|
||||
@click="useMyLocation"
|
||||
>
|
||||
<iconify-icon :class="{ 'animate-spin': locating }" :icon="locating ? 'lucide:loader-circle' : 'lucide:map-pin'" style="font-size:1rem"></iconify-icon>
|
||||
<span class="hidden md:inline-flex">Near me</span>
|
||||
<iconify-icon icon="lucide:locate-fixed" style="font-size:16px;"></iconify-icon>
|
||||
<span class="hidden sr-only">Near me</span>
|
||||
</button>
|
||||
<input
|
||||
id="postcode-input"
|
||||
@@ -65,7 +65,6 @@
|
||||
<option value="price">Price</option>
|
||||
<option value="distance">Distance</option>
|
||||
<option value="updated">Updated</option>
|
||||
<option value="brand">Brand</option>
|
||||
</select>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@@ -1,12 +1,32 @@
|
||||
<template>
|
||||
<div class="bg-zinc-50 p-4 rounded-xl border border-zinc-300 shadow-sm flex flex-col gap-4">
|
||||
<div
|
||||
:aria-expanded="expanded"
|
||||
:class="[
|
||||
'bg-zinc-50 p-4 rounded-xl border shadow-sm flex flex-col gap-4 cursor-pointer transition-colors',
|
||||
expanded ? 'border-accent' : 'border-zinc-300 hover:border-zinc-400',
|
||||
]"
|
||||
role="button"
|
||||
tabindex="0"
|
||||
@click="toggle"
|
||||
@keydown.enter.prevent="toggle"
|
||||
@keydown.space.prevent="toggle"
|
||||
>
|
||||
<div class="flex justify-between items-start gap-3">
|
||||
<div class="space-y-0.5 min-w-0 flex-1">
|
||||
<h4 class="font-bold text-lg text-zinc-800 truncate">{{ station.name }}</h4>
|
||||
<p class="text-xs text-zinc-500 flex items-center gap-1">
|
||||
<iconify-icon class="text-xs" icon="lucide:map-pin"></iconify-icon>
|
||||
<span class="truncate">{{ locationLine }}</span>
|
||||
<p v-if="brandLabel" class="text-[10px] font-black uppercase tracking-widest text-zinc-500">
|
||||
{{ brandLabel }}
|
||||
</p>
|
||||
<h4 class="font-bold text-lg text-zinc-800 truncate">{{ station.name }}</h4>
|
||||
<template v-if="!expanded">
|
||||
<p class="text-xs text-zinc-500 flex items-center gap-1">
|
||||
<iconify-icon class="text-xs" icon="lucide:map-pin"></iconify-icon>
|
||||
<span class="truncate">{{ locationLine }}</span>
|
||||
</p>
|
||||
<p v-if="updatedAgo" :class="[priceColor, 'text-xs flex items-center gap-1 font-semibold']">
|
||||
<iconify-icon class="text-xs" icon="lucide:clock"></iconify-icon>
|
||||
<span>Updated {{ updatedAgo }}</span>
|
||||
</p>
|
||||
</template>
|
||||
</div>
|
||||
<a
|
||||
:href="directionsUrl"
|
||||
@@ -14,25 +34,94 @@
|
||||
class="hidden md:inline-flex w-10 h-10 items-center justify-center rounded-lg bg-accent/10 text-accent active:bg-accent/20 flex-shrink-0"
|
||||
rel="noopener"
|
||||
target="_blank"
|
||||
@click.stop
|
||||
>
|
||||
<iconify-icon class="text-lg" icon="lucide:navigation"></iconify-icon>
|
||||
</a>
|
||||
<div class="text-right flex-shrink-0">
|
||||
<div class="text-right shrink-0">
|
||||
<div :class="priceColor" class="text-xl font-black">
|
||||
{{ station.price }}<span class="text-sm font-bold uppercase ml-0.5">p</span>
|
||||
</div>
|
||||
<p :class="priceColor" class="text-[10px] font-bold uppercase tracking-wider">
|
||||
{{ statusLabel }}
|
||||
</p>
|
||||
<p v-if="priceDelta" :class="priceDeltaColor" class="text-[10px] font-bold mt-0.5">
|
||||
{{ priceDelta }}
|
||||
</p>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<Transition
|
||||
enter-active-class="transition duration-200 ease-out"
|
||||
enter-from-class="opacity-0 -translate-y-1"
|
||||
enter-to-class="opacity-100 translate-y-0"
|
||||
leave-active-class="transition duration-150 ease-in"
|
||||
leave-from-class="opacity-100"
|
||||
leave-to-class="opacity-0"
|
||||
>
|
||||
<div v-if="expanded" class="border-t border-zinc-200 pt-3 space-y-3">
|
||||
<div v-if="badges.length" class="flex flex-wrap gap-1.5">
|
||||
<span
|
||||
v-for="badge in badges"
|
||||
:key="badge.label"
|
||||
:class="[badge.class, 'inline-flex items-center gap-1 px-2 py-0.5 rounded text-[10px] font-bold uppercase tracking-wider']"
|
||||
>
|
||||
<iconify-icon v-if="badge.icon" :icon="badge.icon"></iconify-icon>
|
||||
{{ badge.label }}
|
||||
</span>
|
||||
</div>
|
||||
|
||||
<div v-if="fuelTypes.length" class="flex flex-wrap gap-1.5">
|
||||
<span
|
||||
v-for="type in fuelTypes"
|
||||
:key="type"
|
||||
class="inline-block bg-zinc-200 text-zinc-600 text-[10px] font-bold px-2 py-0.5 rounded"
|
||||
>
|
||||
{{ type }}
|
||||
</span>
|
||||
</div>
|
||||
|
||||
<div class="flex flex-wrap items-center gap-2 text-xs text-zinc-500">
|
||||
<span>{{ distanceMiles }} mi</span>
|
||||
<span
|
||||
v-if="openStatus"
|
||||
:class="[
|
||||
openChipClass,
|
||||
'inline-flex items-center gap-1 px-2 py-0.5 rounded text-[11px] font-bold',
|
||||
]"
|
||||
>
|
||||
<span
|
||||
:class="[openDotClass, 'inline-block size-1.5 rounded-full']"
|
||||
aria-hidden="true"
|
||||
></span>
|
||||
{{ openStatus }}
|
||||
</span>
|
||||
</div>
|
||||
|
||||
<div v-if="amenityItems.length" class="flex flex-wrap gap-3 text-xs text-zinc-500">
|
||||
<span
|
||||
v-for="item in amenityItems"
|
||||
:key="item.key"
|
||||
class="inline-flex items-center gap-1"
|
||||
>
|
||||
<iconify-icon :icon="item.icon" class="text-sm"></iconify-icon>
|
||||
{{ item.label }}
|
||||
</span>
|
||||
</div>
|
||||
|
||||
<p class="text-[11px] text-zinc-400">
|
||||
{{ fullAddress }}<span v-if="updatedAgo"> · Updated {{ updatedAgo }}</span>
|
||||
</p>
|
||||
</div>
|
||||
</Transition>
|
||||
|
||||
<div class="flex items-center justify-end gap-2 md:hidden">
|
||||
<button
|
||||
v-if="removable"
|
||||
aria-label="Remove"
|
||||
class="w-10 h-10 flex items-center justify-center rounded-lg bg-zinc-200 text-zinc-500 active:bg-zinc-300"
|
||||
type="button"
|
||||
@click="emit('remove', station)"
|
||||
@click.stop="emit('remove', station)"
|
||||
>
|
||||
<iconify-icon class="text-lg" icon="lucide:trash-2"></iconify-icon>
|
||||
</button>
|
||||
@@ -41,6 +130,7 @@
|
||||
class="flex-1 h-10 flex items-center justify-center gap-2 rounded-lg bg-accent/10 text-accent font-bold text-sm active:bg-accent/20"
|
||||
rel="noopener"
|
||||
target="_blank"
|
||||
@click.stop
|
||||
>
|
||||
<iconify-icon class="text-lg" icon="lucide:navigation"></iconify-icon>
|
||||
Directions
|
||||
@@ -51,7 +141,7 @@
|
||||
aria-label="Remove"
|
||||
class="w-10 h-10 flex items-center justify-center rounded-lg bg-zinc-200 text-zinc-500 active:bg-zinc-300"
|
||||
type="button"
|
||||
@click="emit('remove', station)"
|
||||
@click.stop="emit('remove', station)"
|
||||
>
|
||||
<iconify-icon class="text-lg" icon="lucide:trash-2"></iconify-icon>
|
||||
</button>
|
||||
@@ -60,23 +150,50 @@
|
||||
</template>
|
||||
|
||||
<script setup>
|
||||
import { computed } from 'vue'
|
||||
import { computed, ref } from 'vue'
|
||||
|
||||
const props = defineProps({
|
||||
station: { type: Object, required: true },
|
||||
lowestPrice: { type: Number, default: null },
|
||||
avgPence: { type: Number, default: null },
|
||||
removable: { type: Boolean, default: false },
|
||||
origin: { type: Object, default: null },
|
||||
})
|
||||
|
||||
const emit = defineEmits(['remove'])
|
||||
|
||||
const expanded = ref(false)
|
||||
|
||||
function toggle() {
|
||||
expanded.value = !expanded.value
|
||||
}
|
||||
|
||||
const RELIABILITY_MAP = {
|
||||
reliable: { label: 'Current', color: 'text-status-good' },
|
||||
stale: { label: 'Stale', color: 'text-status-warn' },
|
||||
outdated: { label: 'Outdated', color: 'text-status-bad' },
|
||||
}
|
||||
|
||||
const AMENITY_META = {
|
||||
customer_toilets: { icon: 'lucide:toilet', label: 'WC' },
|
||||
car_wash: { icon: 'lucide:spray-can', label: 'Wash' },
|
||||
air_pump_or_screenwash: { icon: 'lucide:wind', label: 'Air' },
|
||||
adblue_pumps: { icon: 'lucide:fuel', label: 'AdBlue' },
|
||||
adblue_packaged: { icon: 'lucide:package', label: 'AdBlue' },
|
||||
lpg_pumps: { icon: 'lucide:flame', label: 'LPG' },
|
||||
water_filling: { icon: 'lucide:droplets', label: 'Water' },
|
||||
}
|
||||
|
||||
const AMENITY_ORDER = [
|
||||
'customer_toilets',
|
||||
'car_wash',
|
||||
'air_pump_or_screenwash',
|
||||
'adblue_pumps',
|
||||
'adblue_packaged',
|
||||
'lpg_pumps',
|
||||
'water_filling',
|
||||
]
|
||||
|
||||
const reliabilityInfo = computed(() => RELIABILITY_MAP[props.station.reliability] ?? RELIABILITY_MAP.reliable)
|
||||
|
||||
const priceColor = computed(() => {
|
||||
@@ -95,6 +212,21 @@ const locationLine = computed(() => {
|
||||
return parts.join(' • ')
|
||||
})
|
||||
|
||||
const fullAddress = computed(() => {
|
||||
const parts = [props.station.address, props.station.postcode].filter(Boolean)
|
||||
return parts.join(', ')
|
||||
})
|
||||
|
||||
const updatedAgo = computed(() => {
|
||||
if (!props.station.price_updated_at) return ''
|
||||
const diffMin = Math.floor((Date.now() - new Date(props.station.price_updated_at)) / 60000)
|
||||
if (diffMin < 60) return `${Math.max(diffMin, 0)}m ago`
|
||||
const hours = Math.floor(diffMin / 60)
|
||||
if (hours < 24) return `${hours}h ago`
|
||||
const days = Math.floor(hours / 24)
|
||||
return `${days} day${days === 1 ? '' : 's'} ago`
|
||||
})
|
||||
|
||||
const directionsUrl = computed(() => {
|
||||
const { lat, lng } = props.station
|
||||
const base = `https://www.google.com/maps/dir/?api=1&destination=${lat},${lng}`
|
||||
@@ -103,4 +235,77 @@ const directionsUrl = computed(() => {
|
||||
}
|
||||
return base
|
||||
})
|
||||
|
||||
const brandLabel = computed(() => {
|
||||
const brand = props.station.brand
|
||||
if (!brand) return ''
|
||||
if (brand === props.station.name) return ''
|
||||
return brand
|
||||
})
|
||||
|
||||
const badges = computed(() => {
|
||||
const list = []
|
||||
if (props.station.is_supermarket) {
|
||||
list.push({ label: 'Supermarket', icon: 'lucide:shopping-cart', class: 'bg-lime-500/15 text-lime-700' })
|
||||
}
|
||||
if (props.station.open_today?.is_24_hours || props.station.amenities?.includes('twenty_four_hour_fuel')) {
|
||||
list.push({ label: '24h', icon: 'lucide:clock', class: 'bg-zinc-800 text-white' })
|
||||
}
|
||||
if (props.station.is_motorway) {
|
||||
list.push({ label: 'Motorway', icon: 'lucide:road', class: 'bg-blue-500/15 text-blue-700' })
|
||||
}
|
||||
return list
|
||||
})
|
||||
|
||||
const fuelTypes = computed(() => {
|
||||
const types = props.station.fuel_types_available
|
||||
if (!Array.isArray(types)) return []
|
||||
return types.map(t => t.replace('_STANDARD', '').replace('_PREMIUM', '+').toUpperCase())
|
||||
})
|
||||
|
||||
const amenityItems = computed(() => {
|
||||
const amenities = props.station.amenities
|
||||
if (!Array.isArray(amenities)) return []
|
||||
return AMENITY_ORDER
|
||||
.filter(key => amenities.includes(key) && AMENITY_META[key])
|
||||
.map(key => ({ key, ...AMENITY_META[key] }))
|
||||
})
|
||||
|
||||
const openStatus = computed(() => {
|
||||
const open = props.station.open_today
|
||||
if (!open) return ''
|
||||
if (open.is_24_hours) return ''
|
||||
if (open.is_open_now) return open.close ? `Open until ${open.close}` : 'Open now'
|
||||
return open.open ? `Closed — opens ${open.open}` : 'Closed'
|
||||
})
|
||||
|
||||
const isOpenNow = computed(() => {
|
||||
const open = props.station.open_today
|
||||
return Boolean(open?.is_24_hours || open?.is_open_now)
|
||||
})
|
||||
|
||||
const openChipClass = computed(() => (
|
||||
isOpenNow.value
|
||||
? 'bg-status-good/15 text-status-good'
|
||||
: 'bg-status-bad/10 text-status-bad'
|
||||
))
|
||||
|
||||
const openDotClass = computed(() => (
|
||||
isOpenNow.value ? 'bg-status-good' : 'bg-status-bad'
|
||||
))
|
||||
|
||||
const priceDelta = computed(() => {
|
||||
if (props.avgPence == null) return ''
|
||||
const delta = props.station.price_pence - props.avgPence
|
||||
const pence = Math.abs(delta) / 100
|
||||
if (pence < 0.1) return 'as expected'
|
||||
return `${pence.toFixed(1)}p ${delta < 0 ? 'below' : 'above'} average`
|
||||
})
|
||||
|
||||
const priceDeltaColor = computed(() => {
|
||||
if (props.avgPence == null) return 'text-zinc-500'
|
||||
if (props.station.price_pence < props.avgPence) return 'text-status-good'
|
||||
if (props.station.price_pence > props.avgPence) return 'text-status-bad'
|
||||
return 'text-zinc-500'
|
||||
})
|
||||
</script>
|
||||
|
||||
@@ -1,13 +1,13 @@
|
||||
<template>
|
||||
<div class="space-y-3">
|
||||
<!-- Sort tabs -->
|
||||
<div class="flex gap-2 flex-wrap">
|
||||
<!-- Sort tabs + brand filter -->
|
||||
<div class="flex gap-2 flex-wrap items-center">
|
||||
<button
|
||||
v-for="option in sortOptions"
|
||||
:key="option.value"
|
||||
@click="emit('sort', option.value)"
|
||||
:class="[
|
||||
'px-4 py-1.5 rounded-full text-sm font-bold transition-colors',
|
||||
'h-10 px-4 rounded-xl text-sm font-bold transition-colors',
|
||||
currentSort === option.value
|
||||
? 'bg-accent text-white'
|
||||
: 'bg-white border border-zinc-300 text-zinc-500 hover:border-accent'
|
||||
@@ -15,11 +15,23 @@
|
||||
>
|
||||
{{ option.label }}
|
||||
</button>
|
||||
|
||||
<select
|
||||
v-if="availableBrands.length > 1"
|
||||
v-model="brandFilter"
|
||||
aria-label="Filter by brand"
|
||||
class="min-w-0 h-10 px-2 bg-white border border-zinc-300 rounded-xl text-sm font-medium text-zinc-700 focus:outline-none focus:ring-2 focus:ring-accent"
|
||||
>
|
||||
<option value="">All brands</option>
|
||||
<option v-for="brand in availableBrands" :key="brand" :value="brand">{{ brand }}</option>
|
||||
</select>
|
||||
</div>
|
||||
|
||||
<!-- Count -->
|
||||
<p class="text-sm text-zinc-500 font-medium">
|
||||
{{ stations.length }} station{{ stations.length !== 1 ? 's' : '' }} found
|
||||
{{ filteredStations.length }} station{{ filteredStations.length !== 1 ? 's' : '' }}
|
||||
<span v-if="brandFilter">matching <strong>{{ brandFilter }}</strong></span>
|
||||
<span v-else>found</span>
|
||||
</p>
|
||||
|
||||
<!-- Grouped results when sorting by reliability -->
|
||||
@@ -33,6 +45,7 @@
|
||||
<StationCard
|
||||
v-for="station in reliable"
|
||||
:key="station.station_id"
|
||||
:avg-pence="avgPence"
|
||||
:lowest-price="lowestPrice"
|
||||
:origin="origin"
|
||||
:station="station"
|
||||
@@ -49,7 +62,9 @@
|
||||
<StationCard
|
||||
v-for="station in stale"
|
||||
:key="station.station_id"
|
||||
:avg-pence="avgPence"
|
||||
:lowest-price="lowestPrice"
|
||||
:origin="origin"
|
||||
:station="station"
|
||||
class="mb-2"
|
||||
/>
|
||||
@@ -66,7 +81,9 @@
|
||||
<StationCard
|
||||
v-for="station in outdated"
|
||||
:key="station.station_id"
|
||||
:avg-pence="avgPence"
|
||||
:lowest-price="lowestPrice"
|
||||
:origin="origin"
|
||||
:station="station"
|
||||
class="mb-2"
|
||||
/>
|
||||
@@ -77,18 +94,19 @@
|
||||
<!-- Flat list for other sort modes -->
|
||||
<div v-else class="space-y-2">
|
||||
<StationCard
|
||||
v-for="station in stations"
|
||||
v-for="station in filteredStations"
|
||||
:key="station.station_id"
|
||||
:station="station"
|
||||
:avg-pence="avgPence"
|
||||
:lowest-price="lowestPrice"
|
||||
:origin="origin"
|
||||
:station="station"
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<script setup>
|
||||
import { computed } from 'vue'
|
||||
import { computed, ref } from 'vue'
|
||||
import StationCard from './StationCard.vue'
|
||||
|
||||
const props = defineProps({
|
||||
@@ -99,21 +117,41 @@ const props = defineProps({
|
||||
|
||||
const emit = defineEmits(['sort'])
|
||||
|
||||
const brandFilter = ref('')
|
||||
|
||||
const sortOptions = [
|
||||
{ label: 'Reliable', value: 'reliable' },
|
||||
{ label: 'Price', value: 'price' },
|
||||
{ label: 'Distance', value: 'distance' },
|
||||
{ label: 'Updated', value: 'updated' },
|
||||
{ label: 'Brand', value: 'brand' },
|
||||
]
|
||||
|
||||
const reliable = computed(() => props.stations.filter(s => s.reliability === 'reliable'))
|
||||
const stale = computed(() => props.stations.filter(s => s.reliability === 'stale'))
|
||||
const outdated = computed(() => props.stations.filter(s => s.reliability === 'outdated'))
|
||||
const availableBrands = computed(() => {
|
||||
const brands = new Set()
|
||||
props.stations.forEach(s => {
|
||||
if (s.brand) brands.add(s.brand)
|
||||
})
|
||||
return [...brands].sort((a, b) => a.localeCompare(b))
|
||||
})
|
||||
|
||||
const filteredStations = computed(() => {
|
||||
if (!brandFilter.value) return props.stations
|
||||
return props.stations.filter(s => s.brand === brandFilter.value)
|
||||
})
|
||||
|
||||
const reliable = computed(() => filteredStations.value.filter(s => s.reliability === 'reliable'))
|
||||
const stale = computed(() => filteredStations.value.filter(s => s.reliability === 'stale'))
|
||||
const outdated = computed(() => filteredStations.value.filter(s => s.reliability === 'outdated'))
|
||||
|
||||
const lowestPrice = computed(() => {
|
||||
if (!reliable.value.length && !props.stations.length) return null
|
||||
const pool = reliable.value.length ? reliable.value : props.stations
|
||||
if (!reliable.value.length && !filteredStations.value.length) return null
|
||||
const pool = reliable.value.length ? reliable.value : filteredStations.value
|
||||
return Math.min(...pool.map(s => s.price_pence))
|
||||
})
|
||||
|
||||
const avgPence = computed(() => {
|
||||
const prices = filteredStations.value.map(s => s.price_pence).filter(p => typeof p === 'number')
|
||||
if (!prices.length) return null
|
||||
return prices.reduce((a, b) => a + b, 0) / prices.length
|
||||
})
|
||||
</script>
|
||||
|
||||
Reference in New Issue
Block a user